# Basic Docker Networking on a Single Host

Learn how to use basic and advanced networking tools to manage [Docker containers](https://www.ionos.com/digitalguide/server/know-how/docker-container-volumes/ "Docker Container Volumes"). The networking feature allows users to define their own networks and connect containers 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 networking Docker containers on a single host. For information and instructions on networking Docker containers across multiple hosts, see our article on the subject.

## Requirements

- IONOS Linux Cloud Server
- Docker installed and running on your system.

## 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:

```none
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:

```none
usermod -aG docker jdoe
```

## Docker’s default networks

Each Docker installation automatically builds three default networks. You can list these networks with the command:

```none
sudo docker network ls
```

The output of this command will look something like this:

[![Image: bridge network](https://www.ionos.com/digitalguide/fileadmin/_processed_/5/d/csm_networking-with-docker-1-show-networks_429e835354.webp "bridge network")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2020/networking-with-docker-1-show-networks.jpg) bridge network The **bridge** network is the default network. Unless otherwise specified, **bridge** is the network where Docker will launch any new container.

## Docker Networking: Bridge vs Overlay

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

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

Because overlay networks are more complicated, we will cover them in another article.

## Inspecting a network

Inspecting a Docker network will return information about that network, including which containers are attached to the network, and their IP addresses. This is a valuable testing tool, as well as an easy way to find your container’s IP address.

You can inspect a network with the command:

```none
sudo docker network inspect [network name]
```

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

```none
sudo docker network inspect bridge
```

The output of this command will look something like this:

[![Image: test_centos_container](https://www.ionos.com/digitalguide/fileadmin/_processed_/9/9/csm_networking-with-docker-2-inspect-network_f570ce6361.webp "test_centos_container")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2020/networking-with-docker-2-inspect-network.jpg) test\_centos\_container As you can see in the above screenshot, only one container is running on this **bridge** network. The container’s name is **test\_centos\_container** and the IP address assigned to it is 172.17.0.3.

Note Containers can only communicate with each other when they are running on the same network. For example, if you had a web application in one container which needed to connect to a database in another container, both of those containers would need to be on the same network.

## Creating a new bridge network

To create your own network, the command is:

```none
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:

```none
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 automatically be added to the default bridge network.

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

```none
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:

```none
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:

```none
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:

```none
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 containers to multiple networks. There is no need to disconnect a container from one network before adding it to another.

## Disconnecting a container from a network

To disconnect a container from a network, use the command:

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

For example to disconnect a container named **test\_centos\_container** from the **new\_test\_network network**, the command would be:

```none
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 disconnected.

Note Docker allows you to add containers to multiple networks. There is no need to disconnect 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 containers that have been added to a network have been stopped or disconnected, you can delete the network with the command:

```none
sudo docker network rm [network ID or name]
```

For example, to remove the network **new\_test\_network**, the command would be:

```none
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 containers on the same network

For this example we will create a custom bridge network, start up two containers 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:

```none
sudo docker pull centos
```

If you prefer to use Ubuntu, simply substitute an Ubuntu image instead.

First, create a bridge network named **ping\_test\_network** with the command:

```none
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:

```none
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:

```none
sudo docker network inspect ping_test_network
```

Make a note of the container’s IP address.

[![Image: IP address](https://www.ionos.com/digitalguide/fileadmin/_processed_/2/9/csm_networking-with-docker-5-get-the-ip-address_4ced4e6e48.webp "IP address")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2020/networking-with-docker-5-get-the-ip-address.jpg) 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:

```none
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:

```none
ping [IP address of ping_receiver]
```

In this case we will ping the IP address 172.19.0.2. The results of a successful ping will look something like this:

[![Image: successful ping](https://www.ionos.com/digitalguide/fileadmin/_processed_/9/7/csm_networking-with-docker-4-results-of-ping_34e5f11f83.webp "successful ping")](https://www.ionos.com/digitalguide/fileadmin/DigitalGuide/Screenshots_2020/networking-with-docker-4-results-of-ping.jpg) successful ping Exit ping with **CTRL-c**, then use **CTRL-pCTRL-q** to exit the container.


This is a markdown version of: [https://www.ionos.com/digitalguide/server/configuration/docker-networking-on-a-single-host/](https://www.ionos.com/digitalguide/server/configuration/docker-networking-on-a-single-host/) for AI/LLM consumption.