docker中 映象 容器區別 關係
阿新 • • 發佈:2018-12-26
映象是檔案, 容器是程序。 容器是基於映象建立的, 即容器中的程序依賴於映象中的檔案, 這裡的檔案包括程序執行所需要的可執行檔案, 依賴軟體, 庫檔案, 配置檔案等等...
不妨通過一個例項來獲得更直觀的認識。假設你需要把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映象並非一個單獨的檔案,而是具有層級結構
可知, nginix映象一共有8層, 其中, 第1層為125.1MB, 第5層為57.67MB, 其他層的大小可以忽略。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
將nginx執行在Docker容器中:
sudo docker run -itd \
-p 80:80 \
--name=nginx \
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容器中的程序
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 "[email protected]" 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