Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
Step by step
- Same network? Containers find each other by name only on the same user-defined network (Compose makes one per project). The default
bridgenetwork has no name resolution.
docker network inspect shop_default --format '{{range .Containers}}{{.Name}} {{end}}'
shop-api-1 shop-db-1- The right name and port — between containers use the service/container name and the container port:
| From | Address |
|---|---|
| Container → container (same network) | db:5432 (container port) |
| Host → container | 127.0.0.1:15432 (the published port, -p 15432:5432) |
| Container → host | host.docker.internal (Docker Desktop) or the host's IP / --add-host host.docker.internal:host-gateway |
localhost inside a container means that container, not the host and not other containers.
- Test from inside with a tool container in the same network namespace:
docker run --rm -it --network container:api nicolaka/netshoot:v0.13
/ # nslookup db
/ # nc -zv db 5432
/ # curl -sI http://web:80- Does the target listen on all interfaces?
docker run --rm --network container:db nicolaka/netshoot:v0.13 ss -ltn127.0.0.1:5432 = only inside the container itself → configure the app to listen on 0.0.0.0.
- Connect a container to another network (for example a shared proxy network):
docker network connect proxy shop-api-1- Packets — when the connection starts but fails later, capture inside the namespace:
docker run --rm --network container:api nicolaka/netshoot:v0.13 tcpdump -nn -i any port 5432 -c 20Check that it worked
nc -zv db 5432 from the api's namespace says open/succeeded, nslookup db returns the container IP, and the app's own log shows the connection.
Common problems
| Problem | Cause | Fix |
|---|---|---|
Name or service not known | Different networks, or the default bridge network. | Put both in one user-defined network (Compose does this). |
Connection refused | Wrong port, or the target listens on 127.0.0.1. | Use the container port; make the app listen on 0.0.0.0. |
App uses localhost:5432 | localhost is the container itself. | Use the service name db:5432. |
| Works from the host, not from containers | Using the published port/host IP inside the network. | Use name:containerport between containers. |
| Timeouts to the internet from containers | Host firewall/forwarding, or DNS. | Check ip_forward, firewall rules, /etc/resolv.conf inside the container. |
| Container can not reach a service on the host | Wrong address for the host. | --add-host host.docker.internal:host-gateway and make the service listen on that interface. |
Go further
- Command pages: docker, ss, dig.
- Next how-tos: Run an app with Docker Compose, Test if a remote port is open, Capture network traffic with tcpdump.
- Learn it: Docker — Network drivers, Docker — User-defined networks, Docker — Ports, Docker — Network debugging.
1Inside a container, what does localhost mean?
The container itself — not the host and not other containers.
2Which address does the api container use to reach the db container in the same Compose project?
The service name and the container port, for example db:5432.