I have been playing around with creating docker files and reading the documentation and I was wondering this question: Does adding an EXPOSE
command to my Dockerfile
add a layer? (and if it does why would I care/ does it matter where it is placed inside the file?)
It is not specifly said in the documentation.
I understand RUN
, COPY
and ADD
create layers because they change the file system, but expose simply adds metadata to the container, does it's change generates a layer?
Exposing ports is a way of documenting which ports are used, but does not actually map or open any ports. Exposing ports is optional. You publish ports using the --publish or --publish-all flag to docker run . This tells Docker which ports to open on the container's network interface.
Description. The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command.
The WORKDIR instruction wont create a new layer in the image but will add metadata to the image config. If the WORKDIR doesn't exist, it will be created even if it's not used in any subsequent Dockerfile instruction.
A Docker image consists of several layers. Each layer corresponds to certain instructions in your Dockerfile . The following instructions create a layer: RUN , COPY , ADD . The other instructions will create intermediate layers and do not influence the size of your image.
Yes, every instruction in a Dockerfile generates a new layer for the resulting image.
However, layers created via EXPOSE
are empty layers. That is, their size is 0 bytes.
While they don't impact you storage-wise, they do count for leveraging layer cache while building or pulling/pushing images from a registry.
A good way to understand an image's layers is to use the docker history
command. For instance, given the following Dockerfile:
FROM scratch
EXPOSE 4000
EXPOSE 3000
do
docker build -t test/image .
If you then docker history test/image
you'll see:
IMAGE CREATED CREATED BY SIZE COMMENT
ab9f435de7bc 4 seconds ago /bin/sh -c #(nop) EXPOSE 4000/tcp 0 B
15e09691c313 5 seconds ago /bin/sh -c #(nop) EXPOSE 3000/tcp 0 B
If you switch the order of the EXPOSE
statements and build again, you'll see the layer cache being ignored.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With