Configuring Docker Networks
1. Creating Networks
| Option | Values | Description |
|---|---|---|
| docker network create | network-name |
Create a new user-defined network |
| --driver | bridge, overlay, null |
Network driver to use (default: bridge) |
| --subnet | 172.20.0.0/16 |
Subnet for the network (CIDR notation) |
| --gateway | 172.20.0.1 |
Gateway IP for the network |
| --ip-range | 172.20.1.0/24 |
IP range for the network |
| --opt | com.docker.network.bridge.name=br-abc123 |
Driver-specific options |
Example: Create a custom bridge network
docker network create --driver bridge --subnet 172.20.0.0/16 --gateway 172.20.0.1 my-network
Example: Create an overlay network for swarm mode
docker network create --driver overlay --subnet 10.0.9.0/24 my-overlay-network
Note: By default, Docker creates a
bridge network named bridge when the daemon starts. This is used for containers not explicitly attached to a network.
2. Listing Networks
| Command | Description | Options |
|---|---|---|
| docker network ls | List all networks | --format to customize output format |
| docker network ls --filter | Filter networks by criteria | driver=bridge, name=network_name |
Example: List all networks with custom format
docker network ls --format "{{.ID}}\t{{.Name}}\t{{.Driver}}"
3. Inspecting Networks
| Command | Description | Example |
|---|---|---|
| docker network ls | List all networks | docker network ls |
| docker network inspect <network> | Inspect a specific network | docker network inspect my-network |
| docker network inspect --format | Inspect with custom JSON format | docker network inspect --format '{{.Name}}' my-network |
Note: The inspect command provides detailed information about network configuration, including driver, options, and attached containers.
4. Connecting Containers to Networks
| Option | Description | Default |
|---|---|---|
| --network | Connect container to a network | default |
| --network-alias | Alias for container on a specific network | None |
Example: Connecting a Container to a Network
docker run --network my-network --network-alias web-server nginx
Note: By default, containers connect to the
bridge network if no --network is specified.
Warning: If a container is connected to multiple networks, it can communicate with containers on any of those networks.
5. Disconnecting Containers from Networks
| Option | Values | Description |
|---|---|---|
| docker network disconnect | network container |
Disconnects a running container from a network |
Example: Disconnecting a Container from a Network
docker network disconnect my-network web-container
Note: The container must be running to disconnect it from a network.
Warning: Disconnecting a container from a network may affect its ability to communicate with other services.
| Flag | Description | Usage |
|---|---|---|
| --force | Force disconnect even if the container is not connected to the network | docker network disconnect --force my-network web-container |
6. Using Bridge Networks
| Property | Description | Default | Notes |
|---|---|---|---|
| docker0 | Default bridge network interface | Yes | Created automatically by Docker daemon |
| bridge | Network driver for bridge networks | No | Used to connect containers to each other or host |
| --network | Flag to specify network for container | default | Can be set to custom bridge or host network |
Example: Running Container on Custom Bridge Network
docker run --network my-bridge-network --name web-server nginx
Note: Bridge networks are isolated by default and do not allow direct communication between containers on different bridge networks.
7. Using Host Network
| Option | Values | Notes |
|---|---|---|
| --network=host | host |
Use the host's network stack inside the container |
Note: The
host network mode shares the host's network namespace, which can lead to port conflicts and reduced security isolation.
Warning: Using
--network=host disables the container's network isolation and can cause issues with port binding.
8. Using None Network
| Option | Values | Notes |
|---|---|---|
| --network none | none |
Disables networking for the container |
Example: Run container with no network
docker run --network none alpine sh -c "echo 'No network access'"
Note: Containers with
--network none cannot communicate with other containers or the host network.
9. Configuring Network Aliases
| Property | Description | Usage |
|---|---|---|
| --network-alias | Assigns an alias to a container within a specific network | docker run --network-alias=web-server --network=my-network nginx |
| --network | Specifies the network to connect to | docker run --network=my-network --network-alias=app-server alpine sh |
Example: Using Network Aliases in Docker Compose
version: '3.8'
services:
web:
image: nginx
networks:
app-network:
aliases:
- frontend
api:
image: node:18
networks:
app-network:
aliases:
- backend
networks:
app-network:
Note: Aliases are only valid within the scope of a specific network and do not affect DNS resolution outside that network.
10. Setting Up DNS Configuration
| Option | Values | Description |
|---|---|---|
| --add-host | host:ip |
Add a custom host-to-IP mapping |
| --dns | ip1,ip2 |
Set custom DNS servers |
| --dns-search | domain1,domain2 |
Set DNS search domains |
| --dns-opt | option1,option2 |
Set DNS options (e.g., timeout:1) |
Note: DNS configuration can be overridden at the daemon level via
/etc/docker/daemon.json.
11. Removing Networks
| Option | Values | Notes |
|---|---|---|
| docker network rm | <network-name> |
Remove a network by name |
| docker network rm | <network-id> |
Remove a network by ID |
| docker network prune | |
Remove all unused networks |
Note: Networks that are in use by running containers cannot be removed. Stop the containers first using
docker stop <container-id>.
12. Pruning Unused Networks
Use the docker network prune command to remove all unused networks that are not connected to any running containers.
| Option | Values | Description |
|---|---|---|
| --force | true |
Do not prompt for confirmation before pruning networks |
| --filter | label=<key>[=<value>] |
Filter networks by label (e.g., label=environment=production) |