How to use docker network connect command to manage container networks

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively manage container networks using the docker network connect command. You will explore how to connect a running container to an existing network, connect a container to a network during its initial startup, specify a static IP address for a container on a network, and create network aliases for containers to facilitate easier communication.

Through hands-on exercises, you will gain practical experience in manipulating container network configurations, enabling you to build more robust and interconnected Dockerized applications.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/NetworkOperationsGroup(["Network Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/NetworkOperationsGroup -.-> docker/network("Manage Networks") subgraph Lab Skills docker/run -.-> lab-555173{{"How to use docker network connect command to manage container networks"}} docker/ps -.-> lab-555173{{"How to use docker network connect command to manage container networks"}} docker/rm -.-> lab-555173{{"How to use docker network connect command to manage container networks"}} docker/network -.-> lab-555173{{"How to use docker network connect command to manage container networks"}} end

Connect a running container to a network

In this step, you will learn how to connect a running Docker container to an existing network. This is useful when you have a container that is already running and you need it to communicate with other containers or services on a specific network.

First, let's create a new network that we will use for this demonstration. We will create a bridge network named my-network.

docker network create my-network

You should see output similar to this, indicating the network was created successfully:

<network_id>

Now, let's run a simple container that is not initially connected to my-network. We will run an alpine container in detached mode (-d) and name it my-container.

docker run -d --name my-container alpine sleep infinity

The output will be the container ID:

<container_id>

To verify that the container is running and not connected to my-network, you can inspect the container's network settings.

docker inspect my-container --format '{{json .NetworkSettings.Networks}}'

The output will show the default bridge network, but not my-network:

{
  "bridge": {
    "IPAMData": null,
    "IPAddress": "<ip_address>",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "MacAddress": "<mac_address>",
    "DriverOpts": null
  }
}

Now, let's connect the running my-container to the my-network using the docker network connect command.

docker network connect my-network my-container

There will be no output if the command is successful.

To verify that the container is now connected to my-network, inspect the container's network settings again.

docker inspect my-container --format '{{json .NetworkSettings.Networks}}'

This time, the output should show both the default bridge network and my-network:

{
  "bridge": {
    "IPAMData": null,
    "IPAddress": "<ip_address>",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "MacAddress": "<mac_address>",
    "DriverOpts": null
  },
  "my-network": {
    "IPAMData": null,
    "IPAddress": "<ip_address>",
    "IPPrefixLen": 24,
    "IPv6Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "MacAddress": "<mac_address>",
    "DriverOpts": null
  }
}

Finally, let's clean up the container and the network we created.

docker stop my-container
docker rm my-container
docker network rm my-network

Connect a container to a network when it starts

In this step, you will learn how to connect a Docker container to a specific network when you start it. This is the most common way to ensure your containers are on the correct network from the beginning.

First, let's create a new network named app-network that we will use.

docker network create app-network

You should see output similar to this, indicating the network was created successfully:

<network_id>

Now, we will run a simple nginx container and connect it to the app-network directly when starting it. We use the --network flag followed by the network name. We will also run it in detached mode (-d) and name it web-server.

docker run -d --name web-server --network app-network nginx

The output will be the container ID:

<container_id>

To verify that the container is connected to app-network, you can inspect the container's network settings.

docker inspect web-server --format '{{json .NetworkSettings.Networks}}'

The output should show that the container is connected to app-network:

{
  "app-network": {
    "IPAMData": null,
    "IPAddress": "<ip_address>",
    "IPPrefixLen": 24,
    "IPv6Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "MacAddress": "<mac_address>",
    "DriverOpts": null
  }
}

Notice that this time, the container is only connected to app-network and not the default bridge network, because we specified the network during the docker run command.

Finally, let's clean up the container and the network.

docker stop web-server
docker rm web-server
docker network rm app-network

Specify the IP address for a container on a network

In this step, you will learn how to assign a specific static IP address to a container when connecting it to a network. By default, Docker assigns IP addresses dynamically from the network's subnet. However, in some cases, you might need a container to have a predictable IP address.

First, let's create a new network with a specified subnet. We will create a bridge network named static-net with the subnet 172.20.0.0/16.

docker network create --subnet 172.20.0.0/16 static-net

You should see output similar to this, indicating the network was created successfully:

<network_id>

Now, we will run an alpine container and connect it to the static-net, specifying a static IP address using the --ip flag. We will assign the IP address 172.20.0.10. We will run it in detached mode (-d) and name it static-ip-container.

docker run -d --name static-ip-container --network static-net --ip 172.20.0.10 alpine sleep infinity

The output will be the container ID:

<container_id>

To verify that the container has the assigned static IP address on static-net, inspect the container's network settings.

docker inspect static-ip-container --format '{{json .NetworkSettings.Networks}}'

The output should show the container connected to static-net with the specified IP address:

{
  "static-net": {
    "IPAMData": null,
    "IPAddress": "172.20.0.10",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "MacAddress": "<mac_address>",
    "DriverOpts": null
  }
}

Finally, let's clean up the container and the network.

docker stop static-ip-container
docker rm static-ip-container
docker network rm static-net

Create network aliases for a container

In this step, you will learn how to create network aliases for a container. Network aliases provide alternative names that other containers on the same network can use to resolve and connect to the container. This is useful for service discovery within a Docker network.

First, let's create a new network named alias-network.

docker network create alias-network

You should see output similar to this, indicating the network was created successfully:

<network_id>

Now, we will run an nginx container and connect it to the alias-network, assigning a network alias using the --network-alias flag. We will assign the alias web. We will run it in detached mode (-d) and name it alias-container.

docker run -d --name alias-container --network alias-network --network-alias web nginx

The output will be the container ID:

<container_id>

To verify that the container has the network alias, inspect the container's network settings.

docker inspect alias-container --format '{{json .NetworkSettings.Networks}}'

The output should show the container connected to alias-network and list the network alias:

{
  "alias-network": {
    "IPAMData": null,
    "IPAddress": "<ip_address>",
    "IPPrefixLen": 24,
    "IPv6Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "MacAddress": "<mac_address>",
    "Aliases": ["web", "alias-container"],
    "DriverOpts": null
  }
}

Notice the "Aliases":["web","alias-container"] part in the output. Docker automatically adds the container name as an alias as well.

Now, let's run another container on the same network and try to ping the alias-container using its network alias web. We will run an alpine container interactively (-it) on alias-network.

docker run -it --rm --network alias-network alpine ping -c 3 web

You should see output indicating successful pings to the alias-container using the alias web:

PING web (<ip_address>): 56 data bytes
64 bytes from <ip_address>: seq=0 ttl=64 time=0.xxx ms
64 bytes from <ip_address>: seq=1 ttl=64 time=0.xxx ms
64 bytes from <ip_address>: seq=2 ttl=64 time=0.xxx ms

--- web ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.xxx/0.xxx/0.xxx/0.xxx ms

This demonstrates that containers on the same network can resolve and communicate with each other using network aliases.

Finally, let's clean up the containers and the network.

docker stop alias-container
docker rm alias-container
docker network rm alias-network

Summary

In this lab, you learned how to manage Docker container networks using the docker network connect command. You practiced connecting a running container to an existing network, demonstrating how to dynamically add network connectivity to a container after it has been started.

Furthermore, you explored how to connect a container to a specific network at the time of its creation, specify a static IP address for a container on a network, and create network aliases to provide alternative names for a container within a network, enhancing discoverability and communication between containers.