(1 votes, average: 5.00 out of 5, rated)
Loading...

If you’re like me, and host a thousand things on a single server, you’ve probably tinkered around with docker and docker-compose. The most appealing side of docker-compose, is that it saves you from configuring your machine in ways not even you will know about anymore tomorrow.

So there’s plenty of tutorials on how to deploy wordpress with docker-compose on the internet, and here’s just my take on things I picked up here and there.

WordPress has an official docker repository that can be found here: https://hub.docker.com/_/wordpress/. I also took an official mysql image and threw together this docker-compose.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
version: '3'
services:
    db:
        image: mysql:5.7
        volumes:
            - ./db_data:/var/lib/mysql
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: wordpress
            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
        volumes:
            - ./files:/var/www/html
            - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini

The way this is set up, you will first have to wget wordpress files into ./files, in whichever directory you choose to do this from. Why? Because we are going to mount the files into the container.

You will notice three volumes, one for mysql data (trust me, you’d want that on your host machine and back it up, back it up regularly), one for wordpress files and one config file. Some people choose to only mount the wp-content directory for wordpress, I chose to mount its root directory. Uploads.ini file is mounted because wordpress wasn’t allowing me to upload large themes, so instead of changing config inside a container, I mounted a config file. I believe containers should be disposable and all the config on your host machine, so you can easily destroy your containers, transfer files another machine, run docker-compose up and be on your way.

So what this docker-compose.yml does, is it creates a container in which mysql is installed and all your tables configured as passed in the environment variables. It also creates a container into which it would utomagically put wordpress files, if we didn’t already mount them into the container.

Docker-compose handles the communication between containers for you, so all you’ve got left to do is to choose a port on which you want wordpress to be exposed. I chose 8000, as you can see in the ports directive of wordpress container. Essentially what the directive does is it maps port 80, on which the makers of the wordpress container decided to expose its service, to a port of your choosing. If 80 is okay for you, you can leave this line out.

Now a tiny little more in depth about how the communication between those two containers works here.
Containers are linked via hostnames (so if you name your container ‘wordpress’ in the docker-compose.yml, its IP address and the string ‘wordpress’ will be in /etc/hosts of the db container and vice versa). So now the wordpress container can contact mysql server running at port 3306 (default for mysql, look up the official mysql dockerfile for more info), and it can contact it via the hostname ‘db’. I hope the variable WORDPRESS_DB_HOST makes more sense now 🙂

All that’s left is to run docker-compose up -d and your containers should be running forever, even after a reboot, since their restart policy is set to ‘always’, so there’s no need to tinker with systemd here.

In the directory of your choosing (where you decided to deploy wordpress) you should now be seeing this kind of file structure:

1
2
3
4
5
6
drwxrwxr-x  ./
drwxr-xr-x  ../
drwxr-xr-x  db_data/
-rw-rw-r--  docker-compose.yml
drwxr-xr-x  files/
-rw-rw-r--  uploads.ini

Remember to back up these files! 🙂

Your email is kept private. Required fields are marked *