How to Easily Dockerize a Drupal Application: A Step-by-Step Guide
In today’s world of containerized applications, Docker has become an indispensable tool for developers and DevOps teams alike. It offers a way to package and run applications in isolated environments, ensuring consistency across development, testing, and production environments. In this article, we’ll show you the easiest way to Dockerize a Drupal application, making it portable, scalable, and easier to deploy.
By the end of this guide, you will know how to set up a Drupal application with Docker, covering everything from building a Dockerfile to best practices for using Docker Compose in more complex setups.
Why Dockerize Your Drupal Application?
Drupal is a powerful and flexible content management system (CMS) used for building everything from simple websites to large-scale enterprise applications. Dockerizing Drupal offers several key advantages:
Consistency Across Environments: Docker ensures that your Drupal application behaves the same way in every environment.
Portability: Package your Drupal app with all its dependencies, making it easy to run on any machine.
Simplified Dependencies Management: No more manual setup of LAMP/LEMP stacks; Docker handles everything for you.
Scalability: Docker helps in scaling up applications effortlessly by running multiple containers.
Step-by-Step Guide to Dockerizing a Drupal App
1. Create a Basic Drupal Environment
Start by ensuring you have a working Drupal application. If you don’t have one, you can download Drupal from Drupal.org.Once you have the files ready, place them in a project folder. For simplicity, we’ll assume you have the following project structure:
/my-drupal-project
├── drupal/
├── modules/
├── themes/
└── sites/
2. Create a Dockerfile
The Dockerfile is where we define the environment for our Drupal application. Here’s a basic Dockerfile:
# Use an official PHP image as the base image
FROM php:8.1-apache
# Install dependencies and PHP extensions
RUN apt-get update && apt-get install -y \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
libzip-dev \
zip \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install gd zip opcache
# Enable Apache rewrite module
RUN a2enmod rewrite
# Set the working directory inside the container
WORKDIR /var/www/html
# Copy the current directory contents into the container
COPY ./drupal/ /var/www/html/
# Set the right permissions for Apache
RUN chown -R www-data:www-data /var/www/html
# Expose port 80 for the web server
EXPOSE 80
# Start Apache server
CMD ["apache2-foreground"]
This Dockerfile does the following:
Uses PHP with Apache:
The base image is a PHP/Apache combo, which is ideal for running a Drupal environment.
Installs PHP extensions:
Drupal requires extensions like gd for image processing and zip for handling compressed files.
Enables Apache Rewrite Module:
This is necessary for Drupal’s clean URLs.
Copies Drupal files:
The source files from the local drupal/ folder are copied to the web root inside the container.
Sets permissions:
Ensures the correct ownership for the web server.
Exposes port 80:
Makes the container accessible via port 80.
3. Create a docker-compose.yml File
For a typical Drupal setup, you’ll need a web server, PHP, and a database. Docker Compose simplifies managing these multiple containers. Here’s a basic docker-compose.yml file:
version: '3.8'
services:
drupal:
build: .
ports:
- "8080:80"
volumes:
- ./drupal:/var/www/html
depends_on:
- db
environment:
- DRUPAL_DB_HOST=db
- DRUPAL_DB_NAME=drupal
- DRUPAL_DB_USER=drupal
- DRUPAL_DB_PASSWORD=drupal
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: drupal
MYSQL_USER: drupal
MYSQL_PASSWORD: drupal
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
Drupal Service:
Builds the drupal image, exposing it on port 8080. It also defines the database credentials.
Database Service:
Uses the official MySQL image to create a database for Drupal with persistent storage in the db-data volume.
4. Add a .dockerignore File
Just like .gitignore, the .dockerignore file specifies files and directories you don’t want to copy into your Docker image. Create a .dockerignore file with the following contents:
.git
sites/default/files
vendor
node_modules
This will exclude Git files, uploaded files, and unnecessary directories from being copied into the container.
5. Build and Run the Docker Containers
Now that you have both a Dockerfile and a docker-compose.yml file, you’re ready to build and run your Docker containers.First, build the Docker image:docker-compose buildOnce the image is built, you can start the Drupal app using:docker-compose upThis will spin up the drupal and db containers. You can access your Drupal site by navigating to http://localhost:8080.
6. Install Drupal
After running the containers, visit http://localhost:8080 and you will be greeted by the Drupal installation screen. Here’s how to set it up:
Select your preferred language.
Choose the standard installation profile.
For the database configuration, use the following credentials (as defined in the docker-compose.yml):
Database Name: drupal
Database Username: drupal
Database Password: drupal
Database Host: db
Finish the installation, and you’re ready to go!
Pros of Dockerizing a Drupal Application
Environment Consistency: Docker ensures your Drupal app runs in the same environment, regardless of the host system, eliminating “works on my machine” issues.
Simplified Dependencies: No need to manually install PHP, Apache, MySQL, or extensions — Docker handles it all.
Portability: With Docker, you can easily move your Drupal application between machines or deploy it to any cloud provider.
Easier Scaling: Docker enables you to quickly scale your Drupal app by deploying multiple containers, making it easy to handle higher traffic.
Improved Development Workflow: With Docker Compose, you can spin up an entire Drupal environment, complete with a database, in seconds, reducing the setup time for new developers.
Cons of Dockerizing a Drupal Application
Learning Curve: For those new to Docker, the setup can seem complex, especially managing multiple containers like databases and caching layers.
Performance Overhead: While Docker is lightweight compared to virtual machines, there’s still some overhead, especially with file I/O operations in Drupal.
Database Management: You need to manage database backups and persistence carefully, as Docker containers are ephemeral by default.
Security Concerns: Misconfiguring your Docker setup, such as running containers as root, can introduce security vulnerabilities.
Resource Usage: Docker containers, while efficient, still consume system resources. Running too many containers may lead to performance issues, especially on lower-spec machines.
Conclusion: Why Dockerize Your Drupal App?
Dockerizing your Drupal application brings numerous benefits, from consistency across environments to simplified deployment and scaling. By following the steps outlined in this guide, you can create a fully Dockerized version of your Drupal app, making it easier to manage and deploy in any environment.Using Docker Compose, you can easily integrate a MySQL database, and by fine-tuning your Docker setup with a production-ready environment, you can confidently deploy Drupal apps at scale.
Ready to get started? Dockerize your Drupal app today and unlock the power of containerization!
By Dockerizing Drupal, you’re embracing modern development and deployment practices. Whether you’re working on a small personal project or a large enterprise CMS, Docker can streamline your workflow and make your Drupal app more portable, scalable, and secure.
Start Dockerizing your Drupal site today!
Kiran Chaulagain
kkchaulagain@gmail.com
I am a Full Stack Software Engineer and DevOps expert with over 6 years of experience. Specializing in creating innovative, scalable solutions using technologies like PHP, Node.js, Vue, React, Docker, and Kubernetes, I have a strong foundation in both development and infrastructure with a BSc in Computer Science and Information Technology (CSIT) from Tribhuvan University. I’m passionate about staying ahead of industry trends and delivering projects on time and within budget, all while bridging the gap between development and production. Currently, I’m exploring new opportunities to bring my skills and expertise to exciting new challenges.