2 Drupal images in a single machine

Hello all :wave:

im trying to find a way to run 2 Drupal containers using a docker compose on a single machine. I am currently having a problem to ‘bind’ the each Drupal storage into a separate location in MySQL.

Here’s the YAML file for Docker Compose:

---
version: '3'

services:
  drupal:
    build: ./
    image: geerlingguy/drupal:latest
    environment:
      DRUPAL_DATABASE_HOST: drupal-mysql
      DRUPAL_DATABASE_PORT: 3306
      DRUPAL_DATABASE_NAME: drupal
      DRUPAL_DATABASE_USERNAME: drupal
      DRUPAL_DATABASE_PASSWORD: drupal
      # Generate a salt with: `php -r "echo bin2hex(random_bytes(25));"`
      DRUPAL_HASH_SALT: db0de8a1556aa5348f87cfc950cd2c9641713d46e9412c8b05
    ports:
      #assign random port on the host
      - "80"
    depends_on:
      - mysql
    restart: always
    # new
    working_dir: /var/www/html
    # Uncomment the volumes line and set to the local path of your Drupal
    # installation, if you need to work with a local codebase.
    volumes:
      # - ~/Sites/drupal-container:/var/www/html:rw,delegated
      - /Sites/drupal-container:/var/www/html:rw,delegated

  drupal2:
    build: ./
    image: geerlingguy/drupal:latest
    environment:
      DRUPAL_DATABASE_HOST: drupal-mysql
      DRUPAL_DATABASE_PORT: 3306
      DRUPAL_DATABASE_NAME: drupal
      DRUPAL_DATABASE_USERNAME: drupal
      DRUPAL_DATABASE_PASSWORD: drupal
      # Generate a salt with: `php -r "echo bin2hex(random_bytes(25));"`
      DRUPAL_HASH_SALT: db0de8a1556aa5348f87cfc950cd2c9641713d46e9412c8b05
    ports:
      #assign random port on the host
      - "80"
    depends_on:
      - mysql
    restart: always
    working_dir: /var/www/html
    volumes:
      - /Sites/drupal-container2:/var/www/html:rw,delegated

  mysql:
    image: mysql:5.7
    container_name: drupal-mysql
    # new
    volumes:
      - /Sites/drupal-container:/var/lib/mysql
    environment:
      MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
      MYSQL_DATABASE: drupal
      MYSQL_USER: drupal
      MYSQL_PASSWORD: drupal
    ports:
      - "3306:3306"

networks:
    # Define the default network
    # Services will use this automatically without having a networks key
    default:
        external:
            name: macvlan_net
```

FYI the network named macvlan_net is defined manually outside of the YAML file. (this allows me to access the Drupal container using IP address)

Problem:
when I run docker-compose up, I get 2 Drupal containers:
Drupal 1 - 132.177.14.1
Drupal 2 - 132.177.14.2

but once i run through the initial setup on Drupal 1, Drupal 2 seems to be mirroring the setup that I made also.

Expected result:
I should be greeted by the initial setup when visiting Drupal 2.

First question: do you want to build an image at this place or start a container with image geerlingguy/drupal from Dockerhub? Then I find no source code for this image, so it is not clear what it is doing. Are you sure you can’t use the official Drupal image? And a 3rd question: For what reason do you use 2 times the same database?

1 Like

Hi Tekki,

first of all, thanks for the reply! I use the image geerlingguy/drupal because I was following a guide… so I guess i can use the official Drupal Image if necessary. The ultimate goal for me is to spin up over 50 Drupal services exclusively for different users… i may be able to use 1 Drupal + 1 Database per container and write it 50 times in the code but im not sure how to code it in the YAML file.

I don’t say that you have to start 50 MySQL containers, just that you can’t connect all Drupal instances to a single database called drupal. It makes sense to have only one DB container running. But it has to contain 50 databases (like drupal01 - drupal50). Read ‘Initializing a fresh instance’ in the docs for the MySQL image, there you see how you can create them with a script.
And don’t forget my first question. The build command builds a new image, I’m sure this is not what you want.