Install and Use Docker Compose on Linux Mint 21

What is Docker Compose? Docker Compose is used for running multiple containers as one service. For example, if you were to run an application requiring PHP and MySQL, it would be unreasonable and tiresome to have the separate containers for each. Docker compose enables creating one file which would start both containers as one service and avoid the need to start them separately. The file is called docker-compose.yml. This guide will show you how to install and use Docker Compose on Linux Mint 21 machine.

Docker Compose File

The file contains all application service configurations in YAML format. These are all the properties defined to run a container. It includes the container name, image to be used, ports and any other property needed. This single file will contain the properties of all the containers required for an application such that when the file is run, all the defined containers will be started. Docker compose files have evolved through different stages, from version 1 and currently at version 3. It is required to specify the version for every docker compose file.

What are the Benefits of Using Docker Compose?

Docker compose file contains different container configurations that are needed to run a service, all in one file. This improves organization and avoid having to run the different containers that make up one service separately. If three applications are needed for a certain service, docker compose will enable running them as one file as opposed to running three containers separately.

Installing Docker Compose in Linux Mint

In the next steps we are going to see how to install docker compose in Linux Mint as well as how to create and use docker compose files.

In this guide you are required to have already installed Docker. If not check out how to install Docker CE in Linux Mint. Install curl and wget command line tools. On your terminal, run the following command to download docker compose.

sudo apt update
sudo apt-get install docker-compose-plugin

Check version to confirm installation was successful.

$ docker-compose version
Docker Compose version v2.20.3

Docker Compose Commands

Use –help command to check docker compose and what they do

docker-compose --help

Running Containers with Docker Compose

Docker compose present different ways of running containers. These are up, run and exec. What then is the difference between docker-compose up, docker-compose run and docker-compose exec?

Docker Compose Up

To start a docker compose file (a file containing different containers with their specific configurations), use docker-compose up. This will start all the services defined in the YAML file.

docker-compose up

It you want to run one container defined in the .yml file, specify it in the docker-compose up command. For example, if the .yml file contains mariadb, PHP and Apache, and you wish to run mariadb alone, run a command as below

docker-compose up mariadb

Docker Compose Run

Run command always defines a new container outside those defined in the YAML file. This will be a standalone container running separately from those in the docker compose file, and will not interfere with those already defined in the docker compose file.

docker-compose run <image-name>

Docker Compose Exec

Exec command enables you to interact with an already running container

Example: Create and run LAMP Stack with Docker Compose

LAMP stand for Linux Apache MariaDB and PHP. It provides an environment for creating websites locally. Since LAMP consists of various applications, PHP, Apache and MariaDB, docker compose comes in handy whereby with the use of docker compose yaml file, the services can be defined in a single file and started all at once.

First, create a directory for the project. Within it, create a directory to hold Apache pages.

mkdir -p ~/docker-lamp && cd ~/docker-lamp
mkdir html

Create the following content in the index.php file and save it

tee html/index.php<<EOF
<?php phpinfo(); ?>
EOF

This means the default Apache page will only show information about php.

Next, create docker-compose.yml file in docker-lamp directory with the content as shown below and save the file.

$ vim docker-compose.yml
version: '3'
services:
  php-apache:
    image: php:8-apache
    ports:
      - 80:80
    volumes:
      - ./html:/var/www/html:z
    links:
      - 'mariadb'

  mariadb:
    image: mariadb:10
    volumes:
      - mariadb:/var/lib/mysql
    environment:
      TZ: "Africa/Nairobi"
      MYSQL_ALLOW_EMPTY_PASSWORD: "no"
      MYSQL_ROOT_PASSWORD: "StrongRootPassword"
      MYSQL_USER: 'testuser'
      MYSQL_PASSWORD: 'TestDBPassword'
      MYSQL_DATABASE: 'testdb'
volumes:
  mariadb:

To be able to run the containers, ensure to have pulled the images to be used for the containers

docker pull php:8-apache
docker pull mariadb:10

Sample Output:

