Setting up Docker4Drupal/Docker4WordPress with multiple projects on Mac – Redo

Edited 2018-01-24 to switch to .test for local

DNS name docker.for.mac.host.internal shoud be used instead of docker.for.mac.localhost (still valid) for host resolution from containers, since since there is an RFC banning the use of subdomains of localhost. See https://tools.ietf.org/html/draft-west-let-localhost-be-localhost-06.

#####

Since writing Setting up Docker4Drupal with multiple projects on Mac I have discovered a better way. Træfik allows for automatic detection, so you can leave it running in it’s own container. With a few changes to Docker4Drupal and Docker4Wordpress YAML files you can start and stop your development sites without having to reconfigure Træfik.

Træfik container

Create a working folder for Træfik:

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

Create wildcard cert files for *.test (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 *.test 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.test'
networks:
    webgateway:
        driver: bridge

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

docker-compose up -d

Docker4Drupal/Docker4Wordpress

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.test'

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

#   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 test sites.

EDIT: Download docker-compose.yml on GitHub