install Docker Compose
install Docker Compose

How To Install Docker Compose On Ubuntu 22.04

In this article we will learn how to install and use Docker Compose on Ubuntu 22.04 LTS Linux operating system.

Introduction

Docker Compose is a tool that allows us to define and run multi-container Docker applications using a single configuration file. It simplifies the process of managing complex applications by allowing us to define all the services, networks, and volumes needed for our application in a single file. With Docker Compose, we can define your application’s architecture, including which containers should run, how they should communicate with each other, and what volumes should be used for data persistence. This configuration is defined in a YAML file called docker-compose.yml.

Key Concepts of Docker Compose

  • Services: Each service in Docker Compose corresponds to a Docker container. Services define the image to use, environment variables, ports to expose, and more.
  • Networks: Docker Compose automatically creates a network for your application, allowing containers to communicate with each other using their service names as hostnames.
  • Volumes: Volumes can be used to persist data between container restarts. Docker Compose allows you to specify which volumes should be used for each service.
  • Environment Variables: You can set environment variables for services in the docker-compose.yml file, making it easy to configure containers without modifying their images.
  • Dependencies: Docker Compose allows you to define the order in which services should start, ensuring that dependent services are up and running before others.

Advantages of Using Docker Compose

  • Easy Setup: Docker Compose simplifies the setup of multi-container applications by defining everything in a single file.
  • Reproducibility: The docker-compose.yml file captures the entire application’s configuration, making it easy to reproduce the same environment on different systems.
  • Isolation: Each container runs in isolation, allowing you to develop, test, and deploy applications without
  • Scalability: You can scale services up or down based on your application’s needs, all from a single command.
  • Automation: Docker Compose allows you to automate the deployment and management of your application.

How To Install Docker Compose On Ubuntu 22.04

Installing Docker Compose on Ubuntu is a straightforward process. Docker Compose is not included in the default Docker installation, so we ‘ll need to install it separately. Here’s how to do it.

  • Step 1: Prerequisites
  • Step 2: Download Docker Compose
  • Step 3: Apply Executable Permissions
  • Step 4: Verify Installation

In this tutorial we will use Docker Compose version 3.2.2 which was available on th Docker Git repository.

1. Prerequisites

Before we begin, ensure that we have Docker installed on our Ubuntu system. If Docker is not installed yet, we can follow the official Docker installation guide for Ubuntu. And we have discussed about Docker installation on How to install Docker on Ubuntu 22.04 article. To verify if Docker has been installed on the system, we will check the Docker service and version.

$ sudo systemctl status docker
$ docker --version

Output :

ramansah@infodiginet:~$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2023-08-16 14:33:19 WIB; 1min 28s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 1282 (dockerd)
Tasks: 9
Memory: 22.3M
CPU: 1.830s
CGroup: /system.slice/docker.service
└─1282 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Agu 16 14:33:12 infodiginet dockerd[1282]: time="2023-08-16T14:33:12.134247879+07:00" level=info msg="St>
Agu 16 14:33:12 infodiginet dockerd[1282]: time="2023-08-16T14:33:12.265058507+07:00" level=info msg="de>
Agu 16 14:33:12 infodiginet dockerd[1282]: time="2023-08-16T14:33:12.910143004+07:00" level=info msg="[g>
Agu 16 14:33:12 infodiginet dockerd[1282]: time="2023-08-16T14:33:12.932233803+07:00" level=info msg="Lo>
Agu 16 14:33:17 infodiginet dockerd[1282]: time="2023-08-16T14:33:17.697058046+07:00" level=info msg="De>
Agu 16 14:33:18 infodiginet dockerd[1282]: time="2023-08-16T14:33:18.664013065+07:00" level=info msg="Lo>
Agu 16 14:33:19 infodiginet dockerd[1282]: time="2023-08-16T14:33:19.410150203+07:00" level=info msg="Do>
Agu 16 14:33:19 infodiginet dockerd[1282]: time="2023-08-16T14:33:19.411210897+07:00" level=info msg="Da>
Agu 16 14:33:19 infodiginet dockerd[1282]: time="2023-08-16T14:33:19.785536669+07:00" level=info msg="AP>
Agu 16 14:33:19 infodiginet systemd[1]: Started Docker Application Container Engine.
ramansah@infodiginet:~$ docker --version
Docker version 24.0.5, build ced0996

