Setting up docker4drupal with multiple projects on Mac – Redo

Since writing Setting up docker4drupal with multiple projects on Mac I have discovered a better way.

Træfik container

Create a working folder for Træfik:

mkdir ~/Sites/traefik
cd ~/Sites/traefik

Create wildcard cert files for *.docker.localhost (If you have not setup Dnsmasq locally I recommend it)

mkdir certs
cd certs
openssl genrsa 2048 > traefik.key
openssl req -new -x509 -nodes -sha1 -days 3650 -key traefik.key > host.cert
#[enter *.docker.localhost for the Common Name]
openssl x509 -noout -fingerprint -text < traefik.cert > traefik.info
cat traefik.cert traefik.key > traefik.pem
Trust cert

sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain traefik.cert
cd ~/Sites/traefik

Make the Docker-compose.yml file with https support

version: '2'
services:
    traefik:
        image: traefik:1.3.6
        restart: unless-stopped # you can use always if you choose, I perfer unless-stopped locally
        command: '-c /dev/null --web --docker --logLevel=DEBUG --defaultEntryPoints=''http'',''https'' --entryPoints="Name:https Address::443 TLS:/certs/traefik.cert,/certs/traefik.key" --entryPoints="Name:http Address::80"'
        networks:
            - webgateway
        ports:
            - '80:80'
            - '443:443'
            - '8080:8080'
        volumes:
            - '/var/run/docker.sock:/var/run/docker.sock'
            - './certs:/certs'
    portainer:
        image: portainer/portainer
        restart: unless-stopped
        networks:
            - webgateway
        command: '--no-auth'
        volumes:
            - '/var/run/docker.sock:/var/run/docker.sock'
        labels:
            - traefik.backend=portainer
            - traefik.port=9000
            - 'traefik.frontend.rule=Host:portainer.docker.localhost'
networks:
    webgateway:
        driver: bridge

Start your container. Once it is running it will keep going even after reboot.

docker-compose up -d

Docker 4 Drupal/Wordpress

In each project add the following code for each container:

    networks:
      - web

And one global setting:

# Add networks section
networks:
  web:
    external:
      name: traefik_webgateway

Example: (Assumes the folder is called drupal i.e. /Sites/drupal

version: "2"

services:
  mariadb:
    image: wodby/mariadb:10.1-2.3.5
    networks:
      - web
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: drupal
      MYSQL_USER: drupal
      MYSQL_PASSWORD: drupal
#    volumes:
#      - ./mariadb-init:/docker-entrypoint-initdb.d # Place init .sql file(s) here.
#      - /path/to/mariadb/data/on/host:/var/lib/mysql # I want to manage volumes manually.

  php:
# 1. Images with vanilla Drupal – wodby/drupal:[DRUPAL_VERSION]-[PHP_VERSION]-[STABILITY_TAG].
    image: wodby/drupal:8-7.1-3.0.0
    networks:
      - web
    environment:
      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
      PHP_FPM_CLEAR_ENV: "no"
      DB_HOST: mariadb
      DB_USER: drupal
      DB_PASSWORD: drupal
      DB_NAME: drupal
      DB_DRIVER: mysql
#      PHP_XDEBUG: 1
#      PHP_XDEBUG_DEFAULT_ENABLE: 1
#      PHP_XDEBUG_REMOTE_CONNECT_BACK: 0
#      PHP_XDEBUG_REMOTE_HOST: "10.254.254.254"
#      PHP_XDEBUG_PROFILER_OUTPUT_DIR: /mnt/files/xdebug/profiler
#      PHP_XDEBUG_TRACE_OUTPUT_DIR: /mnt/files/xdebug/traces
    volumes:
      - codebase:/var/www/html
## Options for macOS users (https://docker4drupal.readthedocs.io/en/latest/macos)
#      - codebase:/var/www/html:cached # User-guided caching
#      - docker-sync:/var/www/html # Docker-sync
## For Xdebug profiler files
#      - files:/mnt/files

  nginx:
# wodby/drupal-nginx:[DRUPAL_VERSION]-[NGINX_VERSION]-[STABILITY_TAG].
    image: wodby/drupal-nginx:8-1.13-3.0.1
    networks:
      - web
    depends_on:
      - php
    environment:
      NGINX_STATIC_CONTENT_OPEN_FILE_CACHE: "off"
      NGINX_ERROR_LOG_LEVEL: debug
      NGINX_BACKEND_HOST: php
      NGINX_SERVER_ROOT: /var/www/html/web
    volumes:
      - codebase:/var/www/html
# Options for macOS users (https://docker4drupal.readthedocs.io/en/latest/macos)
#      - codebase:/var/www/html:cached # User-guided caching
#      - docker-sync:/var/www/html # Docker-sync
    labels:
      - 'traefik.backend=drupal_nginx_1'
      - 'traefik.port=80'
      - 'traefik.frontend.rule=Host:drupal.docker.localhost'

  mailhog:
    image: mailhog/mailhog
    networks:
      - web
    labels:
      - 'traefik.backend=drupal_mailhog_1'
      - 'traefik.port=8025'
      - 'traefik.frontend.rule=Host:mailhog.drupal.docker.localhost'

#   portainer: is not needed in each project

# traefik: is not needed in each project

# Add networks section
networks:
  web:
    external:
      name: traefik_webgateway

volumes:
  codebase:
   docker-sync:
     external: true

Using DNSmasq locally will allow Safari and Firefox to work for the docker.localhost sites.