SteelReed

Search

Search posts, questions…

DevOps & CloudQuestion 13 / 35

Junior

What is port mapping in Docker? How do -p, EXPOSE and -P differ?

Short answer

Port mapping -p 8080:80 forwards traffic on host port 8080 to container port 80; the order is always host:container. EXPOSE in a Dockerfile is only documentation; it opens nothing.

host-ip:8080-p 8080:80calls port 80 directlyClient outside the hostHost, port 8080Container, port 80App must listen on 0.0.0.0Container on same network

3 points to say:

  1. -p host:container, binding all host IPs by default; -P publishes EXPOSEd ports to random host ports.
  2. Published but unreachable: usually the app listens on 127.0.0.1 instead of 0.0.0.0 inside the container.
  3. Don't publish a DB on all IPs (use 127.0.0.1:5432:5432): ufw does not block Docker-published ports.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.