Docker: How do I refer a service defined in a container in another?

Asked

Viewed 40 times

1

I have some sites in a VPS running with Docker-Compose, on the same network and using proxy. The structure is more or less this:

.
├── nginx-proxy
│   └── docker-compose.yml
└── blogs
    └── docker-compose.yml

Inside the blogs folder I have this setting:

version: "3"

services:
  db:
    container_name: ${CONTAINER_DB_NAME}
    image: mariadb:latest
    restart: unless-stopped
    volumes:
      - ${DB_PATH}:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}

  wordpress1:
    depends_on:
      - db
    container_name: ${CONTAINER_WP_NAME1}
    image: wordpress:latest
    restart: unless-stopped
    volumes:
      - ${WP_CORE}:/var/www/html
      - ${WP_CONTENT}:/var/www/html/wp-content
      - ./conf.d/php.ini:/usr/local/etc/php/conf.d/php.ini
    environment:
      WORDPRESS_DB_HOST: ${CONTAINER_DB_NAME1}:3306
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE1}
      WORDPRESS_DB_USER: ${MYSQL_USER1}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD1}
      WORDPRESS_TABLE_PREFIX: ${WORDPRESS_TABLE_PREFIX1}
      VIRTUAL_HOST: ${DOMAINS1}
      LETSENCRYPT_HOST: ${DOMAINS1}
      LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL1}
    logging:
      options:
        max-size: ${LOGGING_OPTIONS_MAX_SIZE1:-200k}

  wordpress2:
    depends_on:
      - db
    container_name: ${CONTAINER_WP_NAME2}
    image: wordpress:latest
    restart: unless-stopped
    volumes:
      - ${WP_CORE}:/var/www/html
      - ${WP_CONTENT}:/var/www/html/wp-content
      - ./conf.d/php.ini:/usr/local/etc/php/conf.d/php.ini
    environment:
      WORDPRESS_DB_HOST: ${CONTAINER_DB_NAME2}:3306
      WORDPRESS_DB_NAME: ${MYSQL_DATABASE2}
      WORDPRESS_DB_USER: ${MYSQL_USER2}
      WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD2}
      WORDPRESS_TABLE_PREFIX: ${WORDPRESS_TABLE_PREFIX2}
      VIRTUAL_HOST: ${DOMAINS2}
      LETSENCRYPT_HOST: ${DOMAINS2}
      LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL2}
    logging:
      options:
        max-size: ${LOGGING_OPTIONS_MAX_SIZE2:-200k}

networks:
  default:
    external:
      name: ${NETWORK}

Everything is working fine, but I want to refactor this so it becomes easier to maintain over time. The idea would be to remove the mysql service statement from the above file as well as the blog statements. Then I would create a directory with the configuration for the DB service, another for Blog1 and another for blog2. Example:

.
├── db
│   └── docker-compose.yml
├── blog1
│   └── docker-compose.yml
├── blog2
│   └── docker-compose.yml
└── blog3
    └── docker-compose.yml

db container

version: "3"

services:
  db:
    container_name: ${CONTAINER_DB_NAME}
    image: mariadb:latest
    restart: unless-stopped
    volumes:
      - ${DB_PATH}:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
networks:
  default:
    external:
      name: ${NETWORK}

Blog1 container

version: "3"

services:
  wordpress1:
    depends_on:
      - db // Isto funciona? O Docker saberá
           // que este é o serviço mysql que
           // consta no outro diretório?

    container_name: ${CONTAINER_WP_NAME1}
    image: wordpress:latest
    restart: unless-stopped
    volumes:
        - ${WP_CORE}:/var/www/html
        - ${WP_CONTENT}:/var/www/html/wp-content
        - ./conf.d/php.ini:/usr/local/etc/php/conf.d/php.ini
    environment:
        WORDPRESS_DB_HOST: ${CONTAINER_DB_NAME1}:3306
        WORDPRESS_DB_NAME: ${MYSQL_DATABASE1}
        WORDPRESS_DB_USER: ${MYSQL_USER1}
        WORDPRESS_DB_PASSWORD: ${MYSQL_PASSWORD1}
        WORDPRESS_TABLE_PREFIX: ${WORDPRESS_TABLE_PREFIX1}
        VIRTUAL_HOST: ${DOMAINS1}
        LETSENCRYPT_HOST: ${DOMAINS1}
        LETSENCRYPT_EMAIL: ${LETSENCRYPT_EMAIL1}
    logging:
        options:
        max-size: ${LOGGING_OPTIONS_MAX_SIZE1:-200k}
networks:
  default:
    external:
      name: ${NETWORK}

... and another for blog2, blog3 and so on.

Now comes the question: how do I refer an external service to the current configuration file? In other words, how do I include db as dependency in each blog configuration file?

Any help would be very welcome. Thank you!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.