Docker4drupal: How to: Multiple projects simultaniously

Created on 7 Apr 2017  路  26Comments  路  Source: wodby/docker4drupal

In the documentation I read that running multiple projects in parallel should not be a problem. So what I did is to only change the traffic settings. The two projects look like this:

Project 1:

  traefik:
    image: traefik
    restart: unless-stopped
    command: -c /dev/null --web --docker --logLevel=INFO
    ports:
      - '8000:80'
      - '8080:8080'
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

Project 2:

  traefik:
    image: traefik
    restart: unless-stopped
    command: -c /dev/null --web --docker --logLevel=INFO
    ports:
      - '8001:80'
      - '8081:8080'
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

I can start them both but they are not responsive. When I go to either http://drupal.docker.localhost:8000 or http://drupal.docker.localhost:8001 it takes a while and then I get a gateway timeout.

Is there anything else I need to adjust?

Most helpful comment

Above urls of documentation did not work anymore, here is a working one:
https://wodby.com/docs/stacks/drupal/local/#running-multiple-projects

All 26 comments

Do you use docker-sync as an external volume? If yes, try to rename the volume for one of the projects

I'm on Linux (Ubuntu 16.04) and I'm not using docker-sync. Anything else I could try?

I've updated instructions for multiple projects http://docs.docker4drupal.org/en/latest/multiple-projects/

@csandanov thanks a lot, I'm starting to get the idea. A couple of pieces seem to be missing though. Let's have a look into the nginx service definitions:

  nginx:
    image: wodby/drupal-nginx:6-1.10-2.0.0
    restart: unless-stopped
    depends_on:
      - php
    environment:
      NGINX_BACKEND_HOST: php
    volumes:
      - ./:/var/www/html
    labels:
      - 'traefik.backend=nginx'
      - 'traefik.port=80'
      - 'traefik.frontend.rule=Host:drupal.docker.localhost'

Changing the traefik.backend label is documented and I understand what that's for.

I wonder if I also need to modify the traefik.frontend.rule? Currently all projects are configured to use the same domain drupal.docker.localhost but then I can't imagine how traefik should be able to route traffic to the right nginx container. Does each project require an individual domain name then?

And another question is about the dependencies, here e.g. the php container. Do I have to name those containers differently for each project as well? Or does docker-compose handle that such that the PHP container in project 1 is only known to other containers in the same project?

@jurgenhaas you should update traefik.backend label as described in the instructions to make the backend for traefik unique per project. We add stand-alone traefik container to networks of each project.

Great, thanks. I've built a Composer plugin around this to easily configure our internal development environments for each project and this is working really nicely these days.

Would it work if other projects are not using traefik for reverse proxy?
For example if you have docker containers that were not put together with docker4drupal or even non drupal projects in docker containers.
What settings should we put in traefik.yml file to properly route local domain hosts to those project while keeping them all running at the same time and on the same port (80 or 8000)?

@alesrebec sorry, I don't get your use case, sounds like a question about how traefik works, I recommend to read their docs https://docs.traefik.io/, it's quite simple

I need to get back to this one as I'm running into trouble when multiple projects are up at once. In general, it works really well. I do have a stand-alone container for Traefik and then for each Drupal project a set of containers that do the actual work. My problem is that every other request to one of the development sites gets delivered by a different set of containers.

Here is the setup:

Traefik:

version: '2'
services:
  traefik:
    image: traefik
    restart: unless-stopped
    command: '-c /dev/null --web --docker --logLevel=DEBUG'
    networks:
      - project1
      - project2
    ports:
      - '8000:80'
      - '8080:8080'
    volumes:
      - '/var/run/docker.sock:/var/run/docker.sock'
networks:
  project1:
    external:
      name: project1_default
  project2:
    external:
      name: project2_default

Then, each of the two projects have a compose file with the Nginx configured like this:

  nginx:
    image: wodby/drupal-nginx:8-1.10-2.0.0
    restart: unless-stopped
    depends_on:
      - php
    environment:
      NGINX_BACKEND_HOST: php
      NGINX_SERVER_ROOT: /var/www/html/web/
    volumes:
      - ./:/var/www/html
    labels:
      - 'traefik.backend=nginx'
      - 'traefik.port=80'
      - 'traefik.frontend.rule=Host:project1.docker.localhost'

Now, if all three environments are up and running and I go to http://project1.docker.localhost:8000 it does happen that the Nginx from project2 is handling that request. I was under the impression that with this setup, Traefik would proxy each such requests to project1 always. Any idea what could be wrong?

