So I have 3 ports that should be exposed to the machine's interface. Is it possible to do this with a Docker container?
7 Answers
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>

- 83,387
- 30
- 160
- 202

- 10,976
- 1
- 18
- 12
-
11Thanks! Found this in the docs here: http://docs.docker.com/userguide/dockerlinks/#network-port-mapping-refresher where it says `Note: The -p flag can be used multiple times to configure multiple ports.` – Ted M. Young Jul 14 '14 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 '15 at 21:21
-
16@GiovanniBitliner I'm still pretty new to this, but I'm pretty sure you would define ports in a Dockerfile with [EXPOSE](https://docs.docker.com/reference/run/#expose-incoming-ports), then perform `docker run -P` (note the uppercase) which automatically exposes all ports defined with EXPOSE in the Dockerfile – Ted Avery May 14 '15 at 13:13
-
Multiple ports can halting the init process under a systemd service file? – Lanti Jul 01 '15 at 20:15
-
4I think the correct term here is `publish` not `expose`. – tgogos Dec 01 '17 at 14:17
-
@ChrisSeymore where do we run these commands? Do we run them inside the docker container? – pandasCat Sep 18 '18 at 20:43
-
Is it is possible to map multiple docker ports to 1 host port? – variable Jun 03 '20 at 11:40
-
I tried exposing multiple ports using multiple `-p` flags but it only exposed one port (the last argument of -p). I am using Docker Desktop with Docker Engine v20.10.5 – CᴴᴀZ Aug 25 '21 at 07:42
Step1
In your Dockerfile
, you can use the verb EXPOSE
to expose multiple ports.
e.g.
EXPOSE 3000 80 443 22
Step2
You then would like to build an new image based on above Dockerfile
.
e.g.
docker build -t foo:tag .
Step3
Then you can use the -p
to map host port with the container port, as defined in above EXPOSE
of Dockerfile
.
e.g.
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

- 20,411
- 12
- 49
- 68
-
30EXPOSE 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 '15 at 11:01
-
8Expose is not needed. Remove the first step or make it optional. – Amir Hossein Baghernezad Feb 08 '18 at 06:47
if you use docker-compose.yml
file:
services:
varnish:
ports:
- 80
- 6081
You can also specify the host/network port as HOST/NETWORK_PORT:CONTAINER_PORT
varnish:
ports:
- 81:80
- 6081:6081

- 1,623
- 3
- 18
- 24
-
2When 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 '20 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

- 1,602
- 16
- 26
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.

- 27,717
- 28
- 128
- 190

- 718
- 2
- 14
- 27
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>

- 63
- 5
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
RUN npm install -g pm2
COPY . ./
EXPOSE 3000
EXPOSE 9200
CMD npm run start
it is not working for two ports

- 1,762
- 2
- 7
- 15