In this recipe we are going to look at a simple docker compose example of a WordPress instance in a container that is connected to a MariaDb database which is similar to the backbone of this blog itself.
See also my post on docker-compose commands.

This recipe comes without any git repository, because you only need to have docker-compose installed (which is already done if you use macOS or windows machine) and the docker-compose.yml file which I will show you in this post.

When you do not know my recipes take a look at the introduction, or check out all recipes there are.

As usual in the recipe section we will look at the following:

Why

We can get our hands on the docker-compose command and play a little with it by using it with the WordPress application that also powers this website!

With this we can have a playground for docker-compose and get comfortable around it for much more daunting orchestration tasks.
Also setting up a CMS becomes really easy this way.

What

In this fairly simple example we are going to set up a local WordPress installation that can be accessed by your browser with a MariaDb backend.

We will not only pull the images from the docker hub but also orchestrate the application to work by using the docker-compose.yaml file syntax and the respective docker-compose command.

We will also manage the restart, persistence of data and management of environment variables.

How

As a preparation step you need to install docker-compose first. If you use macOS then it comes with the docker installation. For Linux installation see the documentation.

  1. Create the following docker-compose file
  2. Run docker-compose up -d
  3. Connect to the web app with a browser at localhost:8082
  4. Enjoy your CMS

1.) Docker-compose.yaml file

version: '3'
services:
db:
image: mariadb:latest
volumes:
- /tmp/dc-test-db:/var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: geheim
restart: always
wordpress:
image: wordpress:latest
volumes:
- /tmp/dc-test-wordpress:/var/www/html
ports:
- "8082:80"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: geheim
restart: on-failure

Just for a quick overview:
On the top level we specify the version and the services. Services means here simply the containers we want to compose.
On the next level we specify the names of the containers (or services), here those are db and wordpress.
The next level then specifies the image. Both services are using the latest base image of their respective application.
For the database we define a volume so that the data created by wordpress will be saved when we stop the application (see docker volume intro).
Also we specify environment variables and the port mappings for the database.
The last tag of restart always means that the containers will be restarted until it works correctly or the docker-compose is stopped.

2.) Run docker-compose up


gt; docker-compose up -d

Running the docker-compose command will set look in your current directory for the docker-compose.yml file. It then creates all the services as containers starting with the top one first.
It also sets up the volumes and an internal network for you. (for a more detailed introduction see my other post on docker-compose introduction)

Make sure if you are on macOS to integrate the /private before your path on the host volume path. You can also use the symbolic link /tmp/.
This is because of the docker daemon linux virtual machine that tries to comply with macOS sandbox principles.
See also here.

3.) Connect to your local CMS

Simply open your browser and enter localhost:8082, you will be redirected to the following page. (if everything is setup and running correctly, maybe you need to poll manually by refreshing the page from time to time).

4.) Enjoy your CMS

Caveats

If you run this again it is likely that you get a connection refused error, this is because WordPress is up and running before the MariaDb is initialized.
Docker-compose regrettably has no notion of checking if a container is ready (Whatever this means in terms of the used applications).
But as I show in the Advanced features below, there are solutions to this problem either with third party tools or by utilizing the healthcheck syntax from the docker-compose reference.

Also remember that you need need to suffix the volumes with /private/var if you are using macOS. You can find more info on this issue here.

Advanced features

  • Compose with wait-for-it or any other tool to monitor if the MariaDb is already up and running
  • Create a compose file with health check that connects to MariaDb to avoid the connection issue.

You can learn this, and more in the course on Docker for Developers:

Docker-compose with WordPress as in docker course

Docker online course for software developers


0 Comments

Leave a Reply

Avatar placeholder