参考文章:https://segmentfault.com/q/1010000004611666 感谢作者分享
镜像是文件, 容器是进程。 容器是基于镜像创建的, 即容器中的进程依赖于镜像中的文件, 这里的文件包括进程运行所需要的可执行文件, 依赖软件, 库文件, 配置文件等等...
不妨通过一个实例来获得更直观的认识。假设你需要把nginx(web服务器)运行在Docker容器中, 则第一步需要下载nginx镜像:
sudo docker pull nginx
下载nginx镜像之后可以查看Docker镜像:
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 0d409d33b27e 2 weeks ago 182.7 MB
-
可知, nginx镜像的大小为182.7MB。
-
事实上, nginx镜像并非一个单独的文件,而是具有层级结构
sudo docker history nginx
IMAGE CREATED CREATED BY SIZE COMMENT
0d409d33b27e 2 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon o 0 B
<missing> 2 weeks ago /bin/sh -c #(nop) EXPOSE 443/tcp 80/tcp 0 B
<missing> 2 weeks ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx/ 0 B
<missing> 2 weeks ago /bin/sh -c apt-key adv --keyserver hkp://pgp. 57.67 MB
<missing> 2 weeks ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.11.1-1~ 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) MAINTAINER NGINX Docker Mai 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:5d8521419ad6cfb695 125.1 MB
可知, nginix镜像一共有8层, 其中, 第1层为125.1MB, 第5层为57.67MB, 其他层的大小可以忽略。
将nginx运行在Docker容器中:
sudo docker run -itd \
-p 80:80 \
--name=nginx \
nginx
查看nginx容器
sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
263e88fc53d3 nginx "nginx -g 'daemon off" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp, 443/tcp nginx
可知, nginx容器运行成功。
查看nginx容器中的进程
sudo docker exec nginx ps aux | grep -v ps
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 31680 2872 ? Ss+ 05:18 0:00 nginx: master process nginx -g daemon off;
nginx 5 0.0 0.0 32072 1696 ? S+ 05:18 0:00 nginx: worker process
可知, nginx容器中一共运行了两个进程, 分别是nginx的master和worker进程。
容器中进程的COMMAND为"nginx -g daemon off", 但是, 主机上并未安装nginx, nginx安装在Docker镜像中。
不妨再次查看nginx镜像的各个层,这时使用"--no-trunc"显示详细内容:
docker history --no-trunc nginx
IMAGE CREATED CREATED BY SIZE COMMENT
sha256:0d409d33b27e47423b049f7f863faa08655a8c901749c2b25b93ca67d01a470d 2 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon off;"] 0 B
<missing> 2 weeks ago /bin/sh -c #(nop) EXPOSE 443/tcp 80/tcp 0 B
<missing> 2 weeks ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log 0 B
<missing> 2 weeks ago /bin/sh -c apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 && echo "deb http://nginx.org/packages/mainline/debian/ jessie nginx" >> /etc/apt/sources.list && apt-get update && apt-get install --no-install-recommends --no-install-suggests -y ca-certificates nginx=${NGINX_VERSION} nginx-module-xslt nginx-module-geoip nginx-module-image-filter nginx-module-perl nginx-module-njs gettext-base && rm -rf /var/lib/apt/lists/* 57.67 MB
<missing> 2 weeks ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.11.1-1~jessie 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) MAINTAINER NGINX Docker Maintainers "docker-maint@nginx.com" 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:5d8521419ad6cfb6956ed26ab70a44422d512f82462046ba8e68b7dcb8283f7e in / 125.1 MB
其中, 第5层为57.67MB, 它的CREATED BY是这样的(把连接符号&&换为换行符):
apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
echo "deb http://nginx.org/packages/mainline/debian/ jessie nginx" >> /etc/apt/sources.list
apt-get update
apt-get install --no-install-recommends --no-install-suggests -y ca-certificates nginx=${NGINX_VERSION} nginx-module-xslt nginx-module-geoip nginx-module-image-filter nginx-module-perl nginx-module-njs gettext-base
rm -rf /var/lib/apt/lists/*
可知, nginx镜像的第5层安装了nginx