How to test communication between containers on the same custom network?

DockerDockerBeginner
Practice Now

Introduction

Docker is a powerful platform for building, deploying, and running applications in a containerized environment. Understanding how to manage and test communication between Docker containers is crucial for building robust and scalable applications. This tutorial will guide you through the process of creating a custom Docker network and testing the communication between containers on the same network.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker(("`Docker`")) -.-> docker/NetworkOperationsGroup(["`Network Operations`"]) docker/ContainerOperationsGroup -.-> docker/create("`Create Container`") docker/ContainerOperationsGroup -.-> docker/ps("`List Running Containers`") docker/ContainerOperationsGroup -.-> docker/start("`Start Container`") docker/ContainerOperationsGroup -.-> docker/stop("`Stop Container`") docker/NetworkOperationsGroup -.-> docker/network("`Manage Networks`") docker/ContainerOperationsGroup -.-> docker/ls("`List Containers`") subgraph Lab Skills docker/create -.-> lab-411612{{"`How to test communication between containers on the same custom network?`"}} docker/ps -.-> lab-411612{{"`How to test communication between containers on the same custom network?`"}} docker/start -.-> lab-411612{{"`How to test communication between containers on the same custom network?`"}} docker/stop -.-> lab-411612{{"`How to test communication between containers on the same custom network?`"}} docker/network -.-> lab-411612{{"`How to test communication between containers on the same custom network?`"}} docker/ls -.-> lab-411612{{"`How to test communication between containers on the same custom network?`"}} end

Understanding Docker Networks

Docker provides a built-in networking system that allows containers to communicate with each other and the outside world. Docker supports several types of networks, including:

Bridge Network

The default network type in Docker is the bridge network. When you start a new container, it is automatically connected to the default bridge network unless you specify a different network. Containers on the same bridge network can communicate with each other using their container names or IP addresses.

graph LR A[Host] -- Bridge Network --> B[Container 1] A[Host] -- Bridge Network --> C[Container 2] B[Container 1] -- Bridge Network --> C[Container 2]

Host Network

The host network allows a container to use the host's network stack directly, bypassing the Docker network. This can be useful for performance-sensitive applications or when you need to access host-specific network resources.

Overlay Network

The overlay network is used to connect multiple Docker daemons together, enabling swarm services to communicate with each other. Overlay networks are typically used in a Docker Swarm environment.

Custom Network

In addition to the default networks, you can create your own custom networks. Custom networks allow you to isolate containers from each other and control how they communicate. This is useful for building more complex, multi-tier applications.

graph LR A[Host] -- Custom Network --> B[Container 1] A[Host] -- Custom Network --> C[Container 2] B[Container 1] -- Custom Network --> C[Container 2]

Understanding Docker's networking capabilities is crucial for building and managing containerized applications. In the next section, we'll explore how to create a custom network and test communication between containers on that network.

Creating a Custom Network

To create a custom network in Docker, you can use the docker network create command. This command allows you to specify the network driver, subnet, and other configuration options.

Create a Custom Bridge Network

Let's create a custom bridge network named my-network:

docker network create my-network

You can verify the network creation using the docker network ls command:

NETWORK ID     NAME         DRIVER    SCOPE
a1b2c3d4e5f6   my-network   bridge    local

Connect Containers to the Custom Network

Now, let's start two containers and connect them to the my-network custom network:

## Start container 1
docker run -d --name container1 --network my-network ubuntu:22.04 sleep infinity

## Start container 2
docker run -d --name container2 --network my-network ubuntu:22.04 sleep infinity

You can inspect the network and see the connected containers using the docker network inspect command:

[
  {
    "Name": "my-network",
    "Id": "a1b2c3d4e5f6",
    "Created": "2023-04-18T12:34:56.789Z",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "IPAM": {
      "Driver": "default",
      "Options": {},
      "Config": [
        {
          "Subnet": "172.18.0.0/16"
        }
      ]
    },
    "Internal": false,
    "Attachable": false,
    "Ingress": false,
    "ConfigFrom": {
      "Network": ""
    },
    "ConfigOnly": false,
    "Containers": {
      "container1_id": {
        "Name": "container1",
        "EndpointID": "container1_endpoint_id",
        "MacAddress": "02:42:ac:12:00:02",
        "IPv4Address": "172.18.0.2/16",
        "IPv6Address": ""
      },
      "container2_id": {
        "Name": "container2",
        "EndpointID": "container2_endpoint_id",
        "MacAddress": "02:42:ac:12:00:03",
        "IPv4Address": "172.18.0.3/16",
        "IPv6Address": ""
      }
    },
    "Options": {},
    "Labels": {}
  }
]

Now that we have created a custom network and connected containers to it, we can test the communication between the containers in the next section.

Testing Container Communication

Now that we have created a custom network and connected containers to it, we can test the communication between the containers.

Ping Between Containers

Let's start by pinging one container from the other:

## Enter the first container
docker exec -it container1 bash

## Ping the second container by name
ping -c 4 container2

The output should show that the containers can communicate with each other using their container names:

PING container2 (172.18.0.3) 56(84) bytes of data.
64 bytes from container2.my-network (172.18.0.3): icmp_seq=1 ttl=64 time=0.065 ms
64 bytes from container2.my-network (172.18.0.3): icmp_seq=2 ttl=64 time=0.057 ms
64 bytes from container2.my-network (172.18.0.3): icmp_seq=3 ttl=64 time=0.057 ms
64 bytes from container2.my-network (172.18.0.3): icmp_seq=4 ttl=64 time=0.057 ms

--- container2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 0.057/0.059/0.065/0.003 ms

Test TCP Communication

You can also test TCP communication between the containers. For example, you can run a simple HTTP server in one container and access it from the other container:

## Start a simple HTTP server in container1
docker exec -it container1 bash
python3 -m http.server 8000

## In another terminal, access the HTTP server from container2
docker exec -it container2 bash
curl http://container1:8000

The output in the second terminal should show the contents of the HTTP server running in the first container.

By testing the communication between containers on the same custom network, you can ensure that your containerized application components can properly interact with each other, which is essential for building and deploying complex, multi-service applications.

Summary

In this Docker tutorial, you will learn how to create a custom network, launch containers on that network, and verify the communication between them. By the end of this guide, you will have the knowledge and skills to test the connectivity between your Docker containers, ensuring your applications can seamlessly interact with each other within the same custom network.

Other Docker Tutorials you may like