The docker network
command is used to manage Docker networks. It allows you to create, inspect, list, connect, disconnect, and remove Docker networks. Below are some common subcommands and examples of using the docker network
command:
👉List Docker Networks:
This command provides a list of all networks created on your Docker host. Here’s how you can use it:
docker network ls
When you run this command, Docker will output a table with information about each network, including its ID, name, driver, and scope (local or global). The output will look something like this:
NETWORK ID NAME DRIVER SCOPE
abcdef123456 bridge bridge local
ghijkl789012 host host local
mnopqr345678 my-network bridge local
stuvwx901234 my-overlay-net overlay swarm
In this example output:
NETWORK ID
: Unique identifier for each network.NAME
: Name of the network.DRIVER
: The type of driver used for the network (e.g., bridge, overlay, host).SCOPE
: Specifies whether the network is local (only accessible on the current host) or global (accessible across multiple hosts in a swarm).
The docker network ls
command is useful for quickly checking the existing Docker networks on your system and their basic properties.
👉 Inspect a Docker Network:
To inspect a Docker network and view detailed information about it, you can use the docker network inspect
command followed by the network's name or ID.
docker network inspect NETWORK_NAME_OR_ID
Replace NETWORK_NAME_OR_ID
with the actual name or ID of the Docker network you want to inspect. For example, if you want to inspect a network named my-network
, you would use:
docker network inspect my-network
The output of the docker network inspect
command provides comprehensive details about the specified network, including its configuration, containers connected to it, IP address ranges, and more. The information is presented in JSON format.
Example of the output:
[
{
"Name": "my-network",
"Id": "abcdef1234567890",
"Created": "2022-01-01T12:00:00Z",
"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,
"Containers": {
"container1": {
"Name": "container1",
"EndpointID": "xyz123456789",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"container2": {
"Name": "container2",
"EndpointID": "abc987654321",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
The output contains information such as the network’s name, ID, creation time, driver, IP address management (IPAM) configuration, connected containers, and additional options. This information can be useful for troubleshooting, managing network configurations, or understanding network settings in Docker.
👉Create a Docker Network:
To create a Docker network, you can use the docker network create
command followed by the desired options and the name you want to give to the network.
docker network create my-network
This command creates a Docker network named my-network
using the default bridge driver and default options. If you want to specify additional options, such as the network driver, subnet, gateway, or other configurations, you can include those options in the command.
Here’s an example of creating a Docker network with custom options:
docker network create \
--driver bridge \
--subnet 172.18.0.0/16 \
--gateway 172.18.0.1 \
--ip-range 172.18.0.0/24 \
my-custom-network
In this example:
--driver bridge
: Specifies the network driver as bridge. You can use other drivers like overlay, macvlan, etc., based on your requirements.--subnet 172.18.0.0/16
: Defines the subnet for the network.--gateway 172.18.0.1
: Specifies the gateway IP address for the network.--ip-range 172.18.0.0/24
: Defines the range of IP addresses that can be assigned to containers on this network.my-custom-network
: The name given to the network.
After running the docker network create
command, Docker will create the specified network with the provided configurations. You can verify the network's creation by using the docker network ls
command to list all networks on your Docker host.
👉 Connect a Container to a Network:
To connect a Docker container to a network, you can use the docker network connect
command followed by the network name and the container name or ID.
docker network connect my-network my-container
In this example:
my-network
is the name of the network to which you want to connect the container.my-container
is the name or ID of the container you want to connect to the network.
After running this command, the specified container (my-container
) will be connected to the my-network
network. This allows the container to communicate with other containers on the same network using their container names or IP addresses within the network.
👉 Disconnect a Container from a Network:
To disconnect a Docker container from a network, you can use the docker network disconnect
command followed by the network name and the container name or ID.
docker network disconnect my-network my-container
In this example:
my-network
is the name of the network from which you want to disconnect the container.my-container
is the name or ID of the container you want to disconnect from the network.
After running this command, the specified container (my-container
) will be disconnected from the my-network
network. This means that the container will no longer be able to communicate with other containers on that network, although it may still be connected to other networks or have its own network namespace.
👉Remove a Docker Network:
To remove a Docker network, you can use the docker network rm
command followed by the name or ID of the network you want to remove.
docker network rm my-network
my-network
is the name of the Docker network you want to remove.
After running this command, Docker will delete the specified network (my-network
). Any containers connected exclusively to this network will be disconnected from it. However, if a container is connected to multiple networks and one of them is removed, the container remains connected to the other networks.
👉 docker network prune:
The docker network prune
command is used to remove all unused Docker networks from your system. Unused networks are those that are not connected to any containers. This command helps clean up your Docker environment by removing networks that are no longer in use.
docker network prune
When you run docker network prune
without any options, Docker will prompt you to confirm whether you want to remove all unused networks. You can type y
and press Enter to proceed with the removal.
If you want to skip the confirmation prompt, you can use the -f
or --force
option:
docker network prune -f
This command will immediately remove all unused networks without asking for confirmation.
Note - It’s important to note that the docker network prune
command only removes unused networks. Networks that are still in use by one or more containers will not be removed.
👉docker network create-ipam:
The docker network create-ipam
command is used to create a new IP address management (IPAM) configuration for a Docker network. IPAM allows you to define custom IP address ranges, subnets, gateways, and other network configurations.
docker network create-ipam --subnet=192.168.1.0/24 my-custom-network
In this example:
--subnet=192.168.1.0/24
specifies the subnet for the new IPAM configuration. You can adjust the subnet to match your network requirements.my-custom-network
is the name given to the Docker network for which you are creating the custom IPAM configuration.
After running the docker network create-ipam
command, Docker will create a new IPAM configuration with the specified subnet for the my-custom-network
Docker network. This allows you to define specific IP address ranges and network settings for containers connected to this network.
It’s worth noting that custom IPAM configurations are optional, and Docker provides default IPAM settings for networks created without specifying custom configurations. Custom IPAM configurations are useful for advanced networking scenarios where you need precise control over IP address allocation and network parameters.
👉docker network connect-ipam:
The docker network connect-ipam
command is used to connect a container to a Docker network with a custom IP address management (IPAM) configuration. IPAM allows you to define specific IP address ranges, subnets, gateways, and other network settings.
docker network connect-ipam --ip=192.168.1.10 my-custom-network my-container
In this example:
--ip=192.168.1.10
specifies the IP address that you want to assign to the container on the connected network. You can adjust the IP address as needed.my-custom-network
is the name of the Docker network with a custom IPAM configuration to which you want to connect the container.my-container
is the name or ID of the container you want to connect to the network.
After running the docker network connect-ipam
command, Docker will connect the specified container (my-container
) to the my-custom-network
Docker network with the custom IPAM configuration. The container will be assigned the specified IP address (192.168.1.10
) on the network.
Using the docker network connect-ipam
command with custom IPAM configurations allows you to have more control over IP address assignment and network parameters for containers connected to your Docker networks.
👉docker network disconnect-ipam:
The docker network disconnect-ipam
command is used to disconnect a container from a Docker network that has a custom IP address management (IPAM) configuration. This command is specifically designed for networks with custom IPAM settings.
docker network disconnect-ipam my-custom-network my-container
In this example:
my-custom-network
is the name of the Docker network with a custom IPAM configuration from which you want to disconnect the container.my-container
is the name or ID of the container you want to disconnect from the network.
After running the docker network disconnect-ipam
command, Docker will disconnect the specified container (my-container
) from the my-custom-network
Docker network with the custom IPAM configuration.
It’s important to note that the docker network disconnect-ipam
command is specifically used for networks with custom IPAM configurations. For networks with default IPAM settings, you would use the regular docker network disconnect
command without the -ipam
suffix.
👉 Example:
By default when the bridge network is created, the DNS is not enabled. But if we create our custom bridge network DNS is enabled by default.
loclhost:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
localhost:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
6c51373f78ad bridge bridge local
3a33f83c3664 host host local
e4ebd601732e none null local
localhost:~$ docker container run -itd ubuntu:14.04 bash
7b1af2ee48e43f8018c4324bbcb9f52a27f741bd7a0437ddd0f6766bd7ca6b10
localhost:~$ docker container run -itd ubuntu:14.04 bash
c3ce5dbe5d859705f139e811bc11367d02bf0969492e9d515a3cc6cc636ddfbb
localhost:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3ce5dbe5d85 ubuntu:14.04 "bash" 8 seconds ago Up 7 seconds unruffled_sinoussi
7b1af2ee48e4 ubuntu:14.04 "bash" 10 seconds ago Up 9 seconds trusting_joliot
localhost:~$ docker container exec -it 7b bash
root@7b1af2ee48e4:/# ping c3ce5dbe5d85
ping: unknown host c3ce5dbe5d85
root@7b1af2ee48e4:/#
To achieve the above use case, let’s create a network ‘test’ first:
localhost:~$ docker network create test
b1e05c1afdb2f901e81a66a52d64a9dcdca9c5cab98433cdaed2faa83c5b3e6b
localhost:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
localhost:~$
Now, create a container with ubuntu image with network as ‘test’ :
localhost:~$ docker container run -itd --network=test ubuntu:14.04 bash
c7b07b61bb20cdbb6e1b54a165aed0f8907d95d563fdd7a60940d004694c4557
List the containers:
localhost:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7b07b61bb20 ubuntu "bash" About a minute ago Up About a minute clever_wu
Inspect the container using inspect command:
"Networks": {
"host": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "b1e05c1afdb2f901e81a66a52d64a9dcdca9c5cab98433cdaed2faa83c5b3e6b",
"EndpointID": "b860ca4fdda3e0732367949cb94fd2eded08a4f2e46715a6c125b1bf336c102f",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
Delete a Network:
localhost:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
6c51373f78ac bridge bridge local
3a33f83c3663 host host local
e4ebd601732c none null local
348f7295d3ca test bridge local
localhost:~$ docker network rm test
test
localhost:~$
###