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 what powers this blog itself.
See also my post on docker-compose itself.
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.
As usual in the recipe section we will look at the following:
- Why would you do this
- What do we actually do with this
- How are we going to do it
- Advanced features
We can get our hands on the docker-compose command and play a little with it.
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.
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.
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.
- Create the following docker-compose file
- Run docker-compose up -d
- Connect to the web app with a browser at localhost:8082
- Enjoy your CMS
1.) Docker-compose.yaml file
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
$> 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).
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.
- 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.
Also you can read up on docker-compose in the following books: