Home  >  Q&A  >  body text

linux - Docker中镜像和容器的关系是什么?

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

迷茫迷茫2744 days ago1057

reply all(12)I'll reply

  • 高洛峰

    高洛峰2017-04-17 15:08:47

    A container is an instance of an image running!

    reply
    0
  • 迷茫

    迷茫2017-04-17 15:08:47

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

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 15:08:47

    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.

    reply
    0
  • 黄舟

    黄舟2017-04-17 15:08:47

    It can be compared to the relationship between file and process

    reply
    0
  • 黄舟

    黄舟2017-04-17 15:08:47

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

    reply
    0
  • 黄舟

    黄舟2017-04-17 15:08:47

    is like the relationship between 墙壁 and 壁纸

    reply
    0
  • 阿神

    阿神2017-04-17 15:08:47

    container = image + docker run

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 15:08:47

    Such as and 实例.

    Docker run is to instantiate a class.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 15:08:47

    The image is the system file of the container

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 15:08:47

    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

    reply
    0
  • Cancelreply