docker compose

Docker Compose

Reading Time 4 minutes

Docker Compose is going to become your best friend. Do you remember in the previous post, we talked about running multiple containers in a manual fashion? Well, it’s time to say goodbye to that. In this post, we will look at a more friendlier approach to running multiple containers.

What is Docker Compose?

So, what is Docker Compose? In the previous post, we manually pulled and started containers which contained a WordPress and MySql software. We were able to link both containers together and also tested it in a web browser. In short, we executed a number of commands followed by some magic. In this post, we will take the manual magic and put it in a YAML file. Basically, we will write a short script which will go off and pull, start and manage all containers from a single point of contact. The act of writing a YAML script which does all the handling for us is done through Docker Compose. Let’s have a look at an example.

WordPress, MySql and Docker Compose

docker compose
As shown in the diagram above, we will take our existent solution and convert the entire thing to run from a YAML file. This means we will write a simple and short script which will be able to mimic the same commands we did in the previous post.

Let’s have a quick look at the commands which we executed in the last post, to remind us of the magic we have to write on the command line.

docker pull mysql
docker pull wordpress
docker run –-nameqas-sql–eMYSQL_ROOT_PASSWORD=password -d mysql:latest
docker run –-name qas-wordpress –-linkqas-sql:mysql–p 8080:80 –d wordpress

As you can see, we had to manually pull the images and then run them with parameters, forcing them to talk to one another.

Let’s YAMLify this

There is however a better way, we can convert all the above manual commands into a YAML file as shown below:

File Name: docker-compose.yml

version: '2'
 
services:
 
 mysql:
  image: mysql
  environment:
   MYSQL_ROOT_PASSWORD: password
 
 wordpress:
  image: wordpress
  ports:
   - 8080:80
  environment:
   WORDPRESS_DB_PASSWORD: password

Looking closely at the file, you can see that we define a number of things. Firstly, we define the version. This is the version of YAML that we will use. The benefit of using version 2, this automatically links all the services (containers) which we outline in the YAML file. It is also important to note that the indentation, case sensitivity and keywords used in the YAML file need to be correct. Using additional spaces, incorrect words can easily result in errors when trying to execute the YAML file.

We define containers as services. In this case we are using two services i.e. WordPress and MySql. It should be clear that each service can control its own variables, this is great since we can control a given service in isolation. Also, notice that the link is not directly noted in the YAML file. This is because the link is explicit. The link is explicit, this feature comes from using YAML version 2.

Docker Compose Commands

Now that we have a YAML file ready, let’s have a look at some of the commands which can help to manage a YAML file.

docker-compose ps
docker-compose up
docker-compose stop
docker-compose start
docker-compose restart

The commands above are a little different to what we have seen to date. These commands are used to manage a docker compose instance.

docker-compose ps
When trying to run a YAML file, one of the first commands we need to use should be docker-compose ps. This tells us the current containers that are running which were executed as part of a YAML file.

docker-compose up
You have the option of either starting your containers in a YAML file through ‘up’ or ‘start’. ‘Up’ will try to download a container defined in your YAML file if it has not already been downloaded. It will then try to start the services.

docker-compose stop
This is a very self-explanatory command, this is used to stop any services running through a composed file.

docker-compose start
The ‘start’ command is similar to ‘up’. When trying to ‘start’ a docker compose file, each service listed will be started as a container. However, ‘start’ will not download any images if they don’t exist.

docker-compose restart
The restart command is used to restart all the services in the YAML file. This is a useful command as this encompasses both the start and stop commands.

Docker Compose Conclusion

In this post, we have finally looked at how we can use Docker Compose to control management of multiple containers from a single source. It should be clear how powerful the Docker Compose approach is and the benefits one can gain.

Mo

I am a passionate tester, father, husband and gamer. I love to write blogs about Software Testing and generally contribute back to the Software Testing world.

More Posts - Twitter - Facebook

Published by

Mo

I am a passionate tester, father, husband and gamer. I love to write blogs about Software Testing and generally contribute back to the Software Testing world.