Docker Compose - A gentle introduction with WordPress
Docker Compose is a orchestration tool for multi-container Docker applications.
We can configure our application's services via a single command to create and start all the services from our configuration using a Compose file. In other words, by using Docker Compose we can orchestrate processes of Docker containers such as starting up, shutting down, and setting up intra-container linking and volumes.
Using Compose is basically a three-step process:
- Define app's environment with a Dockerfile so it can be reproduced anywhere.
- Define the services that make up an app in docker-compose.yml so they can be run together in an isolated environment.
- Run docker-compose up and Compose will start and run the entire app.
Throughout this, we'll work on Ubuntu 14.04.
In this section, we'll play with a simple test using Docker Compose just to get the taste of it.
Because we'll launch just one container, the test won't demonstrate one of the main features of Docker Compose: up and down a group of Docker containers all at the same time.
The docker-compose command works on a per-directory basis. So, we just make one directory for each container and one docker-compose.yml file for each container inside its directory. In that way, we can have multiple groups of Docker containers running on one machine.
We'll be working on a virtual environment (python 2.7):
$ virtualenv venv $ source venv/bin/activate (venv) $
$ sudo apt-get -y install python-pip
Install Docker Compose:
$ sudo pip install docker-compose
my-test: image: hello-world
The first line is the container name while the second line specifies the container image which will be downloaded from the Docker Hub repository.
$ docker-compose up Creating helloworld_my-test_1 Attaching to helloworld_my-test_1 my-test_1 | my-test_1 | Hello from Docker. my-test_1 | This message shows that your installation appears to be working correctly. ...
The output then explains what Docker is doing with Docker Hub, Docker daemon, and Docker client:
- The Docker client contacted the Docker daemon.
- The Docker daemon pulled the "hello-world" image from the Docker Hub.
- The Docker daemon created a new container from that image which runs the executable that produces the output above.
- The Docker daemon streamed that output to the Docker client, which sent it to our terminal.
In the following sections, we'll deal with a real-world example of using Docker Compose to install an WordPress application. We'll install MariaDB as well.
Since WordPress app normally runs on a LAMP stack, we need Linux, Apache, MySQL/MariaDB, and PHP.
The official WordPress Docker image provides Apache and PHP for us but not MariaDB which we need to take care of by ourselves.
We need to tell Docker Compose to start a new container called wordpress and download the wordpress image from the Docker Hub.
wordpress: image: wordpress
To bring up the image:
(venv)~/wordpress$ docker-compose up
At the end of the download process, we get complains like this:
... wordpress_1 | error: missing required WORDPRESS_DB_PASSWORD environment variable wordpress_1 | Did you forget to -e WORDPRESS_DB_PASSWORD=... ? wordpress_1 | wordpress_1 | (Also of interest might be WORDPRESS_DB_USER and WORDPRESS_DB_NAME.) wordpress_wordpress_1 exited with code 1
It tells us our wordpress needs db.
We should define a new container called wordpress_db and tell Docker to use the mariadb image from the Docker Hub.
To add the MariaDB image to the group, let's modify our ~/wordpress/docker-compose.yml :
wordpress: image: wordpress links: - wordpress_db:mysql wordpress_db: image: mariadb
With the modification, we'll have two containers: wordpress container and wordpress_db container.
We're telling our wordpress container to link our wordpress_db container into the wordpress container and call it mysql.
So, inside the wordpress container the hostname mysql will be forwarded to our wordpress_db container.
Let's run docker-compose up again:
(venv)~/wordpress$ docker-compose up ... ERROR: for wordpress Cannot start service wordpress: Cannot start container d14451708c365e5c0862194e6585620569f06caaf0fc8c226a0a44a332adf70a: Cannot link to a non running container: /wordpress_wordpress_db_1 AS /wordpress_wordpress_1/mysql ERROR: Encountered errors while bringing up the project.
Still we see that we're not quite there yet though. More work needs to be done.
It looks like just linking the two containers isn't quite enough: we need to set the MYSQL_ROOT_PASSWORD variable.
wordpress: image: wordpress links: - wordpress_db:mysql ports: - 8888:80 wordpress_db: image: mariadb environment: MYSQL_ROOT_PASSWORD: mysql-root-password
In 'port' section, the first port number is the host port number, and the second port number is the container port. The port configuration forwards requests on port 8888 of the host to the default web server port 80 inside the container.
(venv)~/wordpress$ docker-compose up -d Starting wordpress_wordpress_db_1 Starting wordpress_wordpress_1 (venv)~/wordpress$
This time, we run it with the -d option, which will tell docker-compose to run the containers in the background as a service.
Open up a web browser (localhost:8888):
Stop all the Docker containers with the following command:
$ docker-compose stop
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization