How to install Docker Compose on Ubuntu 20.04

Docker Compose is Docker’s container orchestrator. And how does an orchestrator work in an orchestra? It governs how a band should behave / play during a given performance or song.With Docker Compose it’s the same, but we are the conductors! We who will govern this behavior through a file called docker-compose, similar to Dockerfile, written in YAML (recursive acronym for YAML Ain’t Markup Language) is a human-readable data encoding format, which makes it easy to read and understand what a Compose does .

A practical example of how Docker Compose works: 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 want to use a proxy at the front. The scenario is very typical and an infrastructure guy sets up this easy environment in less than a day.

Now imagine that for each client, we need to perform this setup at least three times: one for Development, Homologation and another for Production. It got a little complicated, right?

Another detail, if these environments are in the cloud and there is an Instance / VM for each application, this can generate a lot of cost, wasted resources and unnecessary time spent, not to mention the work it takes to set up this infrastructure every time a new project appears. And in today’s world, we need to be agile to deliver value to the customer.

Another major disadvantage of this process is that it is highly manual, so there is a possibility of human error.

The Compose File

In the aforementioned Compose file, we describe the infrastructure as code and how it will behave when started. If I say that 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.

Docker Compose yml example

Another nice thing is that we can define the behavior that Docker will have if one of the containers fails. I describe in Compose that, in case the database fails for some reason, the Docker will upload another one immediately. I can isolate these applications on a separate network and what volumes these applications will use to persist data. We can upload all these services described in Compose with just one command.

Read: How to install and setup Docker on Linux/Ubuntu 18.04

What can I do in Compose?

Another interesting point to mention are the environment variables which we can configure in Compose, passing the variables that will be used by our application in a certain environment, when the services are active.

In summary, using Docker Compose, instead of the administrator running the docker by hand for each container and uploading the separate services, linking the application containers manually, we have a single file that will perform this orchestration and will upload the services / containers at once. This decreases the responsibility of Sysadmin or the developer of having to manage the deployment and running all the commands to have an application running with all its dependencies. For more information about Docker compose, refer to its official documentation.

Read: How to create new users in a Docker container?

Docker Compose Ubuntu installation

To install Docker Compose, we would first need to add the key into the /usr/local/bin directory:

sudo curl -L “https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)” -o /usr/local/bin/docker-compose

Docker compose install ubuntu

Now, let’s mark the binary we downloaded as executable:

sudo chmod +x /usr/local/bin/docker-compose

In order to check whether the installation was completed successfully, run the command below to check the version:

docker-compose –version                                            

version of docker compose

docker compose version check

Using Docker Compose

Let us create a container that uses Docker compose. For this we will use an already existing image in the public Docker registry, the Hello World image.

For that we will need to create a folder for our YAML file in which we will call the image above :

  • mkdir hello-world
  • cd hello-world

Next we will create the YAML file using :

nano docker-compose.yml

And insert the text :

Now save and exit the file.

Before running Docker compose, let us have a look at the local Docker images :

docker images

Now let us run docker compose using the command :

docker-compose up

docker-compose up

To see up and running, we run :

docker ps -a

docker ps -a

Uninstalling Docker Compose

To uninstall Docker Compose, simply remove the binary by typing:

sudo rm /usr/local/bin/docker-compose

Remember that the container will be executed only while the command is being executed and when it finishes it will be interrupted. We hope that you were able to install Docker Compose on Ubuntu without any problems.


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

amin nahdy

Amin Nahdy, an aspiring software engineer and a computer geek by nature as well as an avid Ubuntu and open source user. He is interested in information technology especially Linux based ecosystem as well as Windows and MacOS. He loves to share and disseminate knowledge to others in a transparent and responsible way.

Leave a Reply

Close Menu