CodecPath

Guide

Docker Compose Bridge Networking: Fix Container Connectivity

Fix Docker Compose bridge failures by checking service-name DNS, container ports, shared networks, external network creation, and stale containers.

CodecPath editors4 min read
Rendered ComposemodelReveals final names andnetwork declarationsService-name DNSProves the caller canresolve the targetContainer portConfirms the processaccepts bridge trafficNetwork membershipExposes split or staleattachments
A shortest-path diagnosis: validate the rendered model, DNS, listening port, and shared network before recreating containers.

Why can’t one Compose container reach another?

Use the service name.

If web reports “Could not resolve host,” “Name or service not known,” or ENOTFOUND api, point it at the Compose service name rather than localhost, a container name, or an old IP address.

Compose attaches services to an automatically created project network unless the file says otherwise, and containers on that network discover one another by service name through Docker DNS. The default name is normally <project>_default; the project portion can change with the directory, -p, COMPOSE_PROJECT_NAME, or a top-level name.

For this file:

YAML
services:
  web:
    image: nginx
  api:
    image: my-api
    expose:
      - "8080"

web should call http://api:8080, not http://localhost:8080. Inside web, localhost means the web container itself. Docker’s Compose networking documentation also warns that a recreated container keeps its service name but receives a new IP address, so applications should reconnect by name instead of caching the address.

Check resolution from the caller:

Terminal
docker compose exec web getent hosts api

If that returns an address but the request still fails, DNS is working. Move to the listening port.

Which port should containers use?

Use the container port.

Container-to-container traffic crosses the bridge directly, so api:8080 is correct when the process listens on 8080 even if the Compose file publishes it as 127.0.0.1:9000:8080 for the host.

The left side of ports belongs to the host. The right side belongs to the container:

YAML
services:
  api:
    ports:
      - "127.0.0.1:9000:8080"

A browser on the host uses http://127.0.0.1:9000; another service uses http://api:8080. Publishing port 9000 does not make the application listen there inside the container.

“Connection refused” usually means the name resolved and the packet arrived, but no process accepted it. Confirm the application binds to 0.0.0.0:8080 or [::]:8080, not only 127.0.0.1:8080; a loopback-only listener cannot accept traffic arriving through the container interface. The bridge driver reference distinguishes ports exposed between containers from ports published through the Docker host.

Run the check inside the target container if its image contains ss:

Terminal
docker compose exec api ss -lntp

A timeout is different. It more often points to separated networks, a firewall rule, or a process stuck before accepting connections.

When do network declarations break connectivity?

Shared membership is mandatory.

If two healthy services sit on different custom networks, add one common network to both services or attach a deliberate proxy between them; merely declaring both networks does not bridge them together.

This configuration isolates frontend from db:

YAML
services:
  frontend:
    networks: [public]
  db:
    networks: [private]

networks:
  public:
    driver: bridge
  private:
    driver: bridge

Add a shared backend network where communication is required. A bridge network driver inside a Docker Compose file is local to one Docker Engine, which is why a Compose file can be targeted at a single host but does not create multi-host routing.

External networks add another common failure. With external: true, Compose expects the named network to exist and can stop with network <name> declared as external, but could not be found; create it first with docker network create -d bridge app_shared, or remove external and let Compose manage the network. The Compose networks reference documents that Compose does not create an external network and rejects other network attributes alongside external: true.

Docker’s user-defined bridge offers more flexibility than the default engine-wide bridge, including automatic name resolution and scoped isolation. I would not attach an application stack to the legacy default bridge just to avoid writing four lines of YAML.

How do you find the failing network layer?

Inspect before rebuilding.

Start with the rendered Compose model, then compare each container’s attached network IDs and test DNS followed by the application port, because repeated rebuilds do not repair a wrong hostname or mismatched network declaration.

  1. Run docker compose config and inspect the resolved service and network names.
  2. Run docker compose ps and confirm both services are running rather than restarting.
  3. Run docker compose exec web getent hosts api from the calling container.
  4. Run docker compose exec web curl -v http://api:8080 if the image includes curl.
  5. Run docker network inspect <project>_default and verify both container IDs appear.
  6. Recreate stale attachments with docker compose up -d --force-recreate.

Use docker inspect when the friendly output hides the detail:

Terminal
docker inspect -f '{{json .NetworkSettings.Networks}}' container_name

An empty result or different network IDs explains ENETUNREACH, failed DNS, and timeouts. If Docker reports network ... not found after a network was deleted manually, remove and recreate the affected containers; a container retains a reference to the vanished network until replacement.

Do not start by pruning everything. docker system prune can remove unrelated stopped containers and unused networks while leaving the bad Compose declaration untouched, whereas the Docker CLI network commands provide targeted inspect, connect, and disconnect operations.

FAQ

Does Docker Compose create a bridge network automatically?
Yes. Unless the Compose file specifies other networks, Compose creates a project-scoped default bridge network and connects each service to it.
Why does localhost fail between containers?
Localhost points back to the current container, not another service or the Docker host. Use the target service name and its internal listening port.
Can containers in different Compose projects communicate?
Yes, if both join the same existing user-defined network. Declare that network as external in both projects and create it before starting either stack.

Related guides