Learn how to use basic and advanced net­work­ing tools to manage Docker con­tain­ers. The net­work­ing feature allows users to define their own networks and connect con­tain­ers to them.

You can create a network on a single host, or on a network that spans multiple hosts using the Docker network feature. This article will cover net­work­ing Docker con­tain­ers on a single host. For in­for­ma­tion and in­struc­tions on net­work­ing Docker con­tain­ers across multiple hosts, see our article on the subject.

Re­quire­ments

  • IONOS Linux Cloud Server
  • Docker installed and running on your system.
Web Hosting
Hosting that scales with your ambitions
  • Stay online with 99.99% uptime and robust security
  • Add per­for­mance with a click as traffic grows
  • Includes free domain, SSL, email, and 24/7 support

Safely working with Docker without using Sudo

The Docker daemon runs as root, which means that users will need to use sudo to run Docker commands.

To avoid having to use sudo for every Docker command, simply add your user(s) to the docker group with the command:

usermod -aG docker [username]

For example, by adding the user jdoe to the Docker group, this user will no longer have to use sudo for every Docker command. To add the user to the group, use the command:

usermod -aG docker jdoe

Docker's default networks

Each Docker in­stal­la­tion au­to­mat­i­cal­ly builds three default networks. You can list these networks with the command:

sudo docker network ls

The output of this command will look something like this:

The bridge network is the default network. Unless otherwise specified, bridge is the network where Docker will launch any new container.

Docker Net­work­ing: Bridge vs Overlay

Docker natively supports two types of networks: bridge and overlay.

  • bridge network is limited to a single host (server).
  • An overlay network can span multiple hosts (servers).

Because overlay networks are more com­pli­cat­ed, we will cover them in another article.

In­spect­ing a network

In­spect­ing a Docker network will return in­for­ma­tion about that network, including which con­tain­ers are attached to the network, and their IP addresses. This is a valuable testing tool, as well as an easy way to find your con­tain­er's IP address.

You can inspect a network with the command:

sudo docker network inspect [network name]

For example, to inspect the bridge network, the command is:

sudo docker network inspect bridge

The output of this command will look something like this:

As you can see in the above screen­shot, only one container is running on this bridge network. The con­tain­er's name is test_centos_container and the IP address assigned to it is 172.17.0.3.

Note

Con­tain­ers can only com­mu­ni­cate with each other when they are running on the same network. For example, if you had a web ap­pli­ca­tion in one container which needed to connect to a database in another container, both of those con­tain­ers would need to be on the same network.

Creating a new bridge network

To create your own network, the command is:

sudo docker network create -d [driver] [new network name]

The -d flag lets you specify the driver for the network. For a bridge network, use the bridge driver.

For example, to create a bridge network named new_test_network, use the command:

sudo docker network create -d bridge new_test_network

You can use sudo docker network ls to list the networks on your host and verify that your network has been created.

Starting a container and adding it to a network

If you do not specify a network when you start up a container, it will au­to­mat­i­cal­ly be added to the default bridge network.

To start up a container and add it to a specific network, use the command:

sudo docker run -it --net=[network name] --name [container name] [image]

For example, to start a container named test_python_container from the python image and add it to a network named new_test_network, the command would be:

sudo docker run -it --net=new_test_network --name test_centos_container centos

Use CTRL-pCTRL-q to detach from the container.

You can use sudo docker network inspect [network name] to check the network and verify that the container is attached.

Adding a running container to a network

You can add a running container to a network with the command:

sudo docker network connect [network name] [container name]

For example, to add a running container named test_centos_container to a network named another_test_network, the command would be:

sudo docker network connect another_test_network test_centos_container

You can use sudo docker network inspect [network name] to check the network and verify that the container is attached.

Note

Docker allows you to add con­tain­ers to multiple networks. There is no need to dis­con­nect a container from one network before adding it to another.

Dis­con­nect­ing a container from a network

To dis­con­nect a container from a network, use the command:

sudo docker network disconnect [network name] [container ID or name]

For example to dis­con­nect a container named test_centos_container from the new_test_network network, the command would be:

sudo docker network disconnect new_test_network test_centos_container

You can use the command sudo docker network inspect [network name] to inspect the network and verify that the container has been dis­con­nect­ed.

Note

Docker allows you to add con­tain­ers to multiple networks. There is no need to dis­con­nect a container from one network before adding it to another.

Deleting a network

Note

You cannot delete the default networks that Docker creates on start-up.

After all of the con­tain­ers that have been added to a network have been stopped or dis­con­nect­ed, you can delete the network with the command:

sudo docker network rm [network ID or name]

For example, to remove the network new_test_network, the command would be:

sudo docker network rm new_test_network

You can verify that the network was removed with the sudo docker network ls command.

Example: Pinging between two con­tain­ers on the same network

For this example we will create a custom bridge network, start up two con­tain­ers on that network, attach to the ping_sender container, and ping the other container ping_receiver from there.

Note

This example will use the official CentOS 7 image named centos which you can download from the Docker website with the command:

sudo docker pull centos

If you prefer to use Ubuntu, simply sub­sti­tute an Ubuntu image instead.

First, create a bridge network named ping_test_network with the command:

sudo docker network create -d bridge ping_test_network

Next, start a container named ping_receiver and attach it to the ping_test_network bridge network with the command:

sudo docker run -it --net=ping_test_network  --name ping_receiver centos /bin/bash

Use CTRL-p CTRL-q to detach from the ping_receiver container.

Inspect the ping_test_network to get the IP address of the ping_receiver container with the command:

sudo docker network inspect ping_test_network

Make a note of the con­tain­er's IP address.

In this case, the IP address is 172.19.0.2.

Start a container named ping_sender and attach it to the ping_test_network bridge with the command:

sudo docker run -it --net=ping_test_network  --name ping_sender centos bin/bash

Once you are attached to the container and have a command prompt, ping the other container with the command:

ping [IP address of ping_receiver]

In this case we will ping the IP address 172.19.0.2. The results of a suc­cess­ful ping will look something like this:

Exit ping with CTRL-c, then use CTRL-pCTRL-q to exit the container.

VPS Hosting (Virtual Private Servers)
World-class VPS at America’s lowest price
  • Save 50% or more vs leading hosts
  • Unlimited traffic & no setup fees 
  • 99.99% uptime guar­an­teed
  • Free 24/7 premium support 
Go to Main Menu