How can I expose more than 1 port with Docker?

Better Stack Team
Updated on October 5, 2023

You can expose more than one port in a Docker container by using the -p option when starting the container. The -p option maps a port on the host machine to a port in the container. You can specify multiple port mappings by using multiple -p options.

Here's an example of how to expose two ports, port 80 and port 443:

 
docker run -d -p 80:80 -p 443:443 my-image

In this example, the -p 80:80 option maps port 80 on the host machine to port 80 in the container, and the -p 443:443 option maps port 443 on the host machine to port 443 in the container.

Note that when you expose multiple ports in a Docker container, the container must be configured to listen on those ports.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github