Docker network is a feature that enables containers to communicate with each other in a networked environment. It provides an isolated network environment for containers so that they can communicate with each other securely and efficiently.
With Docker network, we can create and manage networks for our Docker containers. These networks can be used to connect containers running on the same host or across multiple hosts, even in different data centers.
We can also create different types of networks depending on our needs. For example, a bridge network provides communication between containers on the same host, while an overlay network provides communication between containers across multiple hosts.
Overall, Docker network makes it easier to manage container communication and provides more flexibility and security in container networking.
docker network
is a command used to manage Docker networks. Docker provides a way to create virtual networks for containers to communicate with each other, as well as with the host machine or other networks.
Some of the common commands used with docker network
are:
-
docker network create
: Creates a new Docker network. -
docker network ls
: Lists the available Docker networks. -
docker network inspect
: Inspects the details of a Docker network. -
docker network connect
: Connects a container to a Docker network. -
docker network disconnect
: Disconnects a container from a Docker network. -
docker network rm
: Removes a Docker network.
Docker networks are useful for separating containers from each other and allowing them to communicate securely. They can be used to create multi-container applications and microservices that can be scaled and managed easily.
docker network create
The docker network create
command is used to create a new network in Docker. This allows containers to communicate with each other even if they are running on different Docker hosts.
The basic syntax of the command is:
docker network create [OPTIONS] NETWORK_NAME
Here are some of the most commonly used options:
-
-driver
specifies the driver that should be used for the network. The default isbridge
, but other options includeoverlay
andmacvlan
. -
-subnet
specifies the subnet that should be used for the network. This is only used for networks that use thebridge
driver. -
-gateway
specifies the default gateway for the network. This is only used for networks that use thebridge
driver. -
-ip-range
specifies the range of IP addresses that can be assigned to containers on the network. This is only used for networks that use thebridge
driver.
Here is an example of creating a new network called my_network
using the bridge
driver:
docker network create --driver bridge my_network
This creates a new bridge network named my_network
using the default subnet and gateway settings.
A bridge network is a type of network that connects multiple containers together. It is the default network type for Docker containers.
When a container is connected to a bridge network, it can communicate with other containers on the same network using their IP addresses. The bridge network provides a simple and efficient way for containers to communicate with each other without exposing ports to the host system.
docker network ls
The docker network ls
command lists all the available networks on the Docker host.
Here's an example:
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
3614fc1289ac bridge bridge local
799e5a8e5f53 host host local
dcbdf4e4a4af none null local
The output lists the NETWORK ID
, NAME
, DRIVER
, and SCOPE
of each network. The bridge
, host
, and none
networks are the default networks that are created automatically when Docker is installed.
docker network inspect
The docker network inspect
command is used to display detailed information about a specific Docker network or multiple networks. It provides a JSON format output containing the configuration details of the specified network(s).
The basic syntax of the docker network inspect
command is as follows:
docker network inspect [OPTIONS] NETWORK [NETWORK...]
Here, OPTIONS
are the additional flags that can be used with the docker network inspect
command and NETWORK
is the name or ID of the network to inspect.
Some commonly used options with the docker network inspect
command are:
-
-format
: This option is used to format the output using a Go template. The default value is{{json .}}
. -
-verbose
orv
: This option is used to enable verbose output.
Here is an example command that inspects the bridge
network and displays the output in a pretty format:
docker network inspect --format '{{json .Containers}}' bridge | python -m json.tool
This command inspects the bridge
network and displays the containers connected to it in a pretty format.
Note that the docker network inspect
command can also be used to inspect multiple networks by specifying their names or IDs separated by a space.
docker network inspect network1 network2
docker network connect
The docker network connect
command is used to attach a container to an existing Docker network. This allows the container to communicate with other containers and services that are connected to the same network.
The basic syntax of the command is:
docker network connect <network-name> <container-name-or-id>
Here, <network-name>
is the name of the network to which the container should be attached, and <container-name-or-id>
is the name or ID of the container that should be attached to the network.
For example, let's say we have an existing network named "my-network" and a container named "my-container". We can connect the container to the network using the following command:
docker network connect my-network my-container
After running this command, the container "my-container" will be attached to the "my-network" network and will be able to communicate with other containers and services that are connected to the same network.
It is also possible to specify additional options with the docker network connect
command . Here are some commonly used options:
-
-alias
: Assign a network alias to the container on the specified network. This can be useful for container-to-container communication. -
-ip
: Assign a static IP address to the container on the specified network. This can be useful for certain network configurations that require fixed IP addresses.
Here's an example of using the docker network connect
command with the --alias
option:
docker network connect --alias myalias mynetwork mycontainer
This command connects the container mycontainer
to the network mynetwork
and assigns the alias myalias
to the container on that network. This means that other containers on the same network can refer to mycontainer
using the alias myalias
instead of the container name or IP address.
How to display container ip address ?
To display the IP address of a running container, you can use the docker inspect
command with the --format
option to filter the output and display only the IP address.
Here's an example command:
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container-name-or-id>
In this command, replace <container-name-or-id>
with the name or ID of the container you want to inspect. The output will be the IP address of the container.
You can also use the docker network inspect
command to display the IP address of all the containers connected to a specific network. Here's an example command:
docker network inspect <network-name>
Replace <network-name>
with the name of the network you want to inspect. The output will include information about all the containers connected to that network, including their IP addresses.
The container IP address is assigned when the container is started and attached to a network. The IP address is dynamically allocated by the Docker daemon from the subnet range of the network. The Docker daemon uses the IPAM (IP Address Management) driver to manage the allocation of IP addresses for the containers.
If you want to assign statics ip address to the container at the time of attaching it to the network use the -ip
options of docker network connect
Here's an example command:
$ docker network connect --ip 192.168.0.2 my-network my-container
This will connect the my-container
container to the my-network
network and assign it the IP address 192.168.0.2
It's important to note that the specified IP address must be within the subnet of the network and must not already be in use by another container on the same network.
docker network disconnect
The docker network disconnect
command is used to disconnect a container from a network. It takes two arguments: the network name or ID, and the container name or ID. The syntax is as follows:
docker network disconnect [OPTIONS] NETWORK CONTAINER
Here, NETWORK
is the name or ID of the network to disconnect from, and CONTAINER
is the name or ID of the container to disconnect. The available options are:
--force
, -f
: This option forces the container to disconnect from the network. It is useful when the container is stuck in the "disconnecting" state.
Here is an example command to disconnect a container named "my_container" from a network named "my_network":
docker network disconnect my_network my_container
When a container is disconnected from a Docker network using the
docker network disconnect
command, its IP address is released and can be used by other containers connected to the same network.
docker network rm
The docker network rm
command is used to remove one or more Docker networks. The general syntax of the command is:
docker network rm NETWORK [NETWORK...]
where NETWORK
is the name or ID of the network(s) to be removed.
Here's an example:
$ docker network rm my-network
This command will remove the my-network
network.
Note that you cannot remove a network that is currently being used by a container. You must disconnect the container from the network before you can remove it. You can use the docker network disconnect
command to disconnect a container from a network.
Top comments (0)