Deploy WordPress using Docker Compose

Docker is awesome! Nowadays containers are everywhere. From startups to huge companies, everyone uses it more or less.

On the other hand, WordPress is one of the most popular Content Management Systems (CMS) due to the fact is user-friendly, is flexible and has tons of nice features. There are lots of freelancers worldwide developing themes, plugins, widgets and even new core functions. And where is development, there is the need for environment setup.

Imagine you are a WordPress developer working on three or four different things at the same period. Obviously, you will need three or four different instances of WordPress, databases, dependencies installed locally. So, your development machine will become a mess

Here enters in stage Docker. This awesome tool provides the possibility of isolation for each piece of software you want. In addition, docker compose offers the possibility of sticking together some pieces of software that works together.

In this tutorial, I will show you how to setup WordPress in Docker Compose, for fast and easy deploy and to reduce the time allocated for environment configuration.

Install Docker & Docker Compose

If you are a Mac user, follow these instructions.

Docker installation:

# Debian based systems
$ sudo apt-get update 
$ sudo apt-get install curl
$ curl -fsSL | sh

# CentOS
$ sudo yum update
$ sudo yum install curl
$ curl -fsSL | sh

After installation, you need to add your user to the Docker group. Doing this allows you to run Docker commands without needing to invoke sudo every time.

$ sudo usermod -aG docker $USER

Compose installation:

If you are a Mac user, docker compose is installed when you install Docker. So you don’t need to do anything on this step (yey).

$ sudo su -
# curl -L`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose
# exit

To install it you need to become root, to download the latest version in /usr/local/bin and set execution permissions for binary.

WordPress setup in compose:

First of all, create the workspace:

$ mkdir -p ~/Desktop/compose-wordpress
$ cd ~/Desktop/compose-wordpress
$ mkdir -p data_dir
$ mkdir wordpress
$ touch docker-compose.yml

The purpose of this structure is:

  • directory data_dir will be used for database persistence.
  • wordpress directory will be used to store wp files. It will be mounted as a volume inside WordPress container.
  • docker-compose.yml file will contain the instructions for docker.

Now, download the latest version of docker and unzip it into the wordpress directory. This directory will be mounted as volume inside WordPress container and this approach is flexible enough for development because you can edit any file at any time.

Docker compose instructions:

Our setup contains a database container and a WordPress container. For the database, I choose mysql version 5.7 and for WordPress, I used the latest version. WordPress container depends on the database container and maps port 80 of the container to port 8000 of the host, so WordPress will be accessible in the browser on http://localhost:8000.

The environment for db container means:

  • MYSQL_ROOT_PASSWORD -> MySQL root password. The container will not start if this variable is not set;
  • MYSQL_DATABASE -> name for the database used by WordPress;
  • MYSQL_USER -> user of the MYSQL_DATABASE;
  • MYSQL_PASSWORD -> password for the database;

The environment for wordpress container means:

  • WORDPRESS_DB_HOST: db:3306 // name of the database container and the port;
  • WORDPRESS_DB_USER: corresponds to MYSQL_USER from the db container;
  • WORDPRESS_DB_PASSWORD: corresponds to MYSQL_PASSWORD from the db container;

To start WordPress you simply type:

$ docker-compose up

Due to persistence set for containers, your work will persist. Also, you can edit any file and modifications are real-time visible. When you want to stop all the setup you can use:

$ docker-compose down


This is not a production setup because there are some security issues, but now you can easily develop your work locally and keep things isolated in a bundle of containers. When you finish work, is enough to destroy containers, delete persistence directories and your development machine is nice and clean. No more version conflicts, no more software to uninstall and config files to be cleared.

Happy codding!

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)