How to Dockerize WordPress with Docker Compose

In this blog post, we will guide you on why and how to Dockerize your WordPress projects using Docker Compose.

docker+wordpress

Containerization has become a major trend in software development as an alternative to virtualization. It involves encapsulating or bundling up software code and all its dependencies so that it can run on any infrastructure consistently. Containerization allows developers to create and deploy applications faster and more securely. Docker is the most popular containerization application used across the developer community. In this tutorial, we will learn how to Dockerize WordPress using Docker Compose. So, let’s get started.

What is Docker?

In simple words, Docker is open-source software that does virtualization faster and lighter. It’s so light that a Docker container can be launched in just a few seconds. And, you can easily run dozens of Docker containers on a single PC. Developers use Docker to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package. By doing so, the developer can rest assured that the application will run on any infrastructure.

Why you need to Dockerize WordPress?

To run WordPress locally, you need Apache/Nginx, MySQL server, PHP, and tons of other dependencies. And deployment to staging or production environment is a nightmare when it comes to managing those dependencies.

So, containerization can help you with this. All you need is Docker – installation itself takes seconds and no additional configuration is required

No matter if it’s a dev machine, Stage, or Live server and what OS it is, Docker works the same everywhere. This means you won’t have to look for bugs that, say, appear in one environment and cannot be reproduced in another.

Install Docker Compose

Prerequisites

You can use Docker Compose to easily run WordPress in an isolated environment built with Docker containers. This guide demonstrates how to use Compose to set up and run WordPress.

Docker Compose relies on Docker Engine, so make sure you have Docker Engine installed. On desktop systems like Docker Desktop for Mac and Windows, Docker Compose is included as part of those desktop installs. However, on Linux systems first, install the Docker Engine by following their official guide.

Install Compose on Linux

Run this command to download the current stable release of Docker Compose:

sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Apply executable permissions to the binary:

sudo chmod +x /usr/local/bin/docker-compose

Test Installation by running version command:

docker-compose --version

Define a Dockerfile for WordPress

The first thing we need to do is to define how our image will look like in a Dockerfile. It is a text file that is added to the directory with the sources of your application.

FROM wordpress:php7.1-apache
COPY . /var/www/html/wordpress

/var/www/html/wordpress” will contain full contents of WordPress along with themes, plugins, and contents. If your repository only contains the theme or plugin then just add the relevant folder.

Create a docker-compose.yml file that starts your WordPress blog and a separate MySQL instance with a volume mount for data persistence:

version: '3.3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     build: .
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress
volumes:
    db_data: {}

Now, run the command in your project directory

docker-compose up -d

This runs docker-compose up in detached mode, pulls the needed Docker images, and starts the WordPress and database containers.

Once the containers have started, you can open the URL in the web-browser and start using your application:

http://localhost:8000

Conclusion

In this article we learned about Docker, Docker-Compose. We also learned how to Dockerize WordPress using Docker-Compose. We introduced you to the concept behind Docker and how you can define simple multi-container applications.