Docker Network Commands
Master Docker Network Commands: Learn to create, inspect, and manage networks for seamless container communication and customization.
Published Nov 19, 2024
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:This command provides a list of all networks created on your Docker host. Here’s how you can use it:
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:
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.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.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: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:
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.
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.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:
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.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.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.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.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.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.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.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.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: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.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.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.
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.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.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.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.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.
To achieve the above use case, let’s create a network ‘test’ first:
Now, create a container with ubuntu image with network as ‘test’ :
List the containers:
Inspect the container using inspect command:
Delete a Network: