HomeARCHInstall and Use Docker Compose on Linux Systems

Install and Use Docker Compose on Linux Systems

It is now obvious that containerization technology is everywhere. This technology has been in existence for quite some time now, although it gained massive popularity recently.

Containerization can be defined as the packaging of code in a lightweight executable. A container is a lightweight, standalone, and executable package of software that includes everything needed to run an application, including code, libraries, system tools, and runtime. Containers can be run using several tools such as Docker, Podman, Kubernetes, LXC/LXD, rkt etc.

Docker is a platform for building, shipping, and running applications in containers. It provides a simple and easy-to-use interface for managing containers, allowing developers to package their applications in a consistent and reproducible way, and then deploy them to any environment that supports Docker. Docker containers are portable and can run on any infrastructure, including on-premises, public cloud, or hybrid cloud environments.

You can run Docker containers directly using the Docker CLI with the docker run command. However, this is not recommended for production environments with multiple containers. Here, the recommended tool to use is Docker Compose.

In this guide, we will show you how to Install and Use Docker Compose on Linux Systems.

What is Docker Compose?

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define your application’s services, networks, and volumes in a YAML file, and then run and manage the entire application as a single entity.

With Docker Compose, you can define multiple containers and their configuration in a single file, allowing you to easily manage complex applications that require multiple services. You can define environment variables, network settings, volume mounts, and other configuration options for each container in your application, and Docker Compose will take care of starting and stopping the containers, managing their dependencies, and networking them together.

Generally, it makes it easy to manage the lifecycle of your application by providing simple commands for building, starting, stopping, and deleting your application’s containers. You can also scale your application’s services up or down by adjusting the number of containers defined in your Compose file.

Today, we will learn how to install and use Docker Compose on Linux.

Getting Started

Before we begin the installation, ensure that you have the Docker Engine installed. Get help by following the below guide:

You also need to ensure that you have all the required packages installed:

#On Debian|Ubuntu|Kali Linux
sudo apt update && sudo apt install -y curl wget

#On Rocky Linux|Alma Linux|CentOS|RHEL
sudo yum -y install curl wget

#On Fedora
sudo dnf -y install curl wget

Install and Use Docker Compose on Linux Systems

Once cURL has been installed, proceed as shown below.

#1. Download and Install Docker Compose on Linux Systems

Using cURL, download the latest Docker Compose binary file as shown:

curl -s https://api.github.com/repos/docker/compose/releases/latest | grep browser_download_url  | grep docker-compose-linux-x86_64 | cut -d '"' -f 4 | wget -qi -

Once downloaded, make the file executable using the command:

chmod +x docker-compose-linux-x86_64

Move the downloaded file to your PATH:

sudo mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose

Now verify your installation by checking the version:

$ docker-compose version
Docker Compose version v2.17.2

Now you are ready to use Docker Compose.

#2. Configure Completion For Docker Compose on Linux

Once Docker Compose has been installed, we can use it to spin Docker Containers. But first, we need to configure tab completion. This can be done using the commands:

For BASH:

sudo curl -L https://raw.githubusercontent.com/docker/compose/master/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose

For Zsh:

mkdir -p ~/.zsh/completion
curl -L https://raw.githubusercontent.com/docker/compose/master/contrib/completion/zsh/_docker-compose > ~/.zsh/completion/_docker-compose

Here, add the created path in your ~/.zshrc:

$ vim ~/.zshrc
fpath=(~/.zsh/completion $fpath)

Finally load compinit:

autoload -Uz compinit && compinit -i
exec $SHELL -l

#3. Use Docker Compose on Linux

Now we can test if Docker Compose is running as desired by spinning a test container. We need a docker-compose.yaml file to run a container. This file contains all the variables required for the container.

Create a demo file:

vim docker-compose.yml

Add these lines to it:

version: '3'  
services:
  web:
    image: nginx:latest
    ports:
     - "8088:80"
    links:
     - php
  php:
    image: php:7-fpm

Start the container:

$ docker-compose up -d
[+] Running 3/18
 ⠧ php 10 layers [⣿⣿⣄⣿⣦⠀⠀⠀⠀⠀] 43.23MB/102.4MB Pulling                                               3.7s 
   ✔ a603fa5e3b41 Download complete                                                                 1.6s 
   ✔ c428f1a49423 Download complete                                                                 0.3s 
   ⠙ 156740b07ef8 Downloading  35.38MB/91.63MB                                                      2.1s 
   ✔ fb5a4c8af82f Download complete                                                                 1.0s 
   ⠙ 972155ae644b Downloading  7.842MB/10.74MB                                                      2.1s 
   ⠙ a8e3b94fe6c1 Waiting                                                                           2.1s 
   ⠙ 93346a3f46bc Waiting                                                                           2.1s 
   ⠙ b922b67ca46b Waiting                                                                           2.1s 
   ⠙ 6137f893bda6 Waiting                                                                           2.1s 
   ⠙ 79b1a1b78461 Waiting                                                                           2.1s 
......
[+] Running 2/3
 ✔ Network ubuntu22_default  Created                                                                0.1s 
 ✔ Container ubuntu22-php-1  Started                                                                2.8s 
 ⠿ Container ubuntu22-web-1  Starting  

Check if the containers are running:

$ docker-compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
ubuntu22-php-1      php:7-fpm           "docker-php-entrypoi…"   php                 2 minutes ago       Up 2 minutes        9000/tcp
ubuntu22-web-1      nginx:latest        "/docker-entrypoint.…"   web                 28 seconds ago      Up 27 seconds       0.0.0.0:8088->80/tcp, :::8088->80/tcp

To stop the containers, use:

$ docker-compose stop
[+] Running 2/2
  Container ubuntu22-web-1  Stopped                                                                0.3s 
  Container ubuntu22-php-1  Stopped                                                                0.3s 

To delete the container use:

$ docker-compose rm -f
Going to remove ubuntu22-web-1, ubuntu22-php-1
[+] Running 2/0
  Container ubuntu22-php-1  Removed                                                                0.0s 
  Container ubuntu22-web-1  Removed                                                                0.0s 

That is it for now! You have successfully installed Docker Compose on Linux. You can now explore more on how to use Docker Compose on your own. The official documentation is provided by visiting the Docker Compose documentation page.

See more:

Klinsmann Öteyo
Klinsmann Öteyo
Self proclaimed Geek
- Advertisment -

Recent posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here