Docker Compose is a useful tool for running multi-containers Docker applications. Using Docker Compose, we can configure the application’s services in a YAML file that helps you to create and start all services from the defined configurations. It allows different users to launch, run, communicate and close containers using a just single coordinated command.
Docker Compose works in all environments: production, staging, development, testing, etc. It offers different features that are listed below:
- It allows to create and execute multiple isolated environments on a single host
- It preserves volume data used by your services or when containers are created.
- It can reuse the existing containers which is also an effective feature.
- Using Docker Compose, we can use different environment variables in a configuration file and can customize the composition between different environments.
Table of Contents
In this article, we will show you how to install Docker Compose on RHEL 8 based distros, such as AlmaLinux/Rocky Linux/CentOS, and how to run a container using compose.
Prerequisites
- A machine running a RHEL 8 based distribution.
- Docker should be installed on your system.
- sudo or root privileges are needed to install docker-compose.
Installation steps of Docker compose on AlmaLinux/Rocky Linux 8
To install the latest release of Docker Compose v2 on your system, verify the docker installation using this command:
After that, you need to perform the following steps.
Step 1: Install Curl command
The curl command is required to download the docker-compose binaries from Github. So, install the curl command by using the following command:
sudo dnf install -y curl
Step 2: Download the Docker Compose Binary
The Docker Compose binary can be directly downloaded from the Github project’s releases. In most popular Linux distributions, Docker Compose may be included in their package managers but these are outdated. Therefore, it is best practice to download the latest binary from the releases page. Here, we are installing the 1.29.0 that is available with all its dependencies. So, download the docker-compose binary by using the following command:
sudo curl -L “https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)” -o /usr/local/bin/docker-compose
This command will download the appropriate binary for your system and place it into the /usr/local/bin
. If you want to install another version instead current available then, substitute your version in place of 1.29.2.
Step 3: Change File permissions
Once the binary file is downloaded, make this file executable by changing the permissions on a file. Use the following command for changing file permission:
sudo chmod +x /usr/local/bin/docker-compose
Step 4: Verify Docker Compose Installation
Check the installed docker-compose version to verify the installation by executing the following command:
sudo docker-compose –version
Here’s the output in my case.
Use Docker Compose
Test the installation of Docker Compose to verify that it is working correctly with docker containers. So first, create a new directory for setting up the docker-compose file.
mkdir compose-work
cd compose-work
Now, you need to create a docker-compose.yml
file. This is a configuration file in which you will describe the container’s information such as services, port bindings, networks, environment variables, and volumes. Here, we have created a sample docker-compose.yml file in which we add the following basic hello-world container based on the latest hello-world image:
version: '2' services: hello-world: image: hello-world:latest
Save changes and launch the container in a while within the ‘compose-work’ directory.
sudo docker-compose up
Theoutput should show that the docker-compose installation is working correctly:
Uninstall Docker Compose from RHEL 8 Based Distros
To uninstall the Docker Compose from your system, first remove the docker-compose binary by using the following command:
sudo rm /usr/local/bin/docker-compose
Uninstall the software by running this command:
sudo dnf remove docker-compose
In the end, uninstall all unwanted software dependencies by typing the following command:
sudo dnf autoremove
Conclusion
In this tutorial we explained how to install Docker Compose on RHEL 8-based distros and how to use Docker Compose for running a container image. Moreover, we have provided information about how to create a docker-compose YAML file and run all services using a compose command. If you have any questions or feedback feel free to leave a comment and we’ll get back to you as soon as we can.