Sub topic 2
Publishing a port with -p
This chapter assumes Docker is installed and docker run works for you. It also assumes you know what images and containers are and what a TCP port is. The previous chapter explained why a container's ports are closed to the host by default. This chapter shows the flag that opens one.
The HOST:CONTAINER form
The -p flag (long form --publish) creates a rule that forwards traffic from a port on the host to a port inside the container. The syntax is:
-p HOST_PORT:CONTAINER_PORTThe host port comes first and the container port second. Nginx listens on port 80 inside its container, so this command makes it reachable on host port 8080:
docker run -d --name web -p 8080:80 nginx
curl http://localhost:8080The curl command returns the nginx welcome page. A request to port 8080 on the host is forwarded to port 80 in the container.
The two numbers do not have to match. The container's port is fixed by the application, and you pick the host port. Because the host port is your choice, you can run several containers that use the same internal port.
Checking the mapping
docker ps shows active mappings in the PORTS column:
docker ps --filter name=webA line such as 0.0.0.0:8080->80/tcp means that host port 8080 on all interfaces forwards to TCP port 80 in the container. The details of the 0.0.0.0 part and the /tcp part are covered in later chapters.
Publishing more than one port
Repeat the flag once for each port:
docker run -d --name web-tls -p 8082:80 -p 8443:443 nginxEach -p creates an independent mapping. This container now answers on host ports 8082 and 8443.
Flag position matters
Put -p before the image name. Docker treats everything after the image name as the command and arguments for the container, so a late -p is not read as a Docker option.
# Correct: option comes before the image
docker run -d -p 8083:80 nginx
# Wrong: -p 8083:80 is passed to the container's command
docker run -d nginx -p 8083:80Common errors
Each host port can be used by only one process at a time. Starting a second container on a port that is already taken fails:
docker run -d --name web-dup -p 8080:80 nginxDocker reports an error like Bind for 0.0.0.0:8080 failed: port is already allocated. The same happens if a program outside Docker is already listening on that port. Choose a different host port, or stop the first container.
Two other mistakes are common:
- Swapped numbers.
-p 80:8080forwards host port 80 to container port 8080. If the application listens on 80 inside the container, nothing answers. Read the pair as "host first, container second". - Wrong container port. Docker does not check that the container port matches what the application listens on. The command succeeds even if nothing listens there, and requests then fail or reset.
Cleaning up
Remove the example containers when you finish, so their host ports are free again:
docker rm -f web web-tlsExercise
You run docker run -d --name api -p 9000:3000 myapp, where the application listens on port 3000 inside the container. What URL do you use from the host, and what happens if you start a second container with -p 9000:3000?
Show answer
Use http://localhost:9000, because the host port is the number before the colon. The second container fails to start with a "port is already allocated" error, because host port 9000 is already in use. Starting it with a different host port, such as -p 9001:3000, works.