What is docker compose and how to setup a wordpress website with docker compose

Docker Compose is a tools for defining and running multiple container Docker application, you may spin up one or more container by using the configuration YAML file.

Docker compose work in all environment including production, staging, development, testing as well as CI workflows.

The latest version of Docker Container come with the Compose where you no need to install the Compose tools separately. If your yet to install the docker container in your machine, you may just following the blog post on how to installing the Docker Container in Ubuntu 16.04

By using the Docker Compose, it’s basically consist of the following three-step process:
1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment
3. Run docker-compose up and compose starts and runs your entire app.

Following are the few simple step to compose your first wordpress site with MySQL ready

1. Create an empty project folder

You may give the folder any name as you like as long as you can easily remember, the folder should not contain anything but only your docker-compose.yml file(docker-compose.yaml, but yml and yaml extension both working fine). I will name my folder as my_wordpress.

$ sudo mkdir my_wordpress

2. Change the current directory to the project directory

$ cd ./my_wordpress

3. Create a docker-compose.yml file that starts your WordPress blog and a separate MySQL instance with a volume mount for data persistence:

You may create the compose file with any of your favourite text editor, I’m using vim as my primary text editor on my machine

4. Build the project

Once the above yml file save, now you can start to build your project by using the code

$ docker-compose up -d

By running the code above, it’s actually running at the deamon mode and the docker actually will check on your local images repository to see is the image exists, if not, they will go to the docker hub and pull the needed image. After the pulling process done, it will auto start the MySQL as well as wordpress by showing the following log

Step 5: Browse the website

At this point of time, your wordpress website should be up and running at the port 8000 (this port we specific early in the docker compose file). In order to view your wordpress site, just open up your browser and enter the address of http://localhost:8000 in the address bar.

Step 6: Shutdown and cleanup your docker container

docker-compose down

allow you to removes the containers and default network, but preserves your WordPress database.

docker-compose down --volumes

will removes the containers and default network and also volumes

Hopefully by following the above simple step will let’s you know how’s the docker-compose work and able to kick start by build your own container for your purpose, and also please do share with me if you have any concern or idea to make this better, so that we can learn the docker container together.