DEV Community

Cover image for How to Bridge Networks in Docker Compose (`docker-compose.yml`)
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on • Edited on

How to Bridge Networks in Docker Compose (`docker-compose.yml`)

Hello, I'm Maneshwar. I'm working on FreeDevTools online currently building **one place for all dev tools, cheat codes, and TLDRs* — a free, open-source hub where developers can quickly find and use tools without any hassle of searching all over the internet.


When running multiple containers in Docker Compos
e, bridging them via a shared network allows them to talk to each other by service name. Here's how to do it right.

What’s a Bridge Network?

A bridge network is the default network driver in Docker. It allows containers to communicate with each other by name.

When you define a custom bridge network in Docker Compose, it:

  • Keeps your containers isolated
  • Lets you define aliases and connect external services
  • Avoids using localhost, which doesn’t work across containers

Setup: A Minimal Example

Let’s say you have a backend (Node.js) and a database (PostgreSQL) and you want them to communicate.

1. docker-compose.yml

version: '3.9'

services:
  db:
    image: postgres:16
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
      POSTGRES_DB: mydb
    networks:
      - backend

  app:
    build: .
    environment:
      DB_HOST: db
    depends_on:
      - db
    networks:
      - backend

networks:
  backend:
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

Here’s what’s happening:

  • db and app are both connected to the backend bridge network.
  • app can access the database at db:5432.

Connect to External Networks

Want to connect your service to an external Docker network (maybe created outside Compose)? Do this:

networks:
  backend:
    external: true
    name: my-shared-network
Enter fullscreen mode Exit fullscreen mode

Make sure the external network exists:

docker network create --driver bridge my-shared-network
Enter fullscreen mode Exit fullscreen mode

Use Multiple Networks

You can attach services to more than one network:

services:
  app:
    image: my-app
    networks:
      - backend
      - logging
Enter fullscreen mode Exit fullscreen mode

Then define:

networks:
  backend:
    driver: bridge
  logging:
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

Now app can talk to services in both networks.

Useful Commands

  • List networks: docker network ls
  • Inspect a network: docker network inspect <network-name>
  • Remove a network: docker network rm <network-name>

Testing Connectivity

Use Alpine to test:

docker run -it --network=<network-name> alpine sh
apk add curl
curl http://<service-name>:<port>
Enter fullscreen mode Exit fullscreen mode

Gotchas

  • Don’t use localhost to connect to other containers. Use the service name.
  • If two containers aren’t on the same network, they can’t talk.
  • Compose automatically creates a default network if you don’t define one — but it’s best to be explicit.

Conclusion

Docker Compose + custom bridge networks = clean, isolated, and connected environments.

Define your networks clearly in docker-compose.yml, and your services will behave nicely in any environment.


FreeDevTools

I’ve been building FreeDevTools.

A collection of UI/UX-focused tools crafted to simplify workflows, save time, and reduce friction in searching tools/materials.

Any feedback or contributors are welcome!

It’s online, open-source, and ready for anyone to use.

👉 Check it out: FreeDevTools
⭐ Star it on GitHub: freedevtools

Let’s make it even better together.

Top comments (2)

Collapse
 
dotallio profile image
Dotallio

Super practical guide! The reminder to avoid localhost and use service names is something I've messed up more times than I want to admit. Do you have any favorite tricks for debugging network issues between containers?

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

This is extremely impressive. I always end up referencing guides like this when my containers refuse to talk to each other