You are currently viewing Docker Compose for Multi-Container Applications

Docker Compose for Multi-Container Applications

This is another blog in the docker and kubernetes series. You can have a look at my previous blogs about docker basics and dockerfile.

This article is not formal documentation of docker compose. But this article is for providing high level overview of docker-compose to people who are new to this.

We saw how to use dockerfile to deploy a web application in the docker container. But, in real world, most of the times, the applications are not single container applications.

Your applications may have UI layer, a separate API layer and database servers. On production machines, you will probably create separate containers for each layer.

If you have such a huge application to deploy on docker, you may certainly have an important question – how to automate deploying these multiple containers ?

Obviously, you have choice of creating multiple Dockerfiles to create each image and then use docker run commands to run the containers. You can put those commands in a batch file or powershell file. You will also have to take care of upating the configurations depending on how containers want to access one another.

Although it is completely possible to do everything with command line, there is one another and bit easier declarative way to achieve this. You can compose a .yml file and use docker-compose to do all the work for you.

So, What is Docker-Compose ?

The docker compose is a tool for creating and running the related multiple containers. The Docker for desktop installs for Windows and Mac, contains Docker Compose. Hence no separate installation is required.

Using docker compose for multi-container application is a three step process:

  • Create the dockerfile to define the images of different parts of your application
  • Create a .yaml or .yml file which describes the services and how they can run together in your application
  • Then run docker-compose up command which starts your entire application.

But why to use docker-compose ?

Docker Compose has set of commands to start, stop the application/services. Docker compose can also enable you to view status of running application.

You may want to use docker-compose if you want to spin multiple environments on the single host machine. This may be useful when you are testing the features you developed before releasing them to other teams or users.

You can also configure how many replicas you want of a service or component of your application. You can also configure environment variables which can be used from within the container.

And What is .yml file ?

This is the file in which you specify different images required for your application. In this, file you declare what you want and do not specify how to achieve that.

e.g. when you want to install wordpress, you may need one container to hold all the CMS code while the other container to hold the database. You may need to setup connection between the two containers.

Below is a sample .yml file for setting up wordpress on your host (your local laptop or your virtual machine).

version: '3.3'

     image: mysql:5.7
       - db_data:/var/lib/mysql
     restart: always
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

       - db
     image: wordpress:latest
       - "8000:80"
     restart: always
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress
    db_data: {}

This file has two main sections, version and services.

The version specifies the version of docker-compose. You need to check the docker documentation to understand which all versions are supported for you.

The services section specifies services required for your application. In this compose file, there are two services, db and wordpress.

  • The db service specifies the MYSQL database version and other configurations.
  • The wordpress service specifies that it is dependent on db service and it will be available on port 8080 on the host and it also sets the environment variables which are used by wordpress.

I hope this article has given you a high level idea of what is docker compose. I would be happy to know your thoughts.