Creating Docker Networks for Container Communication

When containers run on the same Docker host, they need a way to find and talk to each other. Docker networks provide exactly that — an isolated communication layer where containers can reach each other by name instead of by IP address. Without a custom network, containers on the default bridge can only communicate by IP, which breaks when containers restart and get new IPs.

Why Use Custom Docker Networks?

Step 1: Create a Docker Network

docker network create app-network

This creates a bridge network (the default driver). You can verify it exists:

docker network ls

You will see your new network listed alongside the default networks (bridge, host, none).

Step 2: Run Containers on the Network

Start two containers on the same network. Here is a practical example with a Node.js app and a PostgreSQL database:

# Start a PostgreSQL database on the app-network
docker run -d --name app-db --network app-network \
  -e POSTGRES_PASSWORD=secret \
  postgres:16

# Start a Node.js app on the same network
docker run -d --name app-server --network app-network \
  -p 3000:3000 \
  my-node-app:latest

Both containers are now on app-network. The Node.js app can connect to the database using app-db as the hostname — no IP address needed.

Step 3: Test Connectivity

Exec into one container and ping the other by name:

docker exec -it app-server /bin/bash
ping app-db

If the ping succeeds, DNS resolution is working. Your application can now use app-db as the database host in its connection string.

Step 4: Inspect a Network

To see which containers are connected to a network and their assigned IPs:

docker network inspect app-network

This outputs the network configuration including the subnet, gateway, and all connected containers with their IP addresses.

Step 5: Connect and Disconnect Running Containers

You can add a running container to a network without restarting it:

# Connect a running container to a new network
docker network connect app-network some-existing-container

# Disconnect a container from a network
docker network disconnect app-network some-existing-container

This is useful when you need to temporarily give a container access to another network for debugging.

Network Drivers

DriverUse Case
bridgeDefault. Containers on the same host communicate over an internal network.
hostContainer shares the host's network stack. No isolation, but no port mapping overhead.
overlayMulti-host networking for Docker Swarm or Kubernetes. Containers on different hosts communicate.
noneNo networking. The container is completely isolated.

Common Mistakes

Practical Example: App + Database + Cache

# Create the network
docker network create backend

# Start PostgreSQL
docker run -d --name db --network backend \
  -e POSTGRES_PASSWORD=secret postgres:16

# Start Redis
docker run -d --name cache --network backend redis:7

# Start the app — connects to both db and cache by name
docker run -d --name app --network backend \
  -p 3000:3000 my-app:latest

Your application code can now connect to the database at db:5432 and the cache at cache:6379.

A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles