Connect Containers by Name · Docker
Connect two containers without publishing a host port.
Learn with progress and quizzesFirst, understand the concepts.
User-defined bridge
Containers on the same user-defined bridge can discover one another by name. Name resolution differs from the default bridge.
Internal access and published ports
Connecting to a container port from the same network does not require -p. Publishing a port creates a separate path through the Docker host.
Separate failure causes
Check name resolution separately from whether the server responds on the expected port. Different networks or a server still starting can cause a request to fail.
Why it matters
Containers on the same user-defined bridge can resolve each other by name. localhost inside a container normally refers to itself, not another server.
Work through the reasoning
Trace internal traffic
Use inspect to check network membership. Address the server by container name and container port. A published host port is a separate entry point.
Narrow by symptom
For name resolution failures, inspect network membership and names. For connection refusal, inspect the listener and port. An HTTP error may mean the request already reached the application.
Learn by doing.
Use Docker Engine or Docker Desktop with Linux containers, Compose v2, a POSIX shell, and curl on a personal practice host. Clean up only the study-prefixed resources you create. Pulling images requires network access.
Start nginx on study-net and request it by name from BusyBox on the same network. If the first request races startup, wait briefly and retry.
docker network create study-net
docker run -d --name study-net-web --network study-net nginx:stable-alpine
docker run --rm --network study-net busybox:1.36 wget -qO- http://study-net-web:80
docker network inspect study-netHow to check your result
An HTML response demonstrates both name resolution and HTTP connectivity. Inspect the network’s Containers field and explain why no host port was needed.
Clean up practice resources
Run after checking the result. Confirm these resources belong to your practice run and contain no data you need to keep.
docker stop study-net-web
docker rm study-net-web
docker network rm study-netApply what you learned
A server publishes container port 80 as host port 8080. Which port should a client on the same bridge use?
Reveal the reasoning after trying
Use the server container’s name and port 80. Host port 8080 belongs to access through the host address. Mixing these endpoints can fail even when the server is healthy.
Ready to check your understanding?
What address normally reaches a server on the same user-defined bridge?
- The server container name and internal port
- Only the host’s public IP
- The image ID
- The Dockerfile name
Reveal the reasoning after trying
The server container name and internal port
User-defined bridges support name-based discovery. Containers on the same network connect to the server’s internal port.