Sitemap
Rakesh Bhetariya

Rakesh Bhetariya

Full Stack Developer, Portfolio : https://www.rakeshbhetariya.com/

Docker Networking How Containers Talk to Each Other

3 min readApr 2, 2026

--

This post is part of my Docker Basics Series, where I explain Docker step-by-step in a simple and practical way.

So far, you’ve learned:

  • How to run containers
  • How images and Dockerfiles work
  • How to manage multiple containers using Docker Compose
  • How to persist data using Docker Volumes

Now comes one of the most important (and slightly confusing) parts:

How do containers actually talk to each other?

Press enter or click to view image in full size

The Problem Most Developers Face

Let’s imagine a real-world setup:

  • A Node.js API running in one container
  • A MongoDB database running in another

Now the obvious question is:

“How does my API connect to the database?”

You might think:

“Just use localhost”

But that won’t work

Why?

Because:

Each container runs in its own isolated environment

Which means:

  • localhost inside one container ≠ your machine
  • localhost inside one container ≠ another container

The Solution Docker Networking

Docker solves this problem using its built-in networking system.

And here’s the most important concept:

Docker’s Secret

Instead of using IP addresses (which change),

You use container names (service names)

Example:

172.17.0.3:27017 (unstable, changes)
mongo:27017 (stable, readable, reliable)

Docker automatically resolves this using its internal DNS.

Simple Understanding

Without Docker Networking

  • No communication between containers
  • IP-based connection (unstable)
  • Manual setup required
  • Breaks on restart

With Docker Networking

  • Containers talk easily
  • Service name-based connection
  • Automatic setup
  • Stable and reliable

Types of Docker Networks (Keep It Simple)

You don’t need to master everything just understand these:

1️. Bridge Network (Default Most Important)

  • Default network created by Docker
  • Containers can communicate with each other
  • Works best for most use cases

You’ll use this 90% of the time

2️. Host Network

  • Uses your machine’s network directly
  • No isolation
  • Faster but less secure

3️. None Network

  • No internet or communication
  • Fully isolated container

Practical Example (Step-by-Step)

Let’s connect a Node.js app with MongoDB.

Step 1: Create Network

docker network create my_network

Step 2: Run MongoDB

docker run -d \
--name mongo \
--network my_network \
mongo:6

Step 3: Run App

docker run -d \
--name app \
--network my_network \
node-app

Step 4: Connect in Code

mongodb://mongo:27017/mydb

Key Insight

Not localhost
Not IP
Just mongo (container name)

Why This Works

Docker has a built-in internal DNS system.

Get Rakesh Bhetariya’s stories in your inbox

Join Medium for free to get updates from this writer.

So when you write:

mongo

Docker automatically finds the correct container.

EXPOSE vs PORT (-p) Important Difference

This confuses almost everyone.

EXPOSE (Dockerfile)

EXPOSE 3000
  • Just documentation
  • Not accessible from browser

Publish Port

docker run -p 3000:3000 app
  • Makes app accessible
  • Connects container → local machine

Simple Rule

  • EXPOSE = information
  • -p = actual access

Docker Networking with Compose (Best Way)

If you’re using Docker Compose (from previous blog ), everything becomes easier.

Example:

version: "3"
services:
api:
build: .
ports:
- "3000:3000"
depends_on:
- mongo
mongo:
image: mongo:6

Inside your app:

mongodb://mongo:27017/mydb

Why Compose is Powerful

  • No manual network creation
  • No --network flags
  • Everything auto-connected

Just run:

docker compose up

Real-World Architecture

In real apps:

  • API → DB
  • API → Redis
  • Workers → Queue

All connected using Docker networking.

Without networking:

Broken systems
Manual IP headaches

With networking:

Clean system
Easy scaling
Reliable communication

Best Practices

  • Use service names, not IP
  • Use Docker Compose
  • Avoid hardcoding configs
  • Use custom networks
  • Keep services loosely coupled

What You Learned

You now understand:

  • Why localhost doesn’t work
  • How Docker networking works
  • Types of networks
  • How containers connect
  • EXPOSE vs -p
  • Compose networking

This is real-world Docker knowledge

What’s Next?

Now you know:

Containers
Data (Volumes)
Communication (Networking)

Next step:

Dockerizing a Real Node.js Application

This is where everything comes together 🔥

Written by Rakesh Bhetariya
Full-Stack Developer | Docker Basics Series 🚀

--

--