@jurgenhaas you should change 'traefik.backend=nginx' to 'traefik.backend=project1_nginx_1' if the folder where you have the docker-compose.yml file is named 'project1'.
Same for 'project2', you need to have 'traefik.backend=project2_nginx_1' as traefik.backend and of course a different traefik.frontend.rule host.
I made it work, but the documentation is not clear enough to set it up the first time.

Oh great. Yes that solves the problem. Thanks @alesrebec for pointing that out.

@alesrebec we'll be glad if you suggest improvements for these instructions, I agree it might be better

OK, here it is for review. Updated multi-project doc page.
https://github.com/alesrebec/docker4drupal/blob/alesrebec-patch-1/docs/multiple-projects.md

Pull Request created.

@alesrebec thanks so much for your input!

For those looking for the current location of the relevant docs page: https://docker4drupal.readthedocs.io/en/latest/multiple-projects/

Thanks all for the instructions, but won't this break CI and multi-developer workflows? It may be convenient for me to deploy a multi-site traefik container with a .yml file in a parent directory, but that directory exists outside of the git repo of each site. When deploying the site through CI, I won't necessarily want to use a multi-site hierarchy and if I share the repo with other project devs, they will need to configure a unique version of docker-compose and then traefik.yml.

I would prefer that these instructions offer the current solution and also a self-contained solution where multiple sites can be run in parallel even if doing so has performance compromises. For example, generating a unique container for each site.

It appears that this can be accomplished by editing all instances of traefik.frontend.rule to xxx.sitename.docker.localhost and then incrementing all port mappings by one.

@jraviotta I think this is not partially true.

I have multiple projects and only one traefik.yml file, that I never edit after initial setup.

My traefik.yml:

version: '2'

services:
  work_traefik:
    image: traefik
    restart: unless-stopped
    command: -c /dev/null --web --docker --logLevel=DEBUG
    networks:
      - codebase
    ports:
      - '80:80'
      - '443:443'
      - '8080:8080'
      - '3306:3306'
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

networks:
  codebase:
    external:
      name: codebase_default

and each of my projects comes in structure:

- project1
-- codebase
--- docker-compose.yml
- project2
-- codebase
--- docker-compose.yml
- project3
-- codebase
--- docker-compose.yml
- project4
-- codebase
--- docker-compose.yml

Each of the projects falls under same network afterwards. and it makes no problem to use it that way.

@irmantasp thanks for the suggestion. I'll try it out and report back.

Is there a way to have a custom domain for development environments, rather than always use http://x.docker.localhost ? For example http://x.projectname.dev ?
extending traefik container command to include --docker.domain=projectname.dev does not help...

@irmantasp where is your traefik.yml: relative to the projects?

Projects

  • traefik.yml
  • project1
    -- codebase
    --- docker-compose.yml
  • project2
    -- codebase
    --- docker-compose.yml
  • project3
    -- codebase
    --- docker-compose.yml
  • project4
    -- codebase
    --- docker-compose.yml

or
Projects

  • project1
    -- codebase
    --- docker-compose.yml
    --- traefik.yml
  • project2
    -- codebase
    --- docker-compose.yml
    --- traefik.yml
  • project3
    -- codebase
    --- docker-compose.yml
    --- traefik.yml
  • project4
    -- codebase
    --- docker-compose.yml
    --- traefik.yml

@jraviotta First one, My traefik.yml is in parent folder for all of others.

@irmantasp you can set the 'domain' in your docker-compose, in the traefik.frontend.rule line.
e.g. - 'traefik.frontend.rule=Host:mylocalsite.dev'
I haven't changed a thing in my traefik.yml

edit:
You do need to add the IP address to /etc/hosts, otherwise it doesn't work (and on second thought: I think that's the problem you're describing)

current location of docs: https://docs.wodby.com/stacks/drupal/local/#running-multiple-projects

Above urls of documentation did not work anymore, here is a working one:
https://wodby.com/docs/stacks/drupal/local/#running-multiple-projects

Hi. Anybody knows what's hostname should I use to connect to the drupal from another container in case when I'm using Single port method (https://wodby.com/docs/stacks/drupal/local/#single-port) for the traefik.

Previously in standard docker4drupal I've used curl http://nginx to connect to the drupal from my testing container, but with multiproject setup it seems it doesn't work.

Heh) I should anwer myself. I forgot to add (I'm using vanilla drupal) ^nginx$ entry to the trusted_host_patterns param within settings.php

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rsanzante picture rsanzante  路  7Comments

quietone1 picture quietone1  路  10Comments

Vacilando picture Vacilando  路  9Comments

jzavrl picture jzavrl  路  3Comments

suricactus picture suricactus  路  9Comments