DEV Community

Yash Sonawane
Yash Sonawane

Posted on

๐Ÿ›ณ๏ธ Docker Series: Episode 7 โ€” Docker Networking: How Containers Talk Behind the Scenes

๐ŸŽฌ "Ever wondered how your frontend talks to your backend inside Docker? Or how multiple containers magically connect like best friends at a tech meetup? In this episode, we dive into Docker networking โ€” simply and visually."


๐ŸŒ Why Docker Networking Matters

In real-world apps, you donโ€™t just run a single container.
You often run:

  • A frontend (React, Vue, etc.)
  • A backend (Node, Django, etc.)
  • A database (MongoDB, Postgres, etc.)

These containers need to talk to each other, securely and efficiently.
Dockerโ€™s networking system makes this possible.


๐Ÿ”— Types of Docker Networks

Network Type Use Case
Bridge Default, isolated container networks
Host Uses the host machineโ€™s network directly
None No network at all
Overlay For multi-host Docker (Swarm, etc.)

For most use cases (especially Docker Compose), Bridge is what you need.


๐Ÿงช Default Bridge Network

When you run containers without specifying a network:

docker run -d --name nginx nginx
Enter fullscreen mode Exit fullscreen mode

Docker connects them to the bridge network. But by default, containers canโ€™t talk to each other by name here.

Youโ€™d have to use IPs โ€” not ideal. So letโ€™s fix that.


๐Ÿ› ๏ธ Create a Custom Bridge Network

docker network create my-network
Enter fullscreen mode Exit fullscreen mode

Now, run containers and attach them:

docker run -d --name backend --network my-network my-backend-image

docker run -d --name frontend --network my-network my-frontend-image
Enter fullscreen mode Exit fullscreen mode

โœ… Now frontend can reach backend using its container name as hostname.
Example: http://backend:5000


๐Ÿงช Real Example: Node App + MongoDB

# Create custom network
docker network create dev-net

# Run MongoDB
docker run -d \
  --name mongo \
  --network dev-net \
  mongo

# Run Node app connected to mongo
docker run -d \
  --name app \
  --network dev-net \
  my-node-app
Enter fullscreen mode Exit fullscreen mode

Inside my-node-app, you can now connect to Mongo using:

mongodb://mongo:27017
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Inspecting Networks

List networks:

docker network ls
Enter fullscreen mode Exit fullscreen mode

Inspect a network:

docker network inspect my-network
Enter fullscreen mode Exit fullscreen mode

๐Ÿšช Port Mapping (Host โ†” Container)

Want to access your app in the browser?

docker run -d -p 8080:3000 my-app
Enter fullscreen mode Exit fullscreen mode

This maps:

  • Port 3000 inside the container
  • To port 8080 on your machine

Visit: http://localhost:8080


๐Ÿง  Key Takeaways

  • Use custom bridge networks to let containers talk by name
  • Use --network when running multi-container apps
  • Inspect networks to understand connections

๐Ÿ”ฎ Up Next: Docker Compose โ€” One YAML to Rule Them All

In Episode 8, weโ€™ll cover:

  • How to run multi-container apps with one command
  • Docker Compose file structure
  • Real-world project with frontend + backend + DB

๐Ÿ’ฌ Over to You

Have you tried connecting containers via network yet?
Was the custom bridge easier than expected?

Comment below with your Docker networking wins or pain points!

โค๏ธ If this episode made container networking click for you, share it with your dev buddy or team.

๐ŸŽฌ Next: โ€œDocker Compose โ€” Build Multi-Container Apps Like a Bossโ€

Top comments (0)