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.
3 points to say:
-p host:container, binding all host IPs by default;-PpublishesEXPOSEd ports to random host ports.- Published but unreachable: usually the app listens on
127.0.0.1instead of0.0.0.0inside the container. - 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.