Docker & Containers (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)
How does Docker port mapping (`-p` vs `-P`) work?
junior LevelMost AskedCode ExampleDocker & Containers
In Simple Words: By default, container network ports are inaccessible from outside the host machine.
-p <host_port>:<container_port>(Explicit Mapping): Maps a specific port on the host to a port inside the container:bash# Binds port 80 on host to port 3000 inside container docker run -d -p 80:3000 my-web-app-P(Publish All / Ephemeral Mapping): Automatically maps all ports exposed in the Dockerfile (EXPOSE) to random high-numbered ephemeral ports on the host (between 32768 and 60999).- Binding to specific host IP:
bash
# Binds only to host localhost interface (prevents external public internet exposure) docker run -d -p 127.0.0.1:5432:5432 postgres