Docker, a cornerstone of the containerization world, offers powerful tools to connect Docker containers seamlessly across various environments. By diving into its rich set of network types—such as bridge, overlay, and macvlan—users can tailor their container communications to fit specific needs. This article will guide you through the foundational steps needed to create, manage, and remove Docker networks, ensuring that you have the knowledge to design optimal networking solutions for your containerized applications.
Latest posts by Hitesh Jethva (see all)

Create Docker networks

To get help and information about the docker network create command, use the –help option, as shown below.

docker network create –-help
Displaying help information

Displaying help information

You can use the docker network create command to create a Docker network. The basic syntax for creating a Docker network is shown below.

docker network create [OPTIONS] NETWORK

There are different types of Docker network drivers that you can use to create a specific type of Docker network. In this section, we will show you how to use some of them to create a Docker network.

Create a custom bridge network

Bridge is a private default network driver. The bridge network driver allows containers connected to the same bridge network to communicate while providing isolation from containers not connected to that bridge network. To create a custom bridge network named test-network, use the following command:

docker network create test-network

Create an overlay network

An overlay network in Docker facilitates distributed network communication between containers running on different hosts, often used in multihost and orchestrated environments like Docker Swarm. To create an overlay network named test-overlay-network, use the following command:

docker network create --driver overlay test-overlay-network

Create a macvlan network

A macvlan network in Docker allows a container to be directly connected to the physical host network, assigning a unique MAC address to each container, thereby making it appear as a physical network device on the network. To create a macvlan network named test-macvlan-network with a private subnet and gateway, run the following command:

docker network create --driver macvlan --subnet= --gateway= -o parent=eth0 test-macvlan-network

List Docker networks

You can use the docker network ls or docker network list command to list Docker networks on your system. You can use this list to identify your created network and manage Docker network configurations.

docker network ls

You will see the list of all created networks on the following screen.

Listing Docker networks

Listing Docker networks

Inspect Docker networks

You can inspect a Docker network using the docker network inspect command. This command retrieves detailed, low-level information about Docker networks in a JSON format.

The basic syntax for inspecting a Docker network is shown below.

docker network inspect [network-name]

For example, to inspect the Docker network named test-network, run the following command:

docker network inspect test-network

You will see detailed information about your network, such as the network's name, ID, driver, scope, and list of connected containers on the following screen.

Inspecting the Docker network

Inspecting the Docker network

Connect containers to a Docker network

You connect containers to a Docker network to enable communication between them.

The basic syntax for connecting containers to a network is shown below:

docker network connect [network-name] [container-name]

For example, to connect your existing container named nginx-container to a Docker network named test-network, run the following command:

docker network connect test-network nginx-container

You can now verify the connected container using the following command:

docker network inspect test-network

If you want to create a new container with your custom network, use the --network option, as shown below:

docker run --network test-macvlan-network nginx:latest

Here is an explanation of the above command:

  • test-macvlan—Specify the name of your network.
  • nginx:latest—Specify the image used to create a container.

You can also use the --network host flag to run a container in host networking mode:

docker run -dit --network host nginx:latest

In Docker, host networking mode allows a container to share the same network namespace as the host system, meaning the container uses the host's networking directly without any isolation, effectively giving it full access to the host's network interfaces and IP address.

Disconnect containers from a Docker network

You can use the docker network disconnect command to disconnect containers from a Docker network.

First, find the name of the network that you want to disconnect from the container using the docker network ls command.

docker network ls

After identifying the network name, run the following command:

docker network disconnect [container-name-or-id]

Retrieve the current networking configuration:

docker inspect nginx-container -f "{{json .NetworkSettings.Networks }}"

Retrieving the current networking configuration

Retrieving the current networking configuration

To disconnect a container named nginx-container from a network named test-network, run the following:

docker network disconnect test-network nginx-container

The above command will disconnect nginx-container from test-network.

You can now verify that the network has been disconnected.

docker inspect nginx-container -f "{{json .NetworkSettings.Networks }}"

The docker inspect nginx-container -f "{{json .NetworkSettings.Networks }}" command retrieves networking details of a Docker container named nginx-container. The output is formatted as a JSON string, focusing specifically on its network configurations. It uses Go templating to extract and present only the desired information from the container's comprehensive details.

Verifying the disconnected network

Verifying the disconnected network

Use Docker networks in Docker Compose

If you are using Docker Compose for multicontainer applications, you can define networks in the docker-compose.yml file using the networks section.

For example, create a docker-compose.yml file for the NGINX web server and define a custom network:

nano docker-compose.yml

Add the following configurations to the file:

version: '3'
    image: nginx:latest
      - my-network
    driver: bridge

Save and close the file, and then run the docker-compose up -d command to create an NGINX container in the my-network Docker network.

docker-compose up -d

This command will create an NGINX container and connect it to the my-network Docker network.

To verify that your new container is connected to the correct network, inspect the container:

docker inspect nginx-container -f "{{json .NetworkSettings.Networks }}"

Verifying the connected network

Verifying the connected network

Remove Docker networks

You can use the docker network rm command to remove your existing network from your system. The basic syntax for removing a Docker network is shown below.

docker network rm [network-name]

For example, to remove a network named test-network, run the following command:

docker network rm test-network

You can also use the docker network prune command to remove all unused networks from your system. This will help you clean up your system and free up resources.

To remove all unused networks, run the following command:

Subscribe to 4sysops newsletter!

docker network prune


Navigating the intricacies of connecting Docker containers is integral to maximizing Docker's capabilities. By leveraging different network types, such as bridge, overlay, and macvlan, you can architect diverse and adaptable communication pathways for your containers.

1 Comment
  1. Avatar

    You have done a fantastic job of explaining the topic. Keep up the good work!

Leave a reply

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2023


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account