Write down Docker Compose file for Drupal application

Write down Docker Compose file for Drupal application

Here's a simple Docker Compose file to set up a Drupal app with a MySQL database. This example assumes you have a Drupal project in a folder named mydrupal:

version: '3'
services:
  drupal:
    image: drupal:latest
    ports:
      - "8080:80"
    volumes:
      - ./mydrupal:/var/www/html
    environment:
      - MYSQL_DATABASE=drupal
      - MYSQL_USER=drupaluser
      - MYSQL_PASSWORD=drupalpassword
      - MYSQL_HOST=database
    depends_on:
      - database
    restart: always

  database:
    image: mysql:5.7
    environment:
      - MYSQL_DATABASE=drupal
      - MYSQL_USER=drupaluser
      - MYSQL_PASSWORD=drupalpassword
      - MYSQL_ROOT_PASSWORD=rootpassword
    volumes:
      - db_data:/var/lib/mysql
    restart: always

volumes:
  db_data:

Explanation:

  • The drupal service uses the official Drupal image, exposes port 80 on the host machine, mounts the local mydrupal directory to the /var/www/html directory inside the container, and sets environment variables for connecting to the MySQL database.

  • The database service uses the official MySQL 5.7 image, sets up the MySQL database and user for Drupal, and mounts a volume (db_data) to persist MySQL data.

  • The depends_on directive ensures that the database service is started before the drupal service.

Save this Docker Compose file as docker-compose.yml in the same folder as your Drupal project (mydrupal). Next, go to that folder in your terminal and run:

docker-compose up -d

This will start both the Drupal and MySQL containers in detached mode. You can access your Drupal site by visiting http://localhost:8080 in your web browser.

To stop and remove the containers, run:

docker-compose down

Don't forget to change the settings to fit your needs, like changing passwords, volumes, or including more services if necessary.

Did you find this article valuable?

Support LingarajTechhub All About Programming by becoming a sponsor. Any amount is appreciated!