Docker Compose is the Docker container conductor. And how does a conductor work in an orchestra? It directs how a band should play during a particular performance or song: high or low notes, compass, rhythm etc.
It works the same way for Docker Compose, but in this case we are the conductors!
With Docker Compose it will work the same way, but we will be the conductors! It’s us who will conduct this behavior through the docker-compose file. Similar to Dockerfile, written in YAML (YAML Ain’t Markup Language), a human readable data encoding format. That makes it easy to read and understand what a Compose does!
A practical example of how Docker Compose works is: Imagine that we have a Java or PHP application and that this application depends on a MySQL database and, to make this application available on the internet, we need to use a proxy in front.
The scenario is quite typical and an infrastructure guy sets up this easy environment in less than a day. Technology, that’s the point!
Now imagine that for every client we need to do this setup at least three times: once for Development, Approval and another for Production. A little complicated, right?
Not to mention that if these environments are in the cloud and there is an Instance/VM for each application, this can generate a lot of cost, wasted resource and unnecessary time, not to mention the hard work it takes to set up this infra every time a new project comes up. And in this world we live in we need to be agile to deliver customer value.
One other huge disadvantage of this process is that it is highly manual, so there is a possibility of human error.
At this point I have tried to make you understand the issue we had in infra and explain how Docker Compose can help solving such difficult task!
The Compose File
In the Compose file that I mentioned at the beginning of this text, we describe the infrastructure as code and how it will behave when it starts. If I say I need a database to run my Java/Php application, I describe it in my Compose and say that my application depends on my Mysql database container to run.
One nice thing is that we can define the behavior that Docker will have if one of the containers fails. I describe in Compose that if the database fails for some reason, Docker will immediately raise another one. I can isolate these applications on a separate network and what volumes these applications will use to persist the data. We will upload all these services described in Compose with just one command.
In order to illustrate, the code below shows how a Docker Compose file and how we declare services:
version: '3' services: db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: wordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: - db image: wordpress:latest ports: - "8000:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress volumes: db_data:
What can I do with Compose?
The environment variables are also worth talking about. We can configure these variables in Compose using the environment. Passing the variables that will be used by our application in a given environment when services are going up.
In the case of the database in our example, we pass the database host, port, username and password that WordPress will use to be able to install and then work.
In summary, using Docker Compose, instead of having the admin run the docker run manually for each container and upload the separate services, linking the application containers, we have a single file that will do this orchestration and upload the services/containers at once.
This decreases the responsibility of Sysadmin or the developer to have to manage the deploy and worry about running all these commands to get your application running with all its dependencies.
In this article we talked about the Docker Compose, a powerful tool that will help us, humans, in our daily developing activities. We will certainly make a tutorial in our community to show how it works in the practice.
If you have any question or comment regarding this subject, don’t forget to leave us a comment!