As I describe in my previous post, this site is running in a docker container on an Azure VM. In this post, I will explain how to configure WordPress docker container using Docker Compose.
I wanted to deploy WordPress on my own host instead of using it as a service. In order to do this, you require a MySQL database in addition to the web host, unless you use Project Nami which enables you to use SQL Azure instead of MySQL. One option could be to use, for example, Azure App Service and a MySQL instance from ClearDB (you can find more information for this approach here).
I preferred to use another approach by taking advantage of Docker. With this approach I can have WordPress and MySQL running in separated containers in the same VM and I can add even more containers in the future at no extra cost (which is my idea).
The first step is to connect to the VM where Docker is running using ssh (if you have any questions about this, see my previous post). I wanted to use docker-compose to set up the environment, which is an orchestration tool that makes spinning up multi-container applications effortless. In a new folder (e.g. /home/user/dev/), create a new file named docker-compose.yml.
In the docker-compose.yml file, add the MySQL container based on the official mysql image under a configuration which I named db (you can choose any name instead of db). Additionally, set up the root password for MySQL specifying the MYSQL_ROOT_PASSWORD environment variable. The following snippet shows how the docker-compose file should looks like at this point.
db: image: mysql environment: MYSQL_ROOT_PASSWORD: YourWordPressPasswordHere
Now, add the WordPress container based on the official wordpress image, adding the link to the mysql container by specifying the name of the configuration under links as it’s shown in the following code snippet.
web: image: wordpress links: - db:mysql
Now, add the expose configuration under the WordPress container configuration (i. e. the web entry) to be able to access port 80 from the outside. Note that you could map the port to any other just by specifying “OutsidePort:80” (remember to use the double quotes).
web: image: wordpress links: - db:mysql expose: - "80"
This is enough for having the WordPress Docker container up and running, but there is another interesting thing to do before starting the containers. You can expose the wp-content folder in order to easily update themes, or other content from WordPress to the VM where docker is running. In order to do this, you will need to configure the working_dir and map the volumes as it’s performed below.
web: image: wordpress links: - db:mysql expose: - "80" working_dir: /var/www/html volumes: - "/pathInVM/wp-content:/var/www/html/wp-content"
The final version of the docker-compose.yml file should look similar to the following one:
web: image: wordpress links: - db:mysql expose: - "80" working_dir: /var/www/html volumes: - "/pathInVM/wp-content:/var/www/html/wp-content" db: image: mysql environment: MYSQL_ROOT_PASSWORD: W0rdpressPassw0rd!
Once you have everything ready, save the changes to the file and execute
docker-compose up to start both containers and try accessing the site which should be running in port 80. And that’s it, you have configured WordPress Docker container.