Docker port mapping: host vs container ports
Lab prerequisites
Use an approved, disposable Linux Docker Engine VM with no external network access, ordinary bridge/NAT settings, and Engine 28.0.0 or later for publication labs. Verify the local context, server version, and that all bm-v05 names used in this example are unused. Use a reviewed, preloaded busybox:1.37 image; record its digest and check that httpd, wget and nslookup are present where used. The tag is not immutable. Run commands individually, stop on unexpected failure, and clean up only resources you created. Results below are expected, not observed. Before contacting the Engine, verify the effective local endpoint including DOCKER_HOST, DOCKER_CONTEXT and CLI overrides. Recheck the approved local image ID and native platform; if the recorded digest is unavailable, report it as unknown rather than inventing one. Use owner-set resource and time limits. If prerequisites cannot be confirmed, do the paper exercise instead. The VM must have no other untrusted workloads, no external routes and no direct-routing exceptions. A loopback mapping or internal bridge alone does not provide these guarantees. Use only a patched, supported Engine release meeting the stated minimum.
A port identifies a service
An IP address identifies a network endpoint; a TCP port selects a service at that endpoint. An HTTP server inside a container can listen on TCP 8080. The host may publish a different port, such as 18080, to that service. In ordinary bridge networking these are separate network environments. The mapping reads host address:host port:container port; the application does not need to change its own port. TCP and UDP port numbers are separate; an omitted protocol in this example means TCP. A server bound only to its own container loopback is not normally reachable through bridge port publishing.
Inspect a mapping
The following fixture creates a small static HTTP server. Use host port 18080 only if it is free. The server uses 8080 inside the container; the host maps it to 18080 on IPv4 loopback. Use the VM terminal, not a remote client machine.
docker run -d --pull=never --name bm-v05-ports -p 127.0.0.1:18080:8080 busybox:1.37 sh -c 'mkdir -p /tmp/www && printf "ByteMind network lab\n" > /tmp/www/index.html && exec httpd -f -p 8080 -h /tmp/www'docker port bm-v05-ports 8080/tcpExpected mapping: 127.0.0.1:18080. After startup, a host HTTP request to port 18080 should reach the server at container port 8080. The mapping alone does not prove that the application is ready.
Request and clean up
Use host curl if installed. If startup has not finished, retry manually after checking container status and logs. Do not change the published port to a public address. Before curl, compare the actual binding to the expected mapping. Record the HTTP body and successful exit status before cleanup.
curl --noproxy "*" --max-time 5 --fail http://127.0.0.1:18080/docker stop bm-v05-portsdocker rm bm-v05-portsExpected response body: ByteMind network lab followed by a newline. Cleanup removes the disposable server, not its image. Mapping to the wrong internal port would not make a server appear there.
Manual practice
Draw the path from host port 18080 to container port 8080. Repeat the lab with another free host port (for example 18081); predict the mapping before running it.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Docker port mapping: host vs container ports — Knowledge check
Apply the concepts from this lesson.
In 127.0.0.1:18080:8080, which port must the application listen on?
The mapping is present but the server has not started. What is established?
Two services use container port 8080 on separate bridge containers. How can the host publish both on the same IPv4 address?