Beginner’s guide to learn Docker Compose (docker-compose.yml) — LinuxTechLab

Docker compose is one of the most important docker utility & it is 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 docker-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/containers 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 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 Docker Compose, we will mention the important & most frequently used options/parameters used in a docker-compose.yml file. We will try to set up a 2 container application i.e. with WordPress & DB.

Learn Docker Compose

In this example to learn docker compose, we will accomplish the following things:

  • Use docker-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 container in case 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 then it looks. Once you understand these, it will be one of the easiest thing 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” services: db: image: mysql:5.7 volumes: — wordpress_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: [email protected] MYSQL_DATABASE: wordpress_db MYSQL_USER: testuser MYSQL_PASSWORD: [email protected] wordpress: depends_on: — db image: wordpress:latest ports: — 8000:80 restart: always environment: wordpress_db_host: db:3306 wordpress_db_password: [email protected] volumes: 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 its 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 .

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’.

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

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

$ docker volume create wordpress_data

Some important Docker 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

To stop the running docker containers,

To remove them from localhost

That’s it for our Beginner’s guide to learn Docker Compose 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:-

Donate us some of your hard-earned money:

Linux TechLab is thankful for your continued support.

Originally published at on September 10, 2019. is a beginner friendly website where you can learn Linux Tips & tricks,Scripting, also has lots of tutorials aimed at making Linux easy.