Dockerize your Drupal
Search Menu

How to set up docker for your Drupal project

How to set up Redis with Docker and Drupal

Dark Light
Our comprehensive tutorial will guide you through dockerizing your Drupal environment, and easily deploying your app across multiple platforms.

Are you tired of spending countless hours setting up your Drupal environment? Do you wish there was an easier way to manage dependencies and ensure consistency across different environments? Look no further than Docker! In this comprehensive tutorial, we will guide you through the steps of dockerizing your Drupal environment. With Docker, you can simplify the setup process, reduce development time and easily deploy your application across multiple platforms. So sit back, grab a coffee and let’s get started!

Setting Up Your Docker Environment

Installing Docker:
Before you can begin using Docker, you’ll need to install it on your development machine. The official website for Docker has installation instructions for a variety of different operating systems: https://docs.docker.com/installation/.

Now you need the docker-compose.yml file in the root of your project. This file tells the docker system with containers you need to build and the correct configuration for your project. Below you find an example of a docker-compose file. The services key contains the container. In this example, you have a web, a database, a phpmyadmin and redis containers. The web container calls the Dockerfile to install its set up.

#docker-compose.yml

version: "3.9"
volumes:
  db-volume:
  redis-cache:
services:
  web:
    links:
      - database:localhost
    build:
      context: .
      dockerfile: Dockerfile
      args:
        - env=local
    volumes:
      - .:/var/www/html
      - ~/.composer:/home/php/.composer:rw
    ports:
      - 80:80
    environment:
      ENV: local
      DATABASE_NAME: kaiacode
      DATABASE_USERNAME: kaiacode
      DATABASE_PASS: kaiacode
      DATABASE_PORT: 3306
      DATABASE_HOST: host.docker.internal
      COMPOSER_MEMORY_LIMIT: -1
    restart: always
  database:
    image: mariadb:10.6.10
    ports:
      - 3306:3306
    command:
      - --max_allowed_packet=32505856
      - --disable-log-bin
      - --transaction-isolation=READ-COMMITTED
    restart: always
    environment:
      - MYSQL_DATABASE=kaiacode
      - MYSQL_USER=kaiacode
      - MYSQL_PASSWORD=kaiacode
      - MYSQL_ALLOW_EMPTY_PASSWORD=1
    volumes:
      - db-volume:/var/lib/mysql:delegated
  phpmyadmin:
    image: phpmyadmin
    restart: always
    links:
      - database:database
    ports:
      - 8000:80
    environment:
      - PMA_HOST=database
      - MYSQL_DATABASE=kaiacode
      - MYSQL_USER=kaiacode
      - MYSQL_PASSWORD=kaiacode
  redis:
    image: redis:6.2.7
    restart: always
    volumes:
      - redis-cache:/data:delegated

The next step is to create your Dockfile with the set-up to your web container. In this example, we will start with an image that contains apache, php8.1. From there, we will install many libraries that we need to run Drupal. We also add an example that if the env argument set in the docker-compose file is equal to local, we will install x-debug in addition.

#Dockfile
# Extend from preconfigured image
# @see https://hub.docker.com/_/drupal/tags
# @see https://www.drupal.org/requirements/php#drupalversions
FROM drupal:php8.1-apache
ARG env
#To allow install php package https://hub.docker.com/_/php
RUN rm /etc/apt/preferences.d/no-debian-php
#Build image
RUN echo "Install basic libraries"
RUN a2enmod rewrite
# Set default timezone
RUN ln -fs /usr/share/zoneinfo/America/Toronto /etc/localtime &&  \
    dpkg-reconfigure --frontend noninteractive tzdata
# Install apachetop for debugging
RUN apt-get update &&  \
    apt-get install -y apachetop libcurl4-openssl-dev
# Install the PHP extensions we need
RUN apt-get update &&  \
    apt-get install -y wget vim git mariadb-client libzip-dev libldap2-dev parallel zip unzip apachetop libcurl4-openssl-dev
# Install lib to gb
RUN apt-get update &&  \
    apt-get install -y libpng-dev libjpeg-dev libonig-dev
