Installing Docker Compose on Ubuntu
Purpose of the post:
- Briefly describe what Docker Compose is and what it is for
- Present a simple way to install it on Ubuntu
What is Docker Compose, and why use it?
You should always ask these two questions about a particular technology that you want to study. First you need to understand what it is, and then for what reasons it can be interesting for your day-to-day life.
In the case of Docker Compose, we have the following definition from Docker, its creator:
Compose is a tool for defining and running multi-container Docker applications.
Source: https://docs.docker.com/compose/
In other words, init is a tool that allows the definition of applications that use multiple containers Docker. In a very trivial and typical example, let's imagine a web application that needs a web server application, such as Tomcat for example, and a database, served by a database management system (DBMS) such as Postgres.
In this example, the web application accesses and writes data to a database, that is, the application server and the DBMS they need to be "connected". This situation shows that we will have at least two containers so that the application can work: the application server and the DBMS.
We could start the DBMS container with the command docker run
(https://docs.docker.com/engine/reference/run/) and
subsequently the container of the application server on the same network as the container of the DBMS
(https://docs.docker.com/engine/reference/run/#network-settings).
However, note that in addition to being manual steps, you still have to follow a certain sequence. In the case of only two containers is still reasonable, but what if there were more containers?
Avoid manual steps whenever possible to avoid problems!
Therefore, we can use Docker Compose to automate this process, defining the images of our containers, the networks they are part of, the ports that will be exposed, and much more.
Another advantage of using Docker Compose, is that it is defined using a YAML file, which is
very semantic and intuitive. This file by default should be named docker-compose.yml
(but it can be arbitrarily
defined by means of parameters). In this example we are analyzing, look how simple Docker Compose file would be:
version: "3.5"
services:
db: # Postgres database service
image: postgres: 12.2-alpine
environment:
- POSTGRES_USER = db_username
- POSTGRES_PASSWORD = user_password
- POSTGRES_DB = db_name
tomcat: # Tomcat service
image: tomcat: 10.0.0
ports:
- "80: 8080"
volumes:
- ./app.war:/usr/lib/tomcat/webapps/app.war # Mapping your application to the container
In this file, we define two services, db
andtomcat
, respectively using the images postgres: 12.2-alpine
and
tomcat: 10.0.0
. Besides that:
- We have defined environment variables to configure the Postgres user, password and database name.
- We have mapped the
app.war
file from our computer to Tomcat'swebapps
directory, so that it can serve our application. - We have exposed the
8080
port of the container to the80
port of our computer.
To start the containers, just execute:
docker-compose up -d
And to finish them off:
docker-compose down
Note that this file can be stored in a source code repository, such as git and made available for more people. Thus, it is easy to distribute your application, even on different platforms, such as Linux and Windows.
Anyway, only advantage. More details on using Docker Compose in the post How to use Flyway? and Docker Compose documentation.
How to install Docker Compose on Ubuntu?
It is super easy to install Docker Compose on Ubuntu. There are only 2 commands:
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
sudo chmod + x /usr/local/bin/docker-compose
You need to have curl installed.
Once installed, you can check the version:
docker-compose version
I hope this post will be useful to you! Leave your criticisms, doubts, comments, or suggestions for new topics! If you liked it, I ask you to share it on your social networks so that more people can get to know this excellent tool!