Skip to content

Networking

First PublishedByAtif Alam

Docker provides networks so containers can talk to each other and to the host. By default you get a bridge network; you can create user-defined networks for better isolation and built-in DNS.

When you install Docker, three networks exist:

NetworkDriverBehavior
bridgebridgeDefault for docker run. Containers get a private IP; port publishing (-p) maps host ports to container ports.
hosthostContainer shares the host network stack. No isolation; no -p needed.
nonenullNo networking. Container has no external access.
Terminal window
# Use default bridge (implicit)
docker run -d nginx:alpine
# Use host network (Linux: container sees host's ports directly)
docker run -d --network host nginx:alpine
# Isolate: no network
docker run -d --network none alpine:3.19 sleep 999

Create a network and attach containers. Containers on the same user-defined network can reach each other by container name (or service name in Compose) thanks to built-in DNS.

Terminal window
# Create a network
docker network create mynet
# Run containers on it
docker run -d --name web --network mynet nginx:alpine
docker run -d --name api --network mynet myapp:1.0
# API can resolve "web" and connect to it

In Compose, all services in the same project are typically on one default network and resolve each other by service name.

The legacy --link option is deprecated. Prefer user-defined networks and DNS by name.

Terminal window
docker network ls
docker network inspect mynet
docker network rm mynet
docker network prune # Remove unused networks
  • bridge — Default; use -p to publish ports. host — Share host network. none — No network.
  • User-defined networks give container-to-container DNS by name; create with docker network create or via Compose.
  • Prefer user-defined networks over —link for multi-container communication.