Docker EXPOSE Ports

TAGS: DOCKER

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.

EXPOSE 8000

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.

Subscribe to my newsletter!
You'll get notified via e-mail when new articles are published. I mostly write about Docker, Kubernetes, automation and building stuff on the web. Sometimes other topics sneak in as well.

Your e-mail address will be used to send out summary emails about new articles, at most weekly. You can unsubscribe from the newsletter at any time.

Für den Versand unserer Newsletter nutzen wir rapidmail. Mit Ihrer Anmeldung stimmen Sie zu, dass die eingegebenen Daten an rapidmail übermittelt werden. Beachten Sie bitte auch die AGB und Datenschutzbestimmungen .

vsupalov.com

© 2024 vsupalov.com. All rights reserved.