Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

To expose just one port, this is what you need to do:

docker run -p <host_port>:<container_port>

To expose multiple ports, simply provide multiple -p arguments:

docker run -p <host_port1>:<container_port1> -p <host_port2>:<container_port2>
                Thanks! Found this in the docs here: docs.docker.com/userguide/dockerlinks/… where it says Note: The -p flag can be used multiple times to configure multiple ports.
– Ted M. Young
                Jul 14, 2014 at 19:32
                Is there a way to specify the ports in a config file? For example using the option --env-file ?
– Giovanni Bitliner
                Jan 21, 2015 at 21:21
                @GiovanniBitliner I'm still pretty new to this, but I'm pretty sure you would define ports in a Dockerfile with EXPOSE, then perform docker run -P (note the uppercase) which automatically exposes all ports defined with EXPOSE in the Dockerfile
– Ted Avery
                May 14, 2015 at 13:13

Step3

Then you can use the -p to map host port with the container port, as defined in above EXPOSE of Dockerfile.

docker run -p 3001:3000 -p 23:22

In case you would like to expose a range of continuous ports, you can run docker like this:

docker run -it -p 7100-7120:7100-7120/tcp 
                EXPOSE is only documentation for the ports that are published and useful for linking only. A complete list of ports can be found using -P and they will be automatically mapped to an available port on the host.
– Arun Gupta
                Oct 20, 2015 at 11:01

You can also specify the host/network port as HOST/NETWORK_PORT:CONTAINER_PORT

varnish:
    ports:
        - 81:80
        - 6081:6081
                When you specify just one number (e.g. 80, not 80:80), docker maps the specified container port to a host port from the ephemeral range.
– x-yuri
                Dec 28, 2020 at 22:16

Use this as an example:

docker create --name new_ubuntu -it -p 8080:8080 -p  15672:15672 -p 5432:5432   ubuntu:latest bash

look what you've created(and copy its CONTAINER ID xxxxx):

docker ps -a 

now write the miracle maker word(start):

docker start xxxxx

good luck

Only one point to add. you have the option to specify a range of ports to expose in the dockerfile and when running it:

on dockerfile:

EXPOSE 8888-8898

Build image:

docker build -t <image_name>:<version> -f dockerfile .

When running the image:

docker run -it -p 8888-8898:8888-8898 -v C:\x\x\x:/app <image_name>:<version>

If you are creating a container from an image and like to expose multiple ports (not publish) you can use the following command:

docker create --name `container name` --expose 7000 --expose 7001 `image name`

Now, when you start this container using the docker start command, the configured ports above will be exposed.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.