Docker EXPOSE Ports

What does it mean to EXPOSE a port in your Dockerfile? What is it good for, and why doesn’t it make your container available?

What EXPOSE Does

Writing EXPOSE in your Dockerfile, is merely a hint that a certain port is useful. Docker won’t do anything with that information by itself.


Defining a port as “exposed” doesn’t publish the port by itself.

Publishing Ports

Docker doesn’t publish exposed ports by itself. You have to tell it what to do explicitly.

If you provide the -P (NOTE: the letter is upper-case) option when running your container, it will bind each exposed port to a random ports of the host.

You can use -p options to publish single ports. For example, if you want the container port 5000 to be available on your localhost 8000, you’d need to specify

$ docker run -p localhost:8000:5000 # and so on

This will work even if your Dockerfile does not mention anything about EXPOSE 5000.

How To Do Better

I hope this has helped you to understand what EXPOSE does, and how to publish your container’s ports when running it.

Configuring Docker containers from the command line can be a pain. If things get more complex, you can forget important options easily, or it gets very verbose very fast.

even better: consider using docker-compose for your container configurations. Writing docker-compose.yml files is more convenient and less error-prone for more complex usecases than using the Docker CLI.