With this tutorial, we are going to learn how to use Docker Compose. Docker-compose is one of the most important docker utility & it is a must-learn if you want to know/learn about docker. Docker-compose is used for running multiple containers using a single file or for running multi-container applications. For this, we write a YAML file called dcker-compose.yml & we mention all the containers aka services details as well other networking or volume details. 

Once we have mentioned all the required dependencies/container details, we can then run all the containers using a single command & if it's required, we can stop or terminate all the things with a single command. Not only does this makes it easier to manage containers but is also very fast.

Once more advantage of using docker-compose is that we don't have to create separate networks or connect to any created network (though we can also connect to the network already created), as all the docker containers mentioned inside the docker-compose file will have network connectivity with each other.

Recommended Read: Lazy SysAdmin’s guide to install Docker-Compose

Also Read: Learn to create Dockerfile with Dockerfile example

This is a beginner’s guide to learn how to use Dcker-Compose, we will mention the important & most frequently used options/parameters used in a compose.yml file. We will try to set up a 2 container application i.e. with WordPress & DB.

How to use Docker-Compose

In this example, we will accomplish the following things:

  • Use compose version 2 to create docker-compose.yaml file. 
  • Add one more service named "db" under the same docker-compose file. 
  • Use an image named MySQL with version 5.7
  • Use volume wordpress_data and map it to /var/lib/mysql 
  • Enable always restart parameter. * Add environment variables named "MYSQL_ROOT_PASSWORD", "MYSQL_DATABASE", "MYSQL_USER", and "MYSQL_PASSWORD" along with corresponding values for all.
  • Create a service/container named "WordPress" using wordpress:latest image. 
  • Add dependency of db service in WordPress service.
  • Map port of WordPress container port 80 to host system port 8000. 
  • Add a parameter to restart the container in case the service went down. 
  • Within the WordPress environment variable, add wordpress_db_host value along with the port. 
  • Also, add one more variable named wordpress_db_password. 
  • Add a volume named wordpress_data

It seems like a handful but no worries, it's easier than it looks. Once you understand these, it will be one of the easiest things to do. So I will first write the whole docker-compose.yml file & then will explain all the sections individually.

$ vi docker-compose.yml

version: "2" 



                                    image: mysql:5.7 


                                               - wordpress_data:/var/lib/mysql 

                                    restart: always 


                                                MYSQL_ROOT_PASSWORD: test@979 

                                                MYSQL_DATABASE: wordpress_db 

                                                MYSQL_USER: testuser 

                                                MYSQL_PASSWORD: test@123




                                                                - db 

                                          image: wordpress:latest 


                                                              - 8000:80 

                                         restart: always 


                                                            wordpress_db_host: db:3306 

                                                            wordpress_db_password: test@123 



                     wordpress_data: {}


That’s it. This is our docker compose file.

 Note: Before we move any further remember INDENTATION IS IMPORTANT. You can write anything but if it's not properly indented, then there is no chance that your docker-compose file will work. Even if you copy-paster, check indentation, better yet do it again.

Note: My indentation might not be right due to wordpress removing my formatting, so please copy/paste at your own risk.

So let start with the understanding of docker-compose.yml,

‘version: "2"’ - Basically this defines the compatibility of various services & docker versions. Its mainly divided into three versions ‘1’, ‘2.X’ & ‘3.X’. You can find more information & compatibility matrix HERE.

‘services:’ - Basically whatever is mentioned under this section are the details of containers we will create.

‘db:’ & ‘wordpress:’ - Name of the containers or we can also say hostname.

‘image: mysql:5.7’ & ‘image: wordpress:latest’ - the name of the images that will be used for creating the docker container. If we have docker files instead of images, we can also use that. In order to do that we will not mention the ‘image’ section but rather will mention the following section named ‘build’,


                           dockerfile: dockerfile.test

‘depends_on:’ - we have mentioned this section only under the wordpress container. This means that the wordpress container will depend on the container named ‘db’.

‘volumes:’ - As you should know that once a container has been terminated, all the data inside that container is gone. So if want data from the container to persist, we mount a volume from localhost to the container. This is what this section is for, we are mounting a localhost volume named ‘wordpress_data’ to ‘/var/lib/mysql’ folder inside the docker container.

‘ports: ’ - This section in wordpress container allows us to port map a localhost port to the docker container port. Here we want to map port 8080 of the localhost to port 80 of the wordpress container.

‘restart: always ’ - With this section, we are saying that in the case due to any issue or errors if the container dies out, it should be restarted immediately. If we don't mention, restart policy then once the container stops, it will remain stopped. Other restart policies that can be used are ‘no’, ‘on-failure’ & ‘unless-stoped’.

‘environment:’ - Under this section, we mention all the variables & their values to be used by containers. 

‘volumes’: Firstly check the indentation of this section, it's not under services & we have already used this option for the ‘db’ container as well. Basically here we are creating a volume named ‘wordpress_data’ to be used by our ‘db’ container. This section is equivalent of command,

$ docker volume create wordpress_data

Some important Compose commands

So we have covered all the sections of the docker-compose.yml file in this learn docker compose tutorial. We now need to create the containers by running the following command,

$ docker-compose up -f docker-compose.yml

To check the running containers, run

$ docker-compose ps

To stop the running docker containers,

 $ docker-compose stop 

To remove them from localhost

$ docker-compose rm

That’s it for our tutorial, please feel free to mention any queries or questions using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

Linux TechLab is thankful for your continued support.