(一)最小的镜像
镜像是 Docker 容器的基石,容器是镜像的运行实例,有了镜像才能启动容器。
(1)镜像的内部结构
为什么我们要讨论镜像的内部结构?如果只是使用镜像,当然不需要了解,直接通过 docker
命令下载和运行就可以了。但如果我们想创建自己的镜像,或者想理解 Docker 为什么是轻量级的,就非常有必要学习这部分知识了。
(2)hello-word最小的镜像
hello-world 是 Docker 官方提供的一个镜像,通常用来验证 Docker 是否安装成功。我们先通过 docker pull
从 Docker Hub 下载它。
root@cuiyongchao:~# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Already exists
Digest: sha256:8c5aeeb6a5f3ba4883347d3747a7249f491766ca1caa47e5da5dfcf6b9b717c0
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
root@cuiyongchao:~#
通过docker images 命令查看镜像详细信息:
root@cuiyongchao:~# docker images hello-world
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 9 months ago 13.3kB
root@cuiyongchao:~#
使用docker run运行容器:
root@cuiyongchao:~# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
其实我们更关心 hello-world 镜像包含哪些内容。
Dockerfile 是镜像的描述文件,定义了如何构建 Docker 镜像。Dockerfile 的语法简洁且可读性强,后面我们会专门讨论如何编写 Dockerfile。hello-world 的 Dockerfile 内容如下:
FROM scratch
COPY hello /
CMD ["/hello"]
-
FROM scratch,此镜像是从白手起家,从 0 开始构建。
-
COPY hello /,将文件“hello”复制到镜像的根目录。
-
CMD ["/hello"],容器启动时,执行 /hello
镜像 hello-world 中就只有一个可执行文件 “hello”,其功能就是打印出 “Hello from Docker ......” 等信息。 /hello 就是文件系统的全部内容,连最基本的 /bin,/usr, /lib, /dev 都没有。hello-world 虽然是一个完整的镜像,但它并没有什么实际用途。通常来说,我们希望镜像能提供一个基本的操作系统环境,用户可以根据需要安装和配置软件。这样的镜像我们称作 base 镜像。