Docker Networking Example

Docker Networking Example

Docker Networking Example

Let’s walk through a practical example that involves setting up and using Docker networking, specifically focusing on a custom bridge network and container-to-container communication.

Scenario:

We want to deploy two containers:

  1. Web container (Nginx): This will serve as the frontend of our application.

  2. Database container (MySQL): This will store the application's data.

Both containers will be able to communicate with each other over a custom bridge network.

Step-by-Step Example

1. Create a Custom Bridge Network

First, we’ll create a custom bridge network. This will ensure that the containers can communicate with each other using their container names as hostnames.

docker network create --driver bridge my_custom_network

This command creates a custom bridge network named my_custom_network.

2. Run the Database Container (MySQL)

We’ll now run a MySQL container and connect it to the custom bridge network. We will also expose the MySQL port (3306) so that other containers can communicate with it.

docker run --network my_custom_network --name mysql-container -e MYSQL_ROOT_PASSWORD=rootpassword -d mysql:latest
  • --network my_custom_network: This connects the container to the custom network we created earlier.

  • --name mysql-container: This assigns the container a name for easy reference.

  • -e MYSQL_ROOT_PASSWORD=rootpassword: This sets the root password for MySQL.

  • -d: Runs the container in detached mode (in the background).

  • mysql:latest: This specifies the MySQL image to use.

You can check if the container is running by using:

docker ps

3. Run the Web Container (Nginx)

Now, let’s run a second container for the web server (Nginx), also connected to the same custom bridge network. This will allow the web container to communicate with the MySQL container using the MySQL container’s name (mysql-container).

docker run --network my_custom_network --name web-container -d nginx
  • --network my_custom_network: This connects the container to the same network as the MySQL container.

  • --name web-container: This assigns the container a name for easy reference.

  • -d: Runs the container in detached mode (in the background).

  • nginx: This specifies the Nginx image to use.

4. Test Communication Between Containers

Now that both containers are running and connected to the same custom bridge network, we can test their communication.

Step 1: Access the Web Container

We will access the web container and try to connect to the MySQL container using the MySQL hostname (mysql-container).

docker exec -it web-container /bin/bash

This opens a bash shell inside the web-container.

Step 2: Install MySQL Client

Inside the web-container, we need to install the MySQL client to test the connection.

apt-get update && apt-get install mysql-client -y

Step 3: Test MySQL Connection

Now, use the MySQL client to connect to the MySQL database using the MySQL container's name (mysql-container) and the root password we set earlier (rootpassword).

mysql -h mysql-container -u root -prootpassword

If successful, this should log you into the MySQL database. The -h mysql-container tells the MySQL client to connect to the mysql-container service, which Docker resolves to the correct IP address on the my_custom_network.

5. Clean Up

To stop and remove the containers, use the following commands:

docker stop web-container mysql-container docker rm web-container mysql-container docker network rm my_custom_network

This stops and removes the containers and then removes the custom network.

Summary of the Example

  1. Created a custom bridge network to allow communication between containers.

  2. Launched a MySQL container on the custom network and exposed the MySQL port.

  3. Launched an Nginx container on the same network, which was able to communicate with the MySQL container.

  4. Tested container-to-container communication using the MySQL client from the web container.

Key Concepts:

  • Bridge Network: A default network type that allows container-to-container communication on the same host.

  • Container Names as Hostnames: Containers on the same network can communicate using their container names as DNS names.

  • Networking Between Containers: By connecting multiple containers to the same network, they can discover and communicate with each other by container name.

Souy Soeng

Souy Soeng

Hi there šŸ‘‹, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
šŸ‘Æ I’m looking to collaborate on open-source PHP & JavaScript projects
šŸ’¬ Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close