Docker Compose is a tool for defining and running multi-container Docker applications. Just like docker, the docker-compose source code is also officially hosted on GitHub and it is opensource.

A Dockerfile is a template to define a single container. But in this real world, you will often need multiple containers e.g database (mysql), message queue like (Apache Kafka or JMS), web app (written in Java, NodeJs or Python) and so on to work together. If you just think in a pure docker prospective, you will have to write a lot of extra configuration code in a bash script or shell script in order to make multiple containers work together and start, stop or destroy them as per your need. In order to make such a task simpler, docker compose is what you should use.

In docker-compose, you define multiple associated containers in a docker-compose.yml file. If you want the application to be running inside a container then create a Dockerfile for your application. Once the configuration is complete, use docker-compose up to run the multiple containers specified in the docker-compose.yml file. I will create a simple spring-boot hello-world example to understand this further.

Installation of Docker Compose

Please follow the official guide for docker-compose installation. Docker Desktop (for Windows and Mac) already comes with docker-compose, so you will not need to install it separately. Only the other OS users will need to install it separately. Not to miss out, you must have Docker installed in your system in order to use docker-compose.

Understand service and project in docker-compose

In order to understand the working of docker-compose, it is important to understand the below concepts.

Service ( service): An application container that can actually run multiple instances of the same image.

Project ( project): A complete business unit consisting of a set of associated application containers.

A project can be associated with multiple services (containers).

Docker compose example with Python 3.x and redis

Download the source code from the link below.

Once you have the code downloaded,

  • Unzip it and navigate inside ./docker-compose/hello-python/
  • Run docker-compose up
  • If everything goes well as expected, the above commands should run successfully
  • Navigate to 
  • You should be able to see “hello there” message as shown in the screenshot below.

Explanation of the code

The example code is taken from the docker official site. It is using Flask python framework and Redis cache to build a simple web application (just like one you develop). Flask is a microframework for Python based on Werkzeug, Jinja 2. Basically, there are 2 containers, one with the python web-app and the other one is a Redis container.

Let’s look at the docker-compose.yml file below. As you know, this is a must have file where you define the configuration of your multiple containers.


There are basically 2 services,

web – This is build from a Dockerfile which is located in the same directory .

redis – This is created from official redis:alpine image.

NOTE: Based on your need you can have multiple containers defined under services. These containers are built from Dockerfile or pulled from docker registry as per your need.

Look at the Dockerfile below. As you can see, it builds a docker-image with python and having the code ( in the right directory.


Finally, our beloved has the hello-there code.


NOTE: After making any code change, you need to rebuild the containers using docker-compose up --build

Docker compose example for a Spring boot + MongoDB + Nginx

This code is already present in the above-downloaded example if you have not, please download it from here.

  • Navigate inside ./docker-compose/springboot-nginx-mongodb
  • Run docker compose up --build
  • Open http://localhost to make sure example is working with Nginx
  • Open http://localhost:8080 to make sure spring-boot app is working

Now, let us look into the docker-compose.yaml file to understand how a Spring boot app is made to work with MongoDB and Nginx.

NOTE: The docker-compose file can have .yaml or .yml extension.


There are several things to note in the above compose file.

restart:always – This is a container restart policy, this value is ignored when using swarm.

volumes – Binds the host folder/file with container folder/file

dependes_on – Docker compose allows container dependencies. In this case bb-nginx starts after app container is started. Similarly, app is dependent on bb-mondo.

You can stop the running containers using docker-compose stop. Docker compose up and compose stop respects the dependencies order. So for the above example, bb-mongo will start first, then the app, then bb-nginx. In the same way, bb-nginx will be stopped first, then the app and finally bb-mongo stops.

By |Last Updated: July 16th, 2019|