Wordpress official docker image comes with Apache, php-fpm and wp-cli build into it. This will make running Wordpress with official image super quick. In fact, there is a quick start guide on the docker website too - Quickstart: Compose and WordPress | Docker Documentation . But in this guide, I will go over each line of the docker compose file and explain what are they.
Docker compose file is a configuration file for running multiple isolated containers in a single host. There are many reasons why you may need this. For example, you might want to run more than one Wordpress site at your dev environment and with docker compose they won’t interfere with each other. Each build can be independent of each other at CI level.
Let's create a docker-compose.yml
file. This file will define services, networks and volumes.
First, you add the version number. This does not refer to docker or docker compose versions. Instead, it is for the version of docker-compose file format. 3.7
is the most recent compose version at the time of writing this article so I will use that.
You need to define each service you want to run under the services key. You only need 2 services for this:
The official Wordpress image comes with Apache web server and php-fpm already so we don’t need to add these services.
We will use MySQL 5.7 image from docker hub. Next, we need to define volumes for the database. Volumes are used for persisting data because docker containers are stateless and we want the data to persist after the container is stopped or restarted.
db_datais used here as just a name so that we can refer to it later. And then wemap it to /var/lib/mysql
folder. This is the default MySQL data directory.
version: "3.7"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
By default, container is not restarted so we need to add restart: always
command here. So if your container crashes far whatever reason, it will be restarted automatically.
restart: always
Next, we need to set environment variables that will be used by MYSQL.
For now, we will add them into the docker-compose file. But when we want to save this is in git, we will need to move them env.sh
file to not include env vars in source code.
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
We will open a port so that we can connect to the database using database client such as Sequal Pro later on.
version: "3.7"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
ports:
- "3306:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
Wordpress has a dependency which is the database and we are going to use the latest official WordPress image.
---
wordpress:
depends_on:
- db
image: wordpress:latest
We also need to open port to connect the wordpress site from outside. Port 80 is the reserved port for HTTP. Expose host port 8000 to container port 80
---
ports:
- "8000:80"
We will also need to add restart command and the environment variables for wordpress
---
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
We will just refer to database as our volume. Docker compose file is now complete.
version: "3.7"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
ports:
- "3306:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
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: {}
Let's run it!
This will create the container and run it at the background:
docker-compose up -d
Once it’s done, you can check if our containers are running
docker ps
Everything looks good, let's go to http:localhost:8000.
Wordpress configuration page should display
Let's configure the WordPress and create our first post on latest WordPress editor!
We can also connect to the database because we already opened the port in docker-compose file. (The password is somewordpress which we set it as env variable in the docker compose file)
Let’s stop the containers
docker stop <container-id|container-name>