root@cloudspinxlab:~/docker-lamp# docker pull php:8-apache
8-apache: Pulling from library/php
2d429b9e73a6: Pull complete
55fb40e0ed27: Pull complete
14300e53af86: Pull complete
0e331794fb8b: Pull complete
486b5746d255: Pull complete
094b35f36371: Pull complete
90f94700ac21: Pull complete
d4f88045b4a9: Pull complete
d8a024347f84: Pull complete
e47085bae1f8: Pull complete
1b162ebcd770: Pull complete
0ea7369b3f2d: Pull complete
5b715b7c6968: Pull complete
4f4fb700ef54: Pull complete
Digest: sha256:18332cc2f59b5d953c46c570309fca971c6d1fbf8295b81e7ce582dbb76010d0
Status: Downloaded newer image for php:8-apache
docker.io/library/php:8-apache
root@cloudspinxlab:~/docker-lamp# docker pull mariadb:10
10: Pulling from library/mariadb
6414378b6477: Already exists
95fcb1e2388c: Pull complete
c31c961068dd: Pull complete
a67827fd8887: Pull complete
7e99aaa10ae0: Pull complete
a670857f0fb2: Pull complete
0aca29d0b893: Pull completes
b2f01a3c4e4c: Pull complete
Digest: sha256:79cf033abf9089f27dd4c3e831cbef83048a52d6f52783930b647c56224d95c3
Status: Downloaded newer image for mariadb:10
docker.io/library/mariadb:10
root@cloudspinxlab:~/docker-lamp#

To run docker-compose.yml, use the command below

docker-compose up

Sample Output:

root@cloudspinxlab:~/docker-lamp# docker-compose up
WARN[0000] /root/docker-lamp/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 4/1
 ✔ Network docker-lamp_default         Created                                                                                                                                                       0.2s
 ✔ Volume "docker-lamp_mariadb"        Created                                                                                                                                                       0.0s
 ✔ Container docker-lamp-mariadb-1     Created                                                                                                                                                       0.0s
 ✔ Container docker-lamp-php-apache-1  Created                                                                                                                                                       0.0s
Attaching to mariadb-1, php-apache-1
mariadb-1     | 2024-11-27 18:51:27+03:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.11.10+maria~ubu2204 started.
mariadb-1     | 2024-11-27 18:51:27+03:00 [Warn] [Entrypoint]: /sys/fs/cgroup///memory.pressure not writable, functionality unavailable to MariaDB
mariadb-1     | 2024-11-27 18:51:27+03:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mariadb-1     | 2024-11-27 18:51:27+03:00 [Note] [Entrypoint]: Entrypoint script for MariaDB Server 1:10.11.10+maria~ubu2204 started.

To verify if the containers are running, list all running containers

docker ps -a

Sample Output:

root@cloudspinxlab:~# docker ps -a
CONTAINER ID   IMAGE                         COMMAND                  CREATED          STATUS                   PORTS                               NAMES
c3fdb07fa30d   php:8-apache                  "docker-php-entrypoi…"   20 minutes ago   Up 20 minutes            0.0.0.0:80->80/tcp, :::80->80/tcp   docker-lamp-php-apache-1
02a1e23bfbf6   mariadb:10                    "docker-entrypoint.s…"   20 minutes ago   Up 20 minutes            3306/tcp                            docker-lamp-mariadb-1
88bf442f0265   passbolt/passbolt:latest-ce   "/usr/bin/wait-for.s…"   13 days ago      Exited (0) 11 days ago                                       root-passbolt-1
ef417199cc5d   mariadb:10.11                 "docker-entrypoint.s…"   13 days ago      Exited (0) 11 days ago                                       root-db-1
1ef30e6c4907   nginx                         "/docker-entrypoint.…"   5 weeks ago      Exited (0) 5 weeks ago                                       docker-nginx

Also go to your browser and type http://localhost. You will the default php page for a successful installation.

This has been a step-by-step guide on how to install and use Docker Compose.

Check out more articles:

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

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

Related Post

In this article, you will learn how to install Nodejs on Rocky Linux / AlmaLinux 8 with NPM. Nodejs is […]

MariaDB is a community version of MySQL database server. The latest stable version of MariaDB is 10.6. In a database […]

Xfce is a lightweight desktop environment for UNIX-like operating systems designed to run fine on minimal system resources ie (small […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.