Docker Compose

If you were following along with the Linux installation in Chapter 1, Installing Docker Locally then you should have already installed Docker Compose manually, for those of you that skipped that part then you will glad to know that Docker Compose is installed and maintained as part of Docker for Mac and Windows.

I am sure that you will agree that so far Docker has proved to be quite intuitive, Docker Compose is no different. It started off life as third-party software called Fig and was written by Orchard Labs (the project's original website is still available at http://fig.sh/).

The original project's goal was the following:

"Provide fast, isolated development environments using Docker"

Since Orchard Labs became part of Docker, they haven't strayed too far from the original projects goal:

"Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application's services.Then, using a single command, you create and start all the services from your configuration."

Before we start looking at Compose files and start containers up, let's think of why a tool such as Compose is useful.

Why Compose?

Launching individual containers is as simple as running the following command:

docker container run -i -t ubuntu:16.04 /bin/bash

This will launch and then attach to an Ubuntu container. As we have already touched upon, there is a little more to it than just launching simple containers though. Docker is not here to replace virtual machines, it is here to run a single application.

This means that you shouldn't really run an entire LAMP stack in single container, instead, you should look at running Apache and PHP in one container, which is then linked with a second container running MySQL.

You could take this further, running NGINX container, a PHP-FPM container, and a MySQL container. This is where it gets complicated. All of sudden, your simple single command for launching a container is now several lines, all of which must executed in the correct order with the correct flags to expose ports, link them together and configure the services using environment variables.

This is exactly the problem Docker Compose tries to fix. Rather than several long commands, you can define your containers using a YAML file. This means that you will be able to launch your application with a single command and leave the logic of the order in which the containers will be launched to Compose.

Note

YAML Ain't Markup Language (YAML) is a human-friendly data serialization standard for all programming languages.

It also means that you can ship your application's Compose file with your code base or directly to another developer/administrator and they will be able to launch your application exactly how you intended it be executed.

Compose files

Let's start by getting a launching WordPress again. First of all, if you haven't already clone the GitHub repository which accompanies this book. You can find it at the following URL: https://github.com/russmckendrick/bootcamp

For more information on how to clone the repository please see the introduction. Once you have repo cloned run the following commands from the top level of the repo:

cd chapter2/compose-wordpress

The compose-wordpress folder contains the following docker-compose.yml file:

version: "3" 

services:
mysql:
     image: mysql
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: wordpress
       MYSQL_DATABASE: wordpress
wordpress:
depends_on:
       - mysql
     image: wordpress
     ports:
       - "8080:80"
     restart: always
     environment:
       WORDPRESS_DB_PASSWORD: wordpress

volumes:
db_data:

As you can see, the docker-compose.yml file is easy to follow; our initial docker-compose.yml file is split into three sections:

  • Version: This tells Docker Compose which file format we are using; the current version is 3
  • Services: These are where our containers are defined, you can define several containers here
  • Volumes: Any volumes for persistent storage are defined here, we will go into this in more detail in later chapters

For the most part, the syntax is pretty similar to that we used to launch our WordPress containers using the Docker command-line client. There are, however a few changes:

  • volumes: In the mysql container we are taking a volume called db_data and mounting it to /var/lib/mysql within the container
  • restart: This is set to always, meaning that if our containers stop responding any reason, like the wordpress container will do until the mysql container is accepting connections, then it will be restarted automatically meaning we don't have to manually intervene
  • depends_on: Here we are telling the wordpress container not to start until the mysql container is running

You may notice that we are not linking our containers, this is because Docker Compose automatically creates a network to launch the services in, each container within the network created by Docker Compose automatically has its host file updated to include aliases for each of the containers within the service, meaning that our WordPress container will be able to connect to our MySQL container using the default host of mysql.

To launch our WordPress installation, all we need to do is run the following commands:

docker-compose pull
docker-compose up -d

Using the -d flag at the end of the command launches the containers in detached mode, this means that they will run in the background.

If we didn't use the -d flag, then our containers would have launched in the foreground and we would not have been able to carry on using the same terminal session without stopping the running containers.

You will see something like the following output:

Compose files

While the containers are up and running, which you can see by running the follow:

docker-compose ps
docker container ps

It will take a short while for the MySQL container to be ready to accept connections, you may find running:

docker-compose logs

Show you connection errors like the ones below:

Compose files

Don't worry, you should soon see something like the following:

Compose files

Again, opening http://localhost:8080/ in your browser should show you the installation screen:

Compose files

The process above works on Docker for Mac and on Linux; however for Docker for Windows you should add.exe to your Docker Compose commands:

cd .chapter02wordpress-compose
docker-compose.exe pull
docker-compose.exe up -d
docker container ps

This will give you something like the following output:

Compose files

Again, opening your browser and going to http://localhost:8080/ should show you the installation screen:

Compose files

Before we move into the next section, let's stop and remove our WordPress containers by running the following commands:

docker-compose stop
docker-compose rm

Or if you are following using Docker for Windows:

docker-compose.exestop
docker-compose.exe rm

So far, we have been using images from the Docker Hub, next we will are going to take a look at customizing images.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset