linux - Docker中镜像和容器的关系是什么?
迷茫
迷茫 2017-04-17 15:06:47
0
12
1030

Docker中镜像和容器的关系是什么?
看了几个版本,有点不太清晰。
是容器里面放置镜像?
还是镜像本身就是一个容器?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(12)
小葫芦

A container is an instance of an image running!

迷茫

Have you ever played a virtual machine? Relatively speaking, it is Ios files and virtual systems.

左手右手慢动作

Images are files and containers are processes. Containers are created based on images, that is, the processes in the container depend on the files in the image. The files here include executable files required for the process to run, dependent software, library files, configuration files, etc...

Want to use an example to gain a more intuitive understanding.

Suppose you need to run nginx (web server) in a Docker container, then the first step is to download the nginx image:

sudo docker pull nginx

After downloading the nginx image, you can view the Docker image:

sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              0d409d33b27e        2 weeks ago         182.7 MB
  • It can be seen that the size of the nginx image is 182.7MB.

In fact, the nginx image is not a separate file, but has a hierarchical structure:

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 
  • It can be seen that the nginix image has a total of 8 layers. Among them, the first layer is 125.1MB, the fifth layer is 57.67MB, and the size of other layers can be ignored.

Run nginx in a Docker container:

sudo docker run -itd \
                -p 80:80 \
                --name=nginx \
                nginx 

View nginx container

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
  • It can be seen that the nginx container runs successfully.

View processes in nginx container

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
  • It can be seen that there are two processes running in the nginx container, which are the master and worker processes of nginx.

  • The COMMAND of the process in the container is "nginx -g daemon off". However, nginx is not installed on the host, and nginx is installed in the Docker image.

You might as well view each layer of the nginx image again, then use "--no-trunc" to display the details:

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  

Among them, the 5th layer is 57.67MB, and its CREATED BY is like this (replace the connection symbol && with a newline character):

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/*
  • It can be seen that nginx is installed on the fifth layer of the nginx image.

黄舟

It can be compared to the relationship between file and process

黄舟

An image can be understood as an ios system CD document
A container is a system that can be run after being installed

黄舟

is like the relationship between 墙壁 and 壁纸

阿神

container = image + docker run

巴扎黑

Such as and 实例.

Docker run is to instantiate a class.

伊谢尔伦

The image is the system file of the container

小葫芦

An image is equivalent to a standardized template. Starting a container is equivalent to instantiating an image for use. After destroying the container, only the instance is destroyed, and the image is still there

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!