What are Docker image “layers”?

后端 未结 9 1617
情话喂你
情话喂你 2020-11-30 17:02

I am brand new to Docker and am trying to understand exactly what a Docker image is. Every single definition of a Docker image uses the term \"layer\", but does not

9条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 18:01

    I used to think they are like diffs on previous layers. After reading some of the answers here I was not so sure; they are described as sets of changes to the filesystem. I've written some Dockerfiles to show they are more like diffs, ie, they really depend on previous layers.

    Given these two Dockerfiles

    FROM bash
    RUN mkdir /data
    RUN dd if=/dev/zero bs=1024 count=1024 of=/data/one
    RUN dd if=/dev/zero bs=1024 count=1024 of=/data/two
    RUN dd if=/dev/zero bs=1024 count=1024 of=/data/three
    

    and

    FROM bash
    RUN mkdir /data
    RUN dd if=/dev/zero bs=1024 count=1024 of=/data/three
    RUN dd if=/dev/zero bs=1024 count=1024 of=/data/two
    RUN dd if=/dev/zero bs=1024 count=1024 of=/data/one
    

    one would expect the same set of layers if they just were about changes to the filesystem, but this is not the case:

    $ docker history img_1
    IMAGE               CREATED             CREATED BY                                      SIZE
    30daa166a9c5        6 minutes ago       /bin/sh -c dd if=/dev/zero bs=1024 count=102…   1.05MB
    4467d16e79f5        6 minutes ago       /bin/sh -c dd if=/dev/zero bs=1024 count=102…   1.05MB
    c299561fd031        6 minutes ago       /bin/sh -c dd if=/dev/zero bs=1024 count=102…   1.05MB
    646feb178431        6 minutes ago       /bin/sh -c mkdir /data                          0B
    78664daf24f4        2 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B
               2 weeks ago         /bin/sh -c #(nop)  ENTRYPOINT ["docker-entry…   0B
    
    

    and

    $ docker history img_2
    IMAGE               CREATED             CREATED BY                                      SIZE
    f55c91305f8c        6 minutes ago       /bin/sh -c dd if=/dev/zero bs=1024 count=102…   1.05MB
    29b3b627c76f        6 minutes ago       /bin/sh -c dd if=/dev/zero bs=1024 count=102…   1.05MB
    18360be603aa        6 minutes ago       /bin/sh -c dd if=/dev/zero bs=1024 count=102…   1.05MB
    646feb178431        6 minutes ago       /bin/sh -c mkdir /data                          0B
    78664daf24f4        2 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B
               2 weeks ago         /bin/sh -c #(nop)  ENTRYPOINT ["docker-entry…   0B
    
    

    You can see how, even if the changes to the filesystem are the same in both cases, the order matters.

提交回复
热议问题