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 Doc
No, directive EXPOSE
does not add a new layer. This information will be stored permanently in the image and container config and could be retrieved via:
docker inspect --format '{{.Config.ExposedPorts}}'
But you may still be wondering why there is a line in the output that says that the new image was created for this command. Consider this Dockerfile
:
FROM alpine
EXPOSE 8000
In the end, Docker produces such output:
Step 1/2 : FROM alpine
---> 965ea09ff2eb
Step 2/2 : EXPOSE 8000
---> Running in 6c8fae4f3499
Removing intermediate container 6c8fae4f3499
---> 067aa2abe94f
Successfully built 067aa2abe94f
Successfully tagged envtest:latest
In the same time docker history
outlines:
IMAGE CREATED CREATED BY SIZE COMMENT
067aa2abe94f About a minute ago /bin/sh -c #(nop) EXPOSE 8000 0B
965ea09ff2eb 7 weeks ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
Every line in the Dockerfile
causes image creation on the top of the current image, thus image has a link to the parent image. Afterward, the immediate container will be initialized based on this new image then your command will be executed within and result committed to the image.
As a general rule, if the command does not lead to changes in the filesystem the new layer won't be created. I would recommend using dive for exploring each layer in a docker image.