Skip to content

Custom Networks and Container Isolation

Use docker network create with --driver bridge to create a named network. The bridge driver is the default, so you can omit it for brevity:

Terminal window
docker network create --driver bridge frontend-net

Attach a container to a custom network with --network at start time:

Terminal window
docker run -d --name nginx-proxy --network frontend-net nginx:alpine

Connecting a running container to an additional network

Section titled “Connecting a running container to an additional network”

A running container can be added to another network without restarting it:

Terminal window
docker network connect backend-net mycontainer

After this, mycontainer has interfaces on both frontend-net and backend-net and can communicate with containers on either network.

Terminal window
docker network disconnect frontend-net mycontainer

The container loses its interface on frontend-net but keeps any other network connections.

Terminal window
docker network inspect frontend-net

The output shows the driver, subnet, gateway, and a Containers map listing every attached container with its IP and MAC address.

Network segmentation is a zero-cost security control. Containers on different networks cannot reach each other unless explicitly connected to both.

A typical three-tier application uses three separate networks:

NetworkContainers attachedCan reach
frontend-netnginx-proxy, web-appEach other only
backend-netweb-app, api-serverEach other only
data-netapi-server, postgresEach other only

web-app is connected to both frontend-net and backend-net, so it bridges the two tiers. postgres is on data-net only — the nginx proxy cannot reach the database directly, even if compromised.

Filter docker network ls by driver to see only bridge networks:

Terminal window
docker network ls --filter driver=bridge

A network cannot be removed while containers are attached to it. Stop and remove the containers first, then remove the network:

Terminal window
docker network rm frontend-net
# Create two networks
docker network create frontend-net
docker network create backend-net

# Start a container on frontend-net only
docker run -d --name frontend --network frontend-net alpine sleep 300

# Start a container on backend-net only
docker run -d --name backend --network backend-net alpine sleep 300

# Show isolation: frontend cannot reach backend by name
docker exec frontend ping -c 1 backend || echo "Isolated: frontend cannot reach backend"

# Connect frontend to backend-net so it can reach both
docker network connect backend-net frontend

# Now frontend can reach backend
docker exec frontend ping -c 3 backend

# Inspect the networks
docker network ls --filter driver=bridge

# Cleanup
docker stop frontend backend && docker rm frontend backend
docker network rm frontend-net backend-net
Two containers are on different user-defined bridge networks. Can they communicate by default?
You have a running container named 'api' and you want to connect it to an existing network named 'data-net' without restarting it. Which command do you use?
You try to run docker network rm mynet but get an error saying the network has active endpoints. What must you do first?
In a three-tier app, you want the database container to be reachable by the API server but not by the frontend proxy. What is the correct network design?