Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 145Containers · Docker

How to debug the network between containers

"The api container can not reach the database container" — usually it is one of four things: they are not in the same network, the app uses localhost, the wrong name or port, or the target listens only on 127.0.0.1. This how-to checks networks, names and ports step by step, tests from inside with a tool container, and explains when to use the container port vs the published port.

  • 5 Lessen
  • 4 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

145.1

The quick answer

docker network ls
docker inspect api --format '{{range $n, $c := .NetworkSettings.Networks}}{{$n}} {{$c.IPAddress}}{{"\n"}}{{end}}'
docker run --rm --network container:api nicolaka/netshoot:v0.13 nc -zv db 5432

The last line tests from the api container's network: can it reach db on port 5432?

145.2

Step by step

  1. Same network? Containers find each other by name only on the same user-defined network (Compose makes one per project). The default bridge network has no name resolution.
docker network inspect shop_default --format '{{range .Containers}}{{.Name}} {{end}}'
shop-api-1 shop-db-1
  1. The right name and port — between containers use the service/container name and the container port:
FromAddress
Container → container (same network)db:5432 (container port)
Host → container127.0.0.1:15432 (the published port, -p 15432:5432)
Container → hosthost.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.

  1. 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
  1. Does the target listen on all interfaces?
docker run --rm --network container:db nicolaka/netshoot:v0.13 ss -ltn

127.0.0.1:5432 = only inside the container itself → configure the app to listen on 0.0.0.0.

  1. Connect a container to another network (for example a shared proxy network):
docker network connect proxy shop-api-1
  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 20

145.3

Check 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.

145.4

Common problems

ProblemCauseFix
Name or service not knownDifferent networks, or the default bridge network.Put both in one user-defined network (Compose does this).
Connection refusedWrong 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:5432localhost is the container itself.Use the service name db:5432.
Works from the host, not from containersUsing the published port/host IP inside the network.Use name:containerport between containers.
Timeouts to the internet from containersHost firewall/forwarding, or DNS.Check ip_forward, firewall rules, /etc/resolv.conf inside the container.
Container can not reach a service on the hostWrong address for the host.--add-host host.docker.internal:host-gateway and make the service listen on that interface.

145.5

Go further

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.

Alle commando's in dit hoofdstuk