Search code examples
dockerdocker-composetraefik

Multi Docker-Compose projects sharing service


I am attempting to create multiple Docker-Compose projects and have them share a single service, a database server. I'm currently unable to get projects to use my mariadb instance. I'm not sure if my issues is in traefik or docker. I can get wordpress to recognize the database if it is included in the same docker-compose, but as I would ultimately like to not have multiple instances of a database server running, I would prefer it to be it's own project.

# ~/docker/traefik/docker-compose.yml
version: "3.3"

services:

  traefik:
    image: "traefik:v2.2"
    container_name: "traefik"
    command:
      - "--api=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--certificatesresolvers.myresolver.acme.dnschallenge=true"
      - "--certificatesresolvers.myresolver.acme.dnschallenge.provider=namedotcom"
      - "--certificatesresolvers.myresolver.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory"
      - "--certificatesresolvers.myresolver.acme.email=<MY-EMAIL>"
      - "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
    networks:
      t2_proxy:
    ports:
      - "80:80"
      - "443:443"
    environment:
      - "NAMECOM_USERNAME=<My-USERNAME>
      - "NAMECOM_API_TOKEN=<My-Token>
    labels:
      # Dashboard
      - "traefik.enable=true"
      - "traefik.http.routers.traefik.rule=Host(`api.mydomain.com`)"
      - "traefik.http.routers.traefik.service=api@internal"
      - "traefik.http.routers.traefik.middlewares=admin"
      - "traefik.http.routers.traefik.tls.certresolver=myresolver"
      - "traefik.http.routers.traefik.entrypoints=websecure"
      - "traefik.http.middlewares.admin.basicauth.users=myuser:mypasswordhash/"
        # middleware redirect
      - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
      # global redirect to https
      - "traefik.http.routers.redirs.rule=hostregexp(`{host:.+}`)"
      - "traefik.http.routers.redirs.entrypoints=web"
      - "traefik.http.routers.redirs.middlewares=redirect-to-https"
    volumes:
      - "./letsencrypt:/letsencrypt"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

networks:
  t2_proxy:
# ~/docker/db/docker-compose.yml
version: '3.3'

services:

  mysql:
    image: mariadb
    container_name: "mariadb"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example
      MYSQL_DATABASE: wordpressDB
      MYSQL_USER: wordpressUSER
      MYSQL_PASSWORD: wordpressPW
    networks:
      default:
    ports:
      - 3306:3306
  adminer:
    image: adminer
    restart: always
    container_name: "adminer"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.adminer.rule=PathPrefix(`/adminer`)"
      - "traefik.http.routers.adminer.entrypoints=websecure"
      - "traefik.http.routers.adminer.tls.certresolver=myresolver"
    networks:
      - default
      - traefik_t2_proxy
networks:
  default:
  traefik_t2_proxy:
    external: true
# ~/docker/wordpress/docker-compose.yml
version: '3.3'

services:
   wordpress:
     image: wordpress:latest
     restart: always
     container_name: "mySite"
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpressUSER
       WORDPRESS_DB_PASSWORD: wordpressPW
       WORDPRESS_DB_NAME: wordpressDB
     networks:
       traefik_t2_proxy:
       db_default:
     labels:
       - "traefik.enable=true"
       - "traefik.http.routers.wp2.rule=PathPrefix(`/wordpress`)"
       - "traefik.http.routers.wp2.entrypoints=websecure"
       - "traefik.http.routers.wp2.tls.certresolver=myresolver"
volumes:
  db_data: {}
networks:
  traefik_t2_proxy:
    external: true
  db_default:
    external: true

Solution

  • You can have multiple docker-compose files, with the same network and run them together as below: docker-compose -f docker-compose-1.yml -f docker-compose-2.yml up -d But if you need the services to recognize eachother, the networks should be the same. Give your traefik_t2_proxy network to mysql too.