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 20 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 20
In the next steps we are going to see how to install docker compose in Linux Mint 20 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 20. On your terminal, run the following command to install docker compose.
sudo apt update sudo apt -y install docker-compose
Docker Compose Commands
Use –help command to check docker compose and what they do
$ docker-compose --help
You should see an output as below:
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.
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: How to Create and run LAMP Stack with Docker Compose
LAMP stand for Linux Apache MariaDB and PHP. It provides an environment for creating websites locally. To learn more about LAMP, check out our guide on how to install LAMP in Linux Mint.
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 defines in a single file and started all at one go.
First, create a directory for the project. Within it, create a directory to hold Apache pages.
sudo mkdir -p docker-lamp/DocumentRoot cd docker-lamp/DocumentRoot sudo nano index.php
Paste the following content in the index.php file and save it
<?php phpinfo(); ?>
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.
To be able to run the containers, ensure to have pulled the images to be used for the containers
$ docker pull php:7.2.1-apache $ docker pull mariadb:10.1
To run docker-compose.yml, use the command below
$ docker-compose up
You will see a sample output as below:
To verify if the containers are running, list all running containers
$ docker ps -a
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 in Linux Mint 20. I hope you have enjoyed. Check out more of our interesting Linux installation guides.
- How to Install Podman on Oracle Linux
- How to Install and Use MongoDB 4 on Linux Mint
- How to Install Vagrant on Oracle Linux
- Install Development Tools on Solus Linus Machine