Networking

Learn Docker networking concepts including bridge networks, port publishing, service discovery, DNS, and common connectivity issues.

Last reviewed: June 2026

·Docker Engine 24+Docker Desktop 4+Docker Compose v2
✅
Quick Understanding: Docker networking controls how containers communicate with each other, the host, and the outside world. Containers on the same user-defined bridge network can usually reach each other by service name.

🔍 What Is It?

Docker networking provides isolated networks for containers.

Common concepts:

  • →Bridge network - default local container network
  • →User-defined bridge - better local service discovery
  • →Port publishing - maps host ports to container ports
  • →DNS - containers can resolve names on user-defined networks
  • →Host network - container shares host networking, used selectively

🎯 Why Do We Need It?

Applications rarely run alone. APIs call databases, frontends call APIs, workers call queues, and monitoring tools scrape metrics.

Docker networking lets those services communicate without hardcoding host-specific IP addresses.

🏗️ What Happens in Real Projects?

In local development, Compose creates a project network automatically. Services can call each other by service name:

  • →api connects to postgres:5432
  • →frontend connects to api:3000
  • →Only frontend or API ports are published to the host

In production, container networking is usually handled by an orchestrator or cloud platform.

⚙️ How It Works

A container has internal ports. Publishing maps a host port to a container port:

-p 8080:80 means host port 8080 forwards to container port 80.

Containers on the same user-defined network can communicate directly using container or service names. Published ports are mainly for host-to-container or external access.

💻 Example

API and Database Network — A user-defined network lets the API reach Postgres by container name.

bash
docker network create app-net

docker run -d --name db --network app-net \
  -e POSTGRES_PASSWORD=secret postgres:16

docker run -d --name api --network app-net \
  -e DATABASE_URL=postgres://postgres:secret@db:5432/postgres \
  -p 3000:3000 orders-api:1.0.0

🤔 Common Confusions

❓ Why cannot my host reach the container port?

The port may not be published. Container ports are internal unless mapped with `-p` or Compose `ports`.

❓ Should containers use `localhost` to reach each other?

No. Inside a container, `localhost` usually means that same container. Use service names on a shared network.

❓ What is the difference between `ports` and `expose` in Compose?

`ports` publishes to the host. `expose` documents or exposes internally to linked services without host publishing.

🚨 Common Production Issues

**Port collisions** - multiple containers try to publish the same host port.

**Localhost mistakes** - services inside containers try to call dependencies on `localhost`.

**Network overlap** - Docker subnet ranges conflict with VPN or cloud network ranges.

**Overexposure** - internal services publish ports unnecessarily to the host or internet.

✅ Best Practices

Use user-defined networks for multi-container local apps

Communicate between containers by service name

Publish only ports that external clients need

Avoid hardcoded container IP addresses

Document required ports and network assumptions

Plan Docker subnet ranges in corporate VPN environments

🏛️ Architect's Note

Senior Architect's Perspective

Most Docker networking bugs are naming bugs. Developers forget which perspective they are in: host, container, another container, or external client.

Debug by asking: where does this request originate, what name is it resolving, and which port is visible from that network boundary?

💬 Frequently Asked Questions

Can containers on different networks talk?

Not by default. A container must share a network or traffic must be routed through published ports or another configured path.

What does `-p 8080:80` mean?

Requests to host port 8080 are forwarded to port 80 inside the container.

Does Docker Compose create a network?

Yes. Compose creates a default project network unless configured otherwise.

🎯 Key Takeaways

1Docker networks isolate and connect containers
2Published ports expose containers to the host
3Containers should use service names, not hardcoded IPs
4`localhost` inside a container means that container
5Only publish what must be externally reachable

🎯 Practice this topic in interviews

↗ Where this topic fits

Sources

Sources / References

Use these Docker references to verify the concept in official docs and continue into the most relevant SplashRide pages.