What are Docker image “layers”?

后端 未结 9 1575
情话喂你
情话喂你 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 17:59

    My personal understanding is that we can compare docker layer to github commit. For your base image(your fresh master repo), you make several commits, every commit is changing your master status, it's the same in docker, every layer is doing some operation based on previous intermediate layer. And then, this layer become a new intermediate layer to the next layer.

    0 讨论(0)
  • 2020-11-30 18:00

    Per Docker's image spec via The Moby Project:

    Images are composed of layers. Each layer is a set of filesystem changes. Layers do not have configuration metadata such as environment variables or default arguments - these are properties of the image as a whole rather than any particular layer.

    So, essentially, a layer is just a set of changes made to the filesystem.

    0 讨论(0)
  • 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
    <missing>           2 weeks ago         /bin/sh -c #(nop)  ENTRYPOINT ["docker-entry…   0B
    <more missing...>
    

    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
    <missing>           2 weeks ago         /bin/sh -c #(nop)  ENTRYPOINT ["docker-entry…   0B
    <more missing...>
    

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

    0 讨论(0)
提交回复
热议问题