Skip to content
Log In Sign Up info@muvcloud.com

The -p Option: Host Port to Container Port

docker run -d --name web -p 8080:80 nginx:1.30

The left number is the port on the server, the right one the port inside the container. Connections to port 8080 on any address of the server go to port 80 in the container, so http://203.0.113.10:8080 shows the Nginx page. The program in the container must listen on all of its addresses (0.0.0.0), not on the container's own loopback, or the forwarded connection finds nothing.

Common Forms

OptionMeaning
-p 8080:80All host addresses, TCP
-p 127.0.0.1:8080:80Loopback only
-p 53:53/udpUDP
-p 8000-8010:8000-8010A range
-p 80Random host port
-PEvery exposed port, random host ports

Repeat -p for each port. Without a suffix the mapping is TCP; a service that needs both protocols gets two options, -p 53:53/tcp -p 53:53/udp.

Bind to 127.0.0.1 Behind a Reverse Proxy

An application that Nginx on the same server passes requests to should not be reachable from outside. Put the loopback address in front:

docker run -d --name app -p 127.0.0.1:3000:3000 myapp:1.0

Nginx then uses proxy_pass http://127.0.0.1:3000;, as in Nginx reverse proxy. Why that address is invisible from the internet is explained in what is localhost.

-P, EXPOSE, and docker port

EXPOSE 80 in a Dockerfile only documents which port the program uses; it publishes nothing. -P publishes every exposed port to a random high port on the host. Find out which one:

docker run -d -P --name web2 nginx:1.30
docker port web2
docker ps --format '{{.Names}}  {{.Ports}}'

docker port prints one line per mapping and host address, such as 80/tcp -> 0.0.0.0:32768, often followed by a second line for the IPv6 address [::]; the number differs each time. sudo ss -tlnp lists what listens on the host.

Why ufw Does Not Block Published Ports

ufw and firewalld filter traffic addressed to the server itself. Docker publishes a port differently: its own firewall rules rewrite the destination of incoming packets to the container's internal address and route them on. Docker's documentation puts it this way: the packets are routed before ufw's rules can be applied, and with firewalld Docker adds its own zone that accepts container traffic. So with ufw set to deny incoming traffic, -p 3306:3306 still opens the database to the internet. Test from another machine, for example with nc, rather than trusting ufw status.

How to Keep a Port Private

  • Do not publish internal services: a database used only by other containers needs no -p. Put them on the same user-defined network and they reach each other by name.
  • Bind to loopback: 127.0.0.1: in front keeps a port on the server.
  • Filter in DOCKER-USER: Docker checks this chain before its own rules and leaves its contents alone.
  • Keep Docker's iptables handling on: turning it off breaks container networking and is not the fix.

Docker's documentation gives this form to let only one address, here 203.0.113.10, reach the published ports through the external interface eth0, plus an accept rule for replies:

sudo iptables -I DOCKER-USER -i eth0 ! -s 203.0.113.10 -j DROP
sudo iptables -I DOCKER-USER -p tcp -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

-I inserts at the top, so the second command puts the accept rule above the drop rule, where Docker says it must be: replies to connections the containers opened themselves come from addresses the drop rule would block. Replace eth0 with your interface from ip -br addr; see Linux network commands. Keep a second SSH session open while you test. Rules added with iptables this way are not kept across a reboot; Docker's documentation does not cover saving them, so use your distribution's tool for that.

Ports in Docker Compose

services:
  app:
    image: myapp:1.0
    ports:
      - "127.0.0.1:3000:3000"
  db:
    image: mysql:9.7
    env_file: db.env

Quote port mappings in YAML. The db service reads its root password from db.env and publishes nothing, and app reaches it as db:3306.

Port Is Already Allocated

Docker stops with this message, or with address already in use, when another container or program holds the host port. Find it with docker ps and sudo ss -tlnp | grep :8080, or with lsof, and change the left side of the mapping.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations