Sponsored by

Containers need to talk to each other sometimes.

Example: a web app connecting to a database.

Since they are in separate containers, the communication isn’t straightforward.

How do you connect to a local database in your computer usually?

You use a connection string.

Probably something like this:

postgresql://localhost:5432/appdb

This works when both web app and database run on your computer.

But it doesn't work when they are in separate containers.

Why?

It's because of localhost.

What does localhost mean to a container?

If you query localhost in the web app container, it's basically querying its own port 27017.

Here's the problem: if you can't use localhost, how would you make those two containers talk to each other?

The answer is Docker network.

Default bridge network

By default, when you run a docker container without network settings, it will belong in the default bridge network.

docker run --name database postgres \
&& docker run --name proxy redis \
&& docker run --name web backend

The cool thing is all containers in the bridge network can talk to each other through IP addresses.

So, the web app can connect to the database like this:

postgresql://172.17.0.3:5432/appdb

But it's not ideal.

Why?

Any time you restart the container, the IP address could change. That's why usually you use another type of bridge network.

Custom bridge network

Connecting with an IP address isn't a good idea. What would be a better approach?

It would be nice if we can just connect to the container through its name:

postgresql://database:5432/appdb

The way to do that is actually pretty simple.

  1. create a custom bridge network

  2. put both containers inside that network

Docker will embed a custom DNS server.

What it means is that your container can talk to one another through their names instead of IP addresses.

And only containers inside the custom bridge have access to each other.

To recap:

  • Using localhost for inter-container communication doesn’t work

  • Containers all belong to a isolated default bridge network by default

  • Always create a custom bridge network for multi-container setups

  • Only put containers that should have access to one another inside a network

If you’re serious about adding Docker to your toolbox, try our gamified Docker course.

The course material are exactly like what you are reading right now, with interactive exercises.

Fee from Anime Coders

The World's Biggest Dev Event Hits Silicon Valley

WeAreDevelopers World Congress comes to San José, CA — September 23–25, 2026. 10,000+ developers, 500+ speakers, and the full software development lifecycle under one roof, in the heart of Silicon Valley.

Kelsey Hightower. Thomas Dohmke (fmr. CEO, GitHub). Christine Yen (CEO, Honeycomb). Mathias Biilmann (CEO, Netlify). Olivier Pomel (CEO, Datadog). The people actually building the tools you use every day — all on one stage.

AI, cloud, DevOps, security, architecture, and everything real builders ship with. Workshops, masterclasses, and the official congress party.

Keep Reading