Docker-Compose Explanation

Getting started with docker-compose

Introduction#

Docker-compose is a tool that combines and runs multiple containers of interrelated services with a single command. It is a tool to define all the application dependencies in one place and let the Docker take care of running all of those services in just one simple command docker-compose up.

If you are following this course on your local machine remember to install the Docker Compose from the official documentation website.

Clone the git repo using git clone https://github.com/venky8283/Docker.git . Type git checkout 4de325cf1da2428e757be4f2bcc53f35c384c598 to get to the code used in this lesson.

Below is an example of docker-compose file that combines a MySQL database service with a Python flask app that implements a simple login and logout web application.

Docker Compose file

Docker Compose file explanation#

The Compose file is in YAML format. It’s a very simple language. You can validate the YAML file using any of the online validator tools. Here is one for reference.

So, without further ado, let’s understand the docker-compose file line by line.

  • version ‘3’: Like other software, docker-compose also started with version 1.0. At the time of writing this course, the current latest version of Compose file is 3.7.

    We have specified the version of Compose file we will be using and Docker will provide the features accordingly.

    Compose versions are backward compatible, hence it is recommended to use the latest version.

  • services: The services section defines all the docker images required and need to be built for the application to work. In short, it’s the collection of all different components of the application that are dependent on each other.

    We have two services namely, web and database. In Compose version 3, we can have multiple containers of the same service as well.

    We will see that in the next section, but if you are curious, you can check here under the deploy section in the compose file.

  • web: The name web is the name of our Flask app service. It can be anything. Docker Compose will create containers with this name.

  • build: This clause specifies the Dockerfile location. ‘.’ represents the current directory where the docker-compose.yml file is located and Dockerfile is used to build an image and run the container from it. We can also provide the absolute path to Dockerfile instead of the current working directory symbol.

  • ports: The ports clause is used to map the container ports to the host machine’s port. It creates a tunnel from the specified container port to the provided host machine’s port.

    This is the same as using the -p 5000:5000 option to map the container’s 5000 port to the host machine’s 5000 port while running the container using the docker run command.

  • volumes: This is the same as the -v option used to mount disks in docker run command. Here, we are attaching our code files directory to the container’s /code directory so that we don’t have to rebuild the images for every change in the files.

    This will also help in auto-reloading the server when running in debug mode.

  • links: Links literally link one service to another. In the bridge network, we have to specify which container should be accessible to what container using a link to the respective containers.

    Here, we are linking the database container to the web container, so that our web container can reach the database in the bridge network.

  • image: If we don’t have a Dockerfile and want to run a service directly using an already built docker image, then specify the image location using the ‘image’ clause. Compose will pull the image and fork a container from it.

  • environment: Any environment variable that should be present in the container can be created using the environment clause. This does the same work as the -e argument in the docker run command while running a container.

All the above clauses or keywords are commonly used keywords and are enough to start a development workflow. However, there are some advanced-level keywords that are used in production.

As we move forward in the course, we will make this docker-compose file production-ready.

In the next lesson, we will play around with docker-compose using commands so that you can get used to it.

About this Module
Docker-Compose Commands Overview
Mark as Completed
Report an Issue