2.1 Basic docker network
Commands
Networks in Docker allow containers to interact with each other and with external networks. The docker network
commands provide flexible options for creating, configuring, and managing networks, ensuring isolation and control over container network interactions. In this lecture, we'll take a closer look at docker network
commands and how to use them to manage networks.
Key docker network
commands:
- docker network create
- docker network ls
- docker network inspect
- docker network connect
- docker network disconnect
- docker network rm
Creating a Network: docker network create
Command
The docker network create
command is used to create new networks. You can create networks with different drivers, such as bridge
, host
, overlay
, and macvlan
.
Example of creating a network with the bridge
driver
docker network create --driver bridge my_bridge_network
Example of creating a network with the overlay
driver
docker network create --driver overlay my_overlay_network
Example of creating a network with the macvlan
driver
docker network create --driver macvlan \
--subnet=192.168.1.0/24 \
--gateway=192.168.1.1 \
-o parent=eth0 my_macvlan_network
2.2 Network Commands
1. Viewing available networks: command docker network ls
The docker network ls
command displays a list of all networks created in Docker.
docker network ls
The output of the command will look like this:
NETWORK ID NAME DRIVER SCOPE
0e7e2d58fe94 bridge bridge local
9c84fdfc69ee host host local
71cfb6a79d9e none null local
2. Viewing network information: command docker network inspect
The docker network inspect
command allows you to get detailed information about a specific network, including connected containers, subnet and gateway settings.
Example usage:
docker network inspect my_bridge_network
The output of the command will contain network information in JSON format:
[
{
"Name": "my_bridge_network",
"Id": "0e7e2d58fe94",
"Created": "2021-01-01T00:00:00.000000000Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Containers": {
"container_id": {
"Name": "my_container",
"EndpointID": "6c52f8c75c1e",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
3. Connecting a container to a network: command docker network connect
The docker network connect
command is used to connect an already running container to an existing network.
Example usage:
docker network connect my_bridge_network my_container
4. Disconnecting a container from a network: command docker network disconnect
The docker network disconnect
command is used to disconnect a container from a network.
Example usage:
docker network disconnect my_bridge_network my_container
5. Removing a network: command docker network rm
The docker network rm
command is used to remove a network. Note that a network can only be removed after all connected containers are disconnected.
Example usage:
docker network rm my_bridge_network
2.3 Examples of docker network
Commands
Examples of using docker network
commands
Example 1: Creating and Using a bridge
Network
Creating a network:
docker network create --driver bridge my_bridge_network
Starting containers and connecting them to the network:
docker run -d --name container1 --network my_bridge_network nginx
docker run -d --name container2 --network my_bridge_network redis
Checking the network:
docker network inspect my_bridge_network
Output of the docker network inspect
command will include detailed information about the network in JSON format. To make the output more readable, you can use the jq
utility:
docker network inspect my_bridge_network | jq
This is convenient for filtering and viewing structured network data, such as connected containers, their IP addresses, and subnet settings.
Example 2: Connecting and Disconnecting Containers
Starting a container:
docker run -d --name my_container nginx
Connecting a container to a network:
docker network connect my_bridge_network my_container
Disconnecting a container from a network:
docker network disconnect my_bridge_network my_container
GO TO FULL VERSION