2. Download Docker Compose

We will download Docker Compose software from its official Github repository. We need to confirm the latest version available in their releases page. At the time of this writing, the most current stable version is 2.3.3, it can be check at Docker Compose GitHub releases page. First, we will create a new folder for Docker Compose software, for the example is as shown below.

$ mkdir -p ~/.docker/cli-plugins/
$ curl -SL https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose

Output :

ramansah@infodiginet:~$ mkdir -p ~/.docker/cli-plugins/
ramansah@infodiginet:~$ curl -SL https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 24.8M 100 24.8M 0 0 745k 0 0:00:34 0:00:34 --:--:-- 732k

3. Apply Executable Permissions

After downloading Docker Compose, you need to make the binary executable using the following command:

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

4. Verify Installation

To verify that Docker Compose was installed successfully, run the following command:

$ docker-compose --version

This should display the version of Docker Compose that we installed as shown below :

ramansah@infodiginet:~$ docker compose version
Docker Compose version v2.3.3

Yeay…. the Docker Compose is now successfully installed on our system.

Working with Docker Compose

In this stage we will demonstrate how to work with Docker Compose by setting up a docker-compose.yml file. In this example, we will create a simple html page that will be displayed by using Nginx as web server containerized environment. The Nginx image was pulled from Nginx official Docker hub. The steps are as follow.

1. Create a new directory call as compose-test :

$ mkdir ~/compose-test
$ cd compose-test/

2. Create a new folder for document root for your Nginx environment :

$ mkdir application
$ vi application/index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Docker Compose Testing</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
</head>
<body>

<h1>Ini adalah halaman Docker Compose untuk Demo</h1>
<p>Halaman ini disupport oleh Nginx</p>

</body>
</html>

3. Create docker-compose.yml file :

$ vi application/index.html

version: '3.7'
services:
web:
image: nginx:alpine
ports:
- "8000:80"
volumes:
- ./application:/usr/share/nginx/html

Executing Docker Compose

In this stage, we will bring our environment up by executing Docker Compose. We will use the following command to do the tasks. download the necessary Docker images, create a container for the web service, and run the containerized environment in background mode:

$ docker compose up -d
$ docker compose ps

Output :

ramansah@infodiginet:~/compose-test$ docker compose up -d
[+] Running 9/9
⠿ web Pulled 38.7s
⠿ 7264a8db6415 Pull complete 14.4s
⠿ 3a753be56661 Pull complete 14.8s
⠿ f3063829fa47 Pull complete 14.8s
⠿ 90fa5aa417ca Pull complete 14.8s
⠿ bc66f7a3f388 Pull complete 14.9s
⠿ 52972a58b96d Pull complete 14.9s
⠿ 1af8c8d8877f Pull complete 14.9s
⠿ 483e4ad92080 Pull complete 27.2s
[+] Running 2/2
⠿ Network compose-test_default Created 0.1s
⠿ Container compose-test-web-1 Started 0.9s
ramansah@infodiginet:~/compose-test$ docker compose ps
NAME COMMAND SERVICE STATUS PORTS
compose-test-web-1 "/docker-entrypoint.…" web running 0.0.0.0:8000->80/tcp, :::8000->80/tcp

By using web browser we will access the containerized web server page as shown below.

Docker Compose Nginx containerized example

 

Installing and using Docker Compose has been done in this tutorial.

Conclusion

Docker Compose is a valuable tool for managing complex containerized applications, making it easier to develop, test, and deploy multi-container environments.

(Visited 236 times, 1 visits today)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *