How to Easily Setup Wordpress Using Docker Compose

Habib Ridho
3 min readMar 30, 2020

--

In the previous article, we learned how to setup Wordpress using Docker. We ran two containers: wordpress and mysql, and connect them using docker network. We ran them imperatively by running multiple commands to get it done.

However, we may want to have a script to do it so that we don’t need to run the commands every time we want to start wordpress. Luckily, Docker has a tool for our case: Docker Compose. We can write a yaml file that consists our requirements and start them with a single command.

Prerequisite

First, we need docker-compose installed on our machine by simply running docker-compose -v. If it is not installed on our machine yet, we need to install it first by following this instructions.

Create Compose File

Next, we need to create a new docker-compose.yml file and fill it with wordpress and mysql setup. The filename actually can be anything, but if we use other name we need to specify the file when running the compose command.

version: "3"
services:
wordpress:
container_name: my_wordpress
image: wordpress
ports:
- "8080:80"
links:
- mysql
environment:
WORDPRESS_DB_HOST: mysql
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: "12345"
WORDPRESS_DB_NAME: wordpress
mysql:
container_name: my_mysql
image: "mysql:5.7"
volumes:
- ./.mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: wordpress
MYSQL_ROOT_PASSWORD: "12345"

There is a lot going on that file, let’s dissect it one by one. The first field is version which tells Compose what file format that we use. Certain field may not be recognized by a certain Compose version, so check which version is supported by the Compose installed on your machine here.

Our second field is services which describe what services we want to run. In this case, we will run wordpress and mysql container. In each service, we have an optional container_name field that will be used as the container name. If we don’t specify container name, Compose will generate it automatically.

Next, we have image field which tells Compose which docker image we want to use. Since we want wordpress to be accessible outside of the container, we open port-mapping from 8080 on our host to 80 port of the container by defining it in ports field.

We learned in the previous article that we need to connect wordpress and mysql container so that they can communicate with each other. In this case, we use links to do that by simply specify which other container we want to connect this service to.

We use environment field to list down all the environment variables that need to be set in each containers. Lastly, in mysql service, we mount a volume to it in the volume field. We link .mysql folder on the host to /var/lib/mysql on mysql container so that database data can be persisted even if we shutdown the container. If we don’t have .mysql folder on the host yet, Docker will create it for us.

Run

To run the services, we only need to run a single command. Note that -d flag will make the services run in the background.

$ docker-compose up -d

A little note, we need to specify the compose file name if we use non-standard name, for example some-compose.yml.

$ docker-compose up -f some-compose.yml -d

Although, we run the containers declaratively, we can still use docker commands to do operation on these containers. For example, if we want to go inside the database, we can use the same command as before.

$ docker exec -it my_mysql mysql -u root -p

To shutdown these containers, we don’t need to delete them one by one. We can simply run:

$ docker-compose down

In this tutorial, we make a simple wordpress setup even simpler by defining the services in a yaml file. In this way, we can turn the setup on and off with a single command. Another advantage of this is that the compose file can be moved to another machine and we will have the same setup running on it. This can ease development in teams as well as deployment to testing or production machine.

--

--