# Configure PHP GD extension
RUN docker-php-ext-configure gd --with-jpeg
# Install PHP extensions
RUN docker-php-ext-install gd curl mbstring opcache pdo pdo_mysql zip
# Configure and install PHP LDAP extension
RUN docker-php-ext-configure ldap
RUN docker-php-ext-install ldap

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Clean build dependencies
RUN rm -rf /var/lib/apt/lists/*

#Installation ImageMagick
RUN apt-get update &&  \
    apt-get install -y imagemagick ghostscript libmagickwand-dev fonts-droid-fallback --no-install-recommends
RUN pecl install imagick
RUN docker-php-ext-enable imagick

#Installation of Redis
RUN pecl install redis \
    && docker-php-ext-enable redis

#Copy server config files
RUN rm /etc/apache2/sites-available/000-default.conf
COPY /docker/config/000-default.conf /etc/apache2/sites-available/000-default.conf
# set recommended PHP.ini settings
COPY docker/config/php.ini /usr/local/etc/php/php.ini
# see https://secure.php.net/manual/en/opcache.installation.php
COPY docker/config/opcache.ini /usr/local/etc/php/conf.d/opcache-recommended.ini

RUN echo "Update PATH variable to include Composer binaries."
ENV PATH "/root/.composer/vendor/bin:$PATH"

# Build GCS
# Copy settings
WORKDIR /var/www/html
COPY . /var/www/html
RUN chown -R www-data:www-data /var/www/html

#composer install -n --prefer-dist --no-dev
RUN composer install --optimize-autoloader
#RUN composer install -n --prefer-dist --no-dev

# Configure drush
RUN ln -sf /var/www/html/vendor/bin/drush /usr/bin/drush

# Config for local env
RUN \
if [ "$env" = "local" ]; \
    then echo "Installation of XDebug"; \
         cp /var/www/html/docker/config/drupal-*.ini /usr/local/etc/php/conf.d/; \
         pecl install xdebug-3.1.5; \
         docker-php-ext-enable xdebug; \
         mkdir /var/run/secrets; \
         mkdir /var/run/secrets/vdmtl; \
         mkdir /var/run/secrets/vdmtl/redis; \
         mkdir /var/log/xdebug; \
         echo '' > /var/run/secrets/vdmtl/redis/password; \
         echo 'redis' > /var/run/secrets/vdmtl/redis/host; \
         echo '6379' > /var/run/secrets/vdmtl/redis/port; \
         curl -fsSL https://deb.nodesource.com/setup_16.x | bash -; \
         apt-get update; \
         apt-get install -y nodejs chromium; \
fi;

RUN echo "CMD apachectl -D FOREGROUND"
CMD apachectl -D FOREGROUND

Some configurations like the vhost is easier to use an external file and copy then in the right folder. You can see in the Dockerfile that we will copy some configuration file like 000-default.conf to our container. They are all located in the docker/config folder from the root of your Drupal project.

#docker/config/000-default.conf

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/web
        Alias /gcs /var/www/html/web
        <Directory /var/www/html/web>
                Options Indexes FollowSymLinks
                AllowOverride All
                Order Allow,Deny
                Allow from All
                RewriteEngine On
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteRule ^ index.php [L]
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        RewriteEngine On
        RewriteCond %{HTTP:Authorization} ^(.*)
        RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
</VirtualHost>
#docker/config/php.ini
upload_max_filesize = 150M
post_max_size = 250M
max_execution_time = 0
memory_limit = 1000M
session.cookie_secure = 1
allow_url_fopen = On
#docker/config/opcache.ini
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
#docker/config/drupal-xdebug.ini
xdebug.client_port = 9000
xdebug.discover_client_host=1
xdebug.idekey = "PHPSTORM"
xdebug.log="/var/log/xdebug/xdebug.log"
xdebug.max_nesting_level = 1000
xdebug.mode=debug
xdebug.client_host=host.docker.internal
#docker/config/drupal-x-error.ini
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
ignore_repeated_errors = Off
ignore_repeated_source = Off

Those are all the files that you need. Now it is time to fire up your environment. First you need to build the container by executing from your terminal the following command:

$ docker-compose build

The first time, this step might take a little while, because of your docker system needs to download all the necessary images. Once the build is done, you can bring up the containers by executing the following command:

$ docker-compose up

When it is done, you should see all the containers on your docker desktop. You can now call composer from your docker by using the following command:

$ docker-compose exec web composer install

This command tells docker to execute the command composer install within the container web.

In order to connect your Drupal project to the database you can set the host to host.docker.internal and the credentials you use to set the database in the docker-compose file (MYSQL_USER, MYSQL_PASSWORD, and MYSQL_DATABASE). In this example, all three variables were set up as kaiacode. You can access the database from your favourite database IDE by connecting through TCP using localhost as the host and the same credentials.

For more details about your docker-compose file. You can find it on the docker https://docs.docker.com/get-started/.

Conclusion

In conclusion, Dockerizing your Drupal environment can be a great way to streamline the development process and make it easier to work with different versions of Drupal. This tutorial has provided you with all the information you need in order to get started on this task and make sure that your environment is correctly configured for optimal performance. We hope that by using these tips, you will be able to successfully dockerize your own Drupal environment!

Related Posts
Total
0
Share