Docker compose nothing difficult

Tram Ho


Hello friends!

Today I continue to write about docker. In the previous post, I talked about everything about Dockerfile (if you are interested in reading here ) and this time I will continue with an equally interesting topic, that is docker compose. And why docking compose? Why is docker compose available in this world? So the answer is not too surprising, docker compose will help you work with Docker more effectively, simplify operations and provide a great experience.

For ease of use, your application needs to create a docker for the DB, backend and frontend. With Dockerfile, you will be able to create a container containing everything in it. However, inserting too many things into Dockerfile will cause problems in building images if you need to edit (increase the build time for example), in addition, a Dockerfile undertakes many tasks is not good Come on with the principles KISS, SRP. If you split it into separate Dockerfiles to avoid the above problems, then running one Dockerfile is also not appropriate if it is dozens or hundreds of images. In addition, if you have a container that needs to be shared (such as DB) or can simply work with any environment such as dev, test, prod … then Dockerfile is not easy to implement.

That’s why docker compose was born to solve problems that Dockerfile could not do well and increase the divinities of Docker. Let’s find out with the content below.

Docker compose – what it is

Compose is a tool for defining and running multi-container Docker applications

The definition of docker compose is also nothing special, extremely concise and easy to understand. Docker compose is an extremely simple tool to execute multiple containers at once for your application. Yes, at the same time, just a simple command you can launch a series of containers, something that Dockerfile can not do. If you pay attention in the definition, it also has a section that defines the containers, ie you do not need dockerfile to build containers anymore, very holy is not it.

So what is the definition of docker compose? Nothing hard, it’s completely understandable.

Docker compose – how to use it

With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. Using Compose is basically a three-step process:

  1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
  2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
  3. Run docker-compose up and Compose starts and runs your entire app.

Yeah very simple, isn’t it. To be able to use docker compose, you need to create a compose file like docker-compose.yml to set the containers needed for your application. It should look like this:

And then to build, run and stop the containers, you can use the following commands:


docker-compose build used to build all containers defined in the compose file. However, I often use this command to rebuild the service that has been changed by the following command docker-compose build <servicename>

docker-compose up performs creating and launching of containers. You can see here to add options corresponding to the up command. Basically, you only need 2 options -d and --force-recreate

With -d , the containers will be run as background. How to run the background when you run Dockerfile, you already know. Detached mode is definitely indispensable when launching any service. And why use --force-recreate . If there is no change, then we need to recreate the container. So my point is like this, one is not renewable, two if it has been recreated then reconstruct all the containers. This ensures consistency between the containers. If you do not like it is okay, just using docker-compose up -d is enough.

And if you just want to upload some services, then you just put the service you want to run behind the up command. For example, docker-compose up -d redis sqlserver

docker-compose down used to stop containers and delete everything created from the up command. It basically deletes the containers and networks defined in the compose file

So, how to use docker compose is difficult? No, it’s as easy as eating a candy

Steps to create a compose file

Reading the content above, you probably realize that creating a compose file is the most important step in using docker compose. Therefore, a method is required to create compose files in a reasonable manner. And I would like to share about the way I am using. If anything is unreasonable, please contribute.

First step: System design and construction of environment variables. This is the necessary step to create an overall for your application. Services to use, environment variable settings such as connect string, local path and port of the service … Basically, an application will be divided into 2 parts and I will create 2 separate compose files for that is:


Second step: Create dockerfile Although docker compose can create containers without using Dockerfile. But as I said in the previous post, Dockerfile is the heart of docker. It makes sense to try to live without a heart. Using docker without usingockerfile is the same, completely unreasonable. The reason for using Dockerfile is because it is the best option to create images for building containers. You can completely get the image from the docker hub and use it to build containers, without the need for a docker like creating a redis container from the image redis:alpine on the docker hub

However, not every image is as you wish, and you also want to add some config to your container, then you have to add the corresponding option in the compose file. Besides, if everything is contained in the compose file, maintaining it is not easy. The split will make the task of each part more clear as the dockerfile will take over the definition of the container and the compose docker will manage those containers, reasonable, right. For example, we create a Dockerfile for redis which is a directory structure like this:

then compose file we just need to adjust

Third step: Write the compose file After creating the necessary Dockerfiles for each service, now we need to gather them into the compose file and of course we will add settings like port, eviroment … For example, to create data Compose the file with the following directory structure

My like this

Since we have clearly defined the service database to use as redis and mysql, creating the corresponding Dockerfile is different easily. Just like creating environment variables to use in the compose file has been defined before, so the process of writing my compose file takes a lot of time. Absolutely easy is not it.

What is the point of creating a compose file? Difficult, where?


To answer the article title, as you have read, the docker compose nothing hard to grasp at all. Therefore, I hope you can quickly learn and use the docker compose proficiently for your project without having to fret or fear things. I wish you success in your learning and using your docker. Goodbye and see you again in the next article


Share the news now

Source : Viblo