Sub topic 4
Publishing UDP and TCP ports
This chapter assumes you can run docker run, know what a container port and a host port are, and have seen -p HOST:CONTAINER before. Here you will learn how the protocol suffix works and what goes wrong when you forget it.
TCP is the default
When you write -p 8080:80, Docker publishes TCP port 80. It does not publish UDP. Many services use only TCP, such as HTTP, PostgreSQL and SSH, so the default usually works.
Some services use UDP instead. DNS queries, many game servers, syslog and some streaming protocols are common examples. For these, you must say so explicitly.
Adding a protocol suffix
Append /tcp or /udp to the container port:
docker run -d --name dns-demo -p 5353:53/udp alpine sleep 300This publishes UDP port 53 in the container on UDP port 5353 on the host. The alpine sleep 300 command only keeps a container running for five minutes. It does not run a DNS server, so nothing will answer on that port. The point of the example is the mapping, which you can inspect:
docker port dns-demoThe output looks like 53/udp -> 0.0.0.0:5353. The /udp in that line confirms the protocol.
The suffix attaches to the container port. The host side takes the same protocol. You cannot map a container's UDP port to a host TCP port.
Publishing both protocols
Some services listen on the same port number over both TCP and UDP. DNS is the standard case. To publish both, repeat the -p flag:
docker run -d --name dns-both \
-p 5354:53/tcp \
-p 5354:53/udp \
alpine sleep 300Docker treats these as two separate mappings. TCP port 5354 and UDP port 5354 on the host do not conflict, because the protocol is part of the address.
You can also combine the suffix with a host IP, in the form IP:HOST:CONTAINER/proto:
docker run -d --name dns-local -p 127.0.0.1:5355:53/udp alpine sleep 300How the protocols relate
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| A UDP client gets no reply | You wrote `-p 5353:53` and Docker published TCP only | Use `-p 5353:53/udp` |
| `docker port` shows only `/tcp` | The `/udp` mapping was never requested | Add a second `-p` with `/udp` |
| `address already in use` | Another process holds that port with the same protocol | Choose a different host port or stop the other process |
| TCP test tools fail against a UDP service | TCP checks such as `curl` cannot talk to a UDP port | Use a UDP-aware client such as `nc -u` |
Checking your mappings
Use docker port to list what a container has published. You can also filter by one container port:
docker port dns-both 53/udpIf the protocol is missing from the output, the mapping does not exist, and no amount of debugging the application will help until you add it.
Clean up
Remove the demo containers when you are done:
docker rm -f dns-demo dns-both dns-localExercise
A container runs a syslog receiver on UDP port 514. You start it with -p 5514:514. Clients on the network send log messages to the host's port 5514, but the container receives nothing. What is wrong, and what is the corrected flag?
Show answer
Without a suffix, Docker publishes TCP, so UDP traffic to host port 5514 is not forwarded. Use -p 5514:514/udp. You can confirm the mapping afterward with docker port <container> 514/udp.
Exercise
How would you publish port 53 in a container over both TCP and UDP on host port 5354, but only on the loopback interface?
Show answer
Repeat the flag with the host IP and each protocol: -p 127.0.0.1:5354:53/tcp -p 127.0.0.1:5354:53/udp.