DEV Community

Cover image for Docker : From Zero to Hero ( part 3 )
Prasenjeet Kumar
Prasenjeet Kumar

Posted on • Edited on

Docker : From Zero to Hero ( part 3 )

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 networkis 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
Enter fullscreen mode Exit fullscreen mode

Here are some of the most commonly used options:

  • -driver specifies the driver that should be used for the network. The default is bridge, but other options include overlay and macvlan.
  • -subnet specifies the subnet that should be used for the network. This is only used for networks that use the bridge driver.
  • -gateway specifies the default gateway for the network. This is only used for networks that use the bridge 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 the bridge driver.

Here is an example of creating a new network called my_network using the bridge driver:

docker network create --driver bridge my_network
Enter fullscreen mode Exit fullscreen mode

This creates a new bridge network named my_networkusing 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
Enter fullscreen mode Exit fullscreen mode

The output lists the NETWORK ID, NAME, DRIVER, and SCOPEof each network. The bridge, host, and nonenetworks 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...]
Enter fullscreen mode Exit fullscreen mode

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 or v: This option is used to enable verbose output.

Here is an example command that inspects the bridgenetwork and displays the output in a pretty format:

docker network inspect --format '{{json .Containers}}' bridge | python -m json.tool
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 connectcommand . 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 connectcommand with the --aliasoption:

docker network connect --alias myalias mynetwork mycontainer
Enter fullscreen mode Exit fullscreen mode

This command connects the container mycontainerto the network mynetworkand assigns the alias myaliasto the container on that network. This means that other containers on the same network can refer to mycontainerusing the alias myaliasinstead 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>
Enter fullscreen mode Exit fullscreen mode

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 inspectcommand to display the IP address of all the containers connected to a specific network. Here's an example command:

docker network inspect <network-name>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This will connect the my-containercontainer to the my-networknetwork 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 disconnectcommand 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
Enter fullscreen mode Exit fullscreen mode

Here, NETWORKis the name or ID of the network to disconnect from, and CONTAINERis 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
Enter fullscreen mode Exit fullscreen mode

When a container is disconnected from a Docker network using the docker network disconnectcommand, its IP address is released and can be used by other containers connected to the same network.

docker network rm

The docker network rmcommand is used to remove one or more Docker networks. The general syntax of the command is:

docker network rm NETWORK [NETWORK...]
Enter fullscreen mode Exit fullscreen mode

where NETWORK is the name or ID of the network(s) to be removed.

Here's an example:

$ docker network rm my-network
Enter fullscreen mode Exit fullscreen mode

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)