Unir dos contenedores definidos dentro de dos docker-compose.yml distintos usando external_links en Docker Compose

Tiempo de lectura: 2 minutos

Reading time: < 1 minute

Today I’m going to show you how you can connect two containers defined in separate docker-compose.yml files.

We have the following:

File docker-compose-db.yml:

version: "3.1"

services:
  miservicio_mariadb:
    image: mariadb
    container_name: mariadb_container
    env_file:
      - ./Dockerfile/mysql.env
    environment:
      MYSQL_DATABASE: "db"
      MYSQL_USER: "user"
      MYSQL_PASSWORD: "pass"
      MYSQL_ROOT_PASSWORD: "pass_root"
    volumes:
      - ./config/mariadb:/var/lib/mysql
    expose:
      - 3306
    ports:
      - 3306:3306
    networks:
      - docker-network
networks:
  docker-network:
    driver: bridge
    external: true

And the file docker-compose-phpmyadmin.yml:

version: "3.1"

services:
  phpmyadmin:
    image: phpmyadmin
    restart: always
    container_name: phpmyadmin_container
    ports:
      - 8080:80
    environment:
      - PMA_ARBITRARY=1
    external_links: 
      - miservicio_mariadb:db
    networks:
      - docker-network
networks:
  docker-network:
    driver: bridge
    external: true

In this case, I have created these two docker-compose files to connect the database externally with PHPMyAdmin.

First, we run and create the miservicio_mariadb container, and then we run and create the phpmyadmin service.

Through external_links, phpmyadmin will communicate with the created mariadb service.

To execute everything, you need to enter the following in the terminal:

docker-compose -p db-f docker-compose-db.yml up -d
docker-compose -p phpmyadmin -f docker-compose-phpmyadmin.yml up -d

We have the following:

File docker-compose-db.yml:

version: "3.1"

services:
  miservicio_mariadb:
    image: mariadb
    container_name: mariadb_container
    env_file:
      - ./Dockerfile/mysql.env
    environment:
      MYSQL_DATABASE: "db"
      MYSQL_USER: "user"
      MYSQL_PASSWORD: "pass"
      MYSQL_ROOT_PASSWORD: "pass_root"
    volumes:
      - ./config/mariadb:/var/lib/mysql
    expose:
      - 3306
    ports:
      - 3306:3306
    networks:
      - docker-network
networks:
  docker-network:
    driver: bridge
    external: true

And the file docker-compose-phpmyadmin.yml:

version: "3.1"

services:
  phpmyadmin:
    image: phpmyadmin
    restart: always
    container_name: phpmyadmin_container
    ports:
      - 8080:80
    environment:
      - PMA_ARBITRARY=1
    external_links: 
      - miservicio_mariadb:db
    networks:
      - docker-network
networks:
  docker-network:
    driver: bridge
    external: true

In this case, I have created these two docker-compose files to connect the database externally with PHPMyAdmin.

First, we run and create the miservicio_mariadb container, and then we run and create the phpmyadmin service.

Through external_links, phpmyadmin will communicate with the created mariadb service.

To execute everything, you need to enter the following in the terminal:

docker-compose -p db-f docker-compose-db.yml up -d
docker-compose -p phpmyadmin -f docker-compose-phpmyadmin.yml up -d

Leave a Comment