Docker - EXPOSE ports in Dockerfile

This assumes you have installed Docker on Linux and Docker is running.

A Docker image contains the code used to create a Docker container, such as creating a Nginx web server, or a mySQL server, or a home grown app, and the list goes on. In this way, an image is like a template used to create a container. An image is kind of like a virtual machine, but much more light weight, using significantly less storage a memory (containers are usually megabytes in size).

 

When building an image using the docker build command and a Dockerfile, the EXPOSE option can be used to define in ports that are meant to be used by the application or service in the image. 

This does NOT add a rule to iptables to allow the port, meaning you will still need to use the -p or --publish option when using the docker run command to create the container from the image.

In this example, TCP port 8080 will be exposed in the image. If TCP or UDP are not included, the TCP protocol will be used.

FROM centos:latest
EXPOSE 8080

 

Or, the TCP protocol can be included.

FROM centos:latest
EXPOSE 8080/tcp

 

Or UDP.

FROM centos:latest
EXPOSE 8080/udp

 

And here is how you would expose multiple ports.

FROM centos:latest
EXPOSE 8080/tcp 8080/udp 8081

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter e0c81 in the box below so that we can be sure you are a human.