Each line in a Dockerfile is a layer in an image
What has helped me grok docker a bit better is knowing that every line in a Dockerfile has a corresponding hash identifier after the image has been built. Here is a sample Dockerfile:
FROM alpine
RUN echo 'helloworld' > helloworld.txt
CMD ["cat", "helloworld.txt"]
I create the image with:
docker build -t helloworld .
I can now examine each layer in the Dockerfile with docker history helloworld
IMAGE CREATED CREATED BY SIZE COMMENT
1e5d27ca20a8 13 hours ago /bin/sh -c #(nop) CMD ["env"] 0B
84f489011989 13 hours ago /bin/sh -c echo "Hello World" > helloworld.t… 12B
3fd9065eaf02 2 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
<missing> 2 months ago /bin/sh -c #(nop) ADD file:093f0723fa46f6cdb… 4.15MB
Three commands and four layers. The FROM alpine
command is actually 2 layers that have been squashed together. You can see the <missing>
hash for the initial command because it has been squashed into 3fd9065
.
The command that creates the helloworld.txt file has a size of 12 bytes because thats the size of the file that was created.
Tweet