Docker – The EXPOSE instruction

Installing MySQL On CentOS 8

The EXPOSE instruction is a way to document what network ports the image expects to be opened when a container is run from the image built using the Dockerfile. The syntax for the EXPOSE instruction is as follows:

# EXPOSE instruction syntax
EXPOSE <port> [<port>/<protocol>...]

It is important to understand that including the EXPOSE instruction in the Dockerfile does not actually open network ports in containers. When containers are run from the images with the EXPOSE instruction in their Dockerfile, it is still necessary to include the -p or -P parameters to actually open the network ports to the container.

You can include multiple EXPOSE instructions in your Dockerfile as needed. Including the -P parameter at runtime is a shortcut way to automatically open ports for all of the EXPOSE instructions included in the Dockerfile. The corresponding host ports will be randomly assigned when using the -P parameter on the run command.

Think of the EXPOSE instruction as a message from the image developer telling you that the application in the image is expecting you to open the indicated port(s) when you run your containers. The EXPOSE instruction creates a zero-byte-sized layer in the resulting image.

Comments are closed.