How Docker Containers on Separate Networks Can Communicate

Last updated on August 21st, 2026 at 01:03 pm

Main lesson: two Docker containers can be healthy and still be unable to reach each other when they are attached to different bridge networks. The reliable fix is to place both services on a shared network and use Docker’s service-name DNS.

What I learned

A port published to 127.0.0.1 makes a service available from the host. It does not automatically make that service discoverable from another container.

On Linux, host.docker.internal may also be unavailable unless it is configured explicitly. For container-to-container traffic, a shared Docker network is usually simpler.

Diagnose the connection

  1. Inspect the networks for both containers:
    docker inspect app_service --format '{{json .NetworkSettings.Networks}}'
    docker inspect search_service --format '{{json .NetworkSettings.Networks}}'
  2. If they do not share a network, connect the second service to the application network:
    docker network connect --alias search_service shared_network search_service
  3. Test from inside the application container:
    docker exec app_service curl http://search_service:7700/health
  4. Configure the application to use the container DNS name:
    SEARCH_HOST=http://search_service:7700

Make the fix permanent

A manual docker network connect can be lost when a container is recreated. Declare the shared network in Compose instead:

services:
  app_service:
    networks:
      - shared

  search_service:
    networks:
      - shared

networks:
  shared:

If the services are managed by separate Compose projects, define the same external network in both files.

Common mistakes

  • Cause: using localhost inside the application container. Fix: use the destination service name.
  • Cause: relying on a host-only port mapping. Fix: attach both containers to a shared network.
  • Cause: making only a temporary network change. Fix: declare the network in Compose.
  • Cause: changing an environment value while configuration is cached. Fix: reload or clear the application’s configuration cache.

Conclusion

When one container cannot reach another, inspect their networks before debugging the application. The next action is simple: confirm both services share a network, then test the destination by its service name.

Tags:

Leave a Comment

Discover more from Juzhax Technology

Subscribe now to keep reading and get access to the full archive.

Continue reading