Multiple ports
- Docker & Kubernetes course
- Beginner
- Lesson 45/82
- 4 min
Learn to map multiple ports.
Multiple ports
A container can publish several ports; a separate -p is written for each. Ranges are supported too:
docker run -d --name web -p 80:80 -p 443:443 nginx
docker run -d -p 7000-7005:7000-7005 kume-dugumuIn services like RabbitMQ one port is for application traffic and the other for the management UI: -p 5672:5672 -p 15672:15672.
Looking at all ports
docker port can also query a single container port. docker inspect gives the mappings as JSON:
$ docker port web 443
0.0.0.0:443
$ docker inspect -f '{{json .NetworkSettings.Ports}}' web
{"443/tcp":[{"HostIp":"0.0.0.0","HostPort":"443"}],"80/tcp":[{"HostIp":"0.0.0.0","HostPort":"80"}]}Practice RabbitMQ
Start a container named mq from the rabbitmq:3-management image in the background; publish ports 5672 and 15672 on the host with the same numbers.
Starter code
# buraya yazExpected output
e5f6a7b8c9d0
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is done to publish HTTP and HTTPS from the same container?
- Two separate -p flags are written Correct answer
- -p 80,443 is written
- Two containers are opened
- It isn't possible
Explanation: Each mapping is a separate -p.
2 Publish a port range.
docker run -d -p 7000____:7000-7005 kume-dugumu
docker run -d -p 7000-7005:7000-7005 kume-dugumu
Explanation: A range is written with a dash and must be the same length on both sides.
3 Match.
| Term | Meaning |
|---|---|
-p 80:80 -p 443:443 |
Two separate mappings |
7000-7005 |
Port range |
docker port web 443 |
Single port query |
15672 |
Management UI |