Best Way To Install Docker Compose on Rocky Linux 8

Posted on

Best Way To Install Docker Compose on Rocky Linux 8

In this guide, we’ll walk through the process to Install Docker Compose on Rocky Linux 8. Docker Compose is an invaluable tool for defining and managing multi-container applications. It allows you to define your entire application stack in a single file, enabling you to bring up and tear down complex environments with a single command. Imagine building an application with a NodeJS front-end, a Python backend, and a PostgreSQL database. Instead of managing each container separately, Docker Compose lets you define them all in one place.

For example, if you’re developing a project that combines NodeJS and MongoDB, Docker Compose simplifies the process. Instead of starting each container individually, you can define a single docker-compose.yml file that specifies how these containers should be built, linked, and run. This creates a cohesive, manageable unit for your application.

Let’s dive into the steps to Install Docker Compose on Rocky Linux 8.

To follow this guide effectively, ensure you have a non-root user with sudo privileges on your Rocky Linux 8 server. If not, you can refer to our guide on Initial Server Setup with Rocky Linux 8. Furthermore, Docker itself must be installed. Our guide on Install and Use Docker on Rocky Linux 8 will walk you through that process.

Now, let’s proceed with the installation steps for Install Docker Compose on Rocky Linux 8.

Set up Docker Compose on Rocky Linux 8

First, confirm your Docker installation is functional by checking its version:

docker --version
**<mark>Output</mark>**
Docker version 23.0.3, build 3e7cbfd

Download Docker Compose Binary

Next, download the Docker Compose binary. Visit the GitHub Docker Compose release page and use the curl command to download the latest binary package suitable for your system architecture. You can learn more about the curl command in our guide: Use Curl Command in Linux.

sudo curl -L "https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

This command downloads the Docker Compose binary and places it in the /usr/local/bin directory.

Docker Compose File Permission

Now, set the correct execute permissions for the Docker Compose binary:

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

Verify Docker Compose Installation

Verify the installation by checking the Docker Compose version:

sudo docker compose version
Verify Docker Compose Installation Rocky Linux 8

How To Use Docker Compose on Rocky Linux 8?

To ensure Docker Compose is working as expected, let’s test it with a simple "hello-world" container.

First, create a directory for your docker-compose file:

mkdir compose-test

Navigate into the new directory:

cd compose-test

Now, create the docker-compose.yml file. This file defines the services, networks, and volumes for your Docker containers. Several versions of the compose file format are available, including 1, 2, 2.x, and 3.x. Note that Compose V1 is no longer supported after June 2023.

Use your preferred text editor (e.g., vi, nano) to create the file. For example, using vi (refer to our guide on How to Work with the Vi Editor in Linux):

sudo vi docker-compose.yml

Add the following configuration for a simple "hello-world" container:

version: '2'
services:
   hello-world:
      image:
         hello-world:latest

Save and close the file.

Launch the container from within the compose-test directory:

sudo docker compose up

The output will show that Docker Compose is working correctly on your Rocky Linux 8 system.

Uninstall Docker Compose

To uninstall Docker Compose, first remove the docker-compose binary:

sudo rm /usr/local/bin/docker-compose

Then, remove the Docker Compose package (though this step might not be necessary if you installed it solely via the binary):

sudo dnf remove docker-compose

Finally, remove any unused dependencies:

sudo dnf autoremove

Conclusion

By following the steps above, you can successfully Install Docker Compose on Rocky Linux 8 and begin managing multi-container applications with ease.

You may also find these articles helpful:

  • Install and Configure OpenVPN on Rocky Linux 9
  • Fix Error Failed to load SELinux policy freezing
  • Run Binary Files on Linux

Alternative Installation Methods

While the above method of downloading and installing the Docker Compose binary is a straightforward approach, alternative methods exist, each with its own advantages. Here are two alternative approaches for Install Docker Compose on Rocky Linux 8:

1. Installing Docker Compose as a Plugin (Recommended for Newer Docker Versions)

Newer versions of Docker strongly encourage using Docker Compose as a plugin. This method integrates Compose directly into the Docker CLI, providing a more seamless experience. The advantage of this approach is that it simplifies management and ensures compatibility with the Docker ecosystem.

This method leverages the docker command directly and eliminates the need for a separate docker-compose command.

To install Docker Compose as a plugin on Rocky Linux 8:

  1. Verify Docker Version: Ensure you have a recent version of Docker installed. The plugin approach is generally recommended for Docker versions 1.29 and later.

    docker version
  2. Install the Docker Compose Plugin using DNF: This is the preferred method on Rocky Linux 8 if available. It relies on the official repositories (or enabled third-party repositories).

    sudo dnf install docker-compose-plugin

    If the above command doesn’t find the package, you might need to enable the Docker CE repository. Refer to the official Docker documentation for instructions on setting up the Docker CE repository on Rocky Linux 8.

  3. Verify the Installation: After installation, verify that the plugin is correctly installed by checking the Docker version again.

    docker version

    The output should indicate that Compose is integrated into the Docker CLI. You can now use commands like docker compose up instead of docker-compose up.

Example Usage:

The usage remains largely the same. Create your docker-compose.yml file as before:

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"

Then, from the directory containing the docker-compose.yml file, run:

docker compose up -d

This will start the Nginx container in detached mode.

2. Installing Docker Compose with Pip (Python Package Installer)

Another alternative is to use pip, the Python package installer, to Install Docker Compose on Rocky Linux 8. This method can be useful if you prefer managing Compose as a Python package or if the binary installation fails. However, it requires Python and pip to be installed on your system.

  1. Install Python and Pip: If Python and pip are not already installed, install them:

    sudo dnf install python3 python3-pip
  2. Install Docker Compose using Pip:

    sudo pip3 install docker-compose

    It’s recommended to use sudo to install the package system-wide.

  3. Verify the Installation: Check the Docker Compose version to confirm successful installation:

    docker-compose --version

Important Considerations for Pip Installation:

  • Path: Ensure that the directory where pip installs the docker-compose executable is in your system’s PATH environment variable. If not, you might need to add it manually. The default location is often ~/.local/bin or /usr/local/bin.

  • Dependencies: pip will handle the installation of any necessary Python dependencies for Docker Compose.

  • Version Management: pip allows for easy upgrading and downgrading of Docker Compose versions. For example, to upgrade to the latest version:

    sudo pip3 install --upgrade docker-compose

Code Example (Same as Before):

The docker-compose.yml file remains the same:

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"

And the command to start the application:

docker-compose up -d

These alternative methods provide flexibility in how you choose to Install Docker Compose on Rocky Linux 8, allowing you to select the approach that best suits your needs and system configuration. The plugin method is generally preferred for newer Docker versions, while the pip method provides a Python-centric approach.