Communicating two docker-compose.yml using a common network: example of communicating MariaDB with PHPMyAdmin.

Tiempo de lectura: 3 minutos

Reading time: 3 minutes

Today I’m going to show you how to communicate two docker-compose using a Docker virtual network.

To communicate two Docker containers created using different docker-compose.yml files, it is necessary to use a Docker network. A Docker network is a means of communication between different Docker containers that allows containers to communicate with each other and with the Docker host. In this tutorial, I will guide you through the process of creating a Docker network and configuring two Docker containers to communicate with each other.

Step 1: Create a Docker network

The first thing to do is to create a Docker network where the two containers can communicate with each other. To do this, open a terminal and run the following command:

docker network create mynetwork

This command will create a new Docker network named “mynetwork”. You can choose any name you want for your network.

Step 2: Configure the docker-compose.yml files

Once the Docker network has been created, it is necessary to configure the docker-compose.yml files so that the containers use the network created earlier. To do this, we need to add the network section in both docker-compose.yml files and specify the name of the network we created in the previous step.

For example, if we have two docker-compose.yml files named docker-compose1.yml and docker-compose2.yml, we can add the following network section to both files:

networks:
  mynetwork:
    external:
      name: mynetwork

The networks section should be added in the same section where each container is defined. In this case, we are adding the network section outside the services sections.

Step 3: Start the containers

Once the docker-compose.yml files have been configured, it’s time to start the containers. To do this, open a terminal and run the following commands:

cd /path/to/docker-compose1.yml
docker-compose up -d

cd /path/to/docker-compose2.yml
docker-compose up -d

These commands will start the containers defined in the docker-compose.yml files. The containers will run in the background (-d) and will be connected to the Docker network we created earlier.

Step 4: Check the communication between the containers

To verify that the containers are communicating correctly, we can use the docker exec command to access the shell of one container and perform a connection test with the other container.

For example, if we want to check that the container defined in the docker-compose1.yml file can communicate with the container defined in the docker-compose2.yml file, we can execute the following commands:

docker exec -it <container-name-in-docker-compose1> /bin/bash

ping <container-name-in-docker-compose2>

This should produce output similar to the following:

PING <container-name-in-docker-compose2> (<container-IP-address-in-docker-compose2>): 56 data bytes
64 bytes from <container-IP-address-in-docker-compose2>: icmp_seq=0 ttl=64 time=0.138 ms
64 bytes from <container-IP-address-in-docker-compose2>:

In this example, we will communicate MariaDB with PhpMyadmin in two different files:

Step 1: Create a docker-compose1.yml file for the MariaDB container

version: '3.9'

services:
  db:
    image: mariadb
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: mysecretpassword
    ports:
      - "3306:3306"
    volumes:
      - db_data:/var/lib/mysql
    networks:
      mynetwork:
        aliases:
          - mariadb

volumes:
  db_data:

networks:
  mynetwork:
    driver: bridge

In this file we are defining a service called phpmyadmin that uses the phpmyadmin image. We have specified the host and port of the database in the environment variables section.

In addition, we have exposed port 80 to access PhpMyAdmin in the browser and added an alias section with the name phpmyadmin to identify this container.

Finally, we have added the networks section and specified that we are using an external network mynetwork defined previously in the docker-compose1.yml file.

Step 3: Start the containers on the Docker network

Now that we have created both docker-compose.yml files, we can start the containers on the Docker network we created. To do this, open a terminal in the same directory as the docker-compose.yml files and run the following command:

docker-compose -f docker-compose1.yml up -d
docker-compose -f docker-compose2.yml up -d

This will start the MariaDB and PhpMyAdmin containers and connect them to the mynetwork network.

Step 4: Check the connection

Finally, we can check if the connection between the MariaDB and PhpMyAdmin containers is working correctly. To do this, we can open a web browser and access http://localhost:8080. This should open PhpMyAdmin in the browser and allow us to access the database created in the MariaDB container.

Leave a Comment