Installing and Using Docker Compose on Rocky Linux 9: A Step-by-Step Guide

Introduction:

Docker Compose is a powerful tool that allows you to define and manage multi-container Docker applications. It simplifies the process of deploying complex applications by defining services, networks, and volumes in a single file. In this article, we will walk you through the process of installing Docker Compose on Rocky Linux 9 and demonstrate how to use it to deploy and manage containerized applications.

Step 1: Install Docker Compose:

Before installing Docker Compose, ensure that you have Docker installed on your Rocky Linux 9 system. Docker Compose is not included in the default repositories, but you can install it using the following commands:

sudo dnf install curl
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

These commands download the latest version of Docker Compose and make it executable.

Step 2: Verify Docker Compose Installation:

To verify that Docker Compose is installed correctly, run the following command:

docker-compose --version

You should see the version number displayed, confirming that Docker Compose is installed and accessible.

Step 3: Create a Docker Compose File:

To use Docker Compose, you need to create a YAML file called docker-compose.yml. This file will define your services, networks, and volumes. Here’s an example of a basic docker-compose.yml file:

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - 80:80
  db:
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=your_root_password
    volumes:
      - ./data:/var/lib/mysql

In this example, we define two services: web and db. The web service uses the Nginx image and maps port 80 on the host to port 80 in the container. The db service uses the MySQL image, sets the root password, and mounts a volume to persist data.

Step 4: Start Docker Compose:

To start the services defined in your docker-compose.yml file, navigate to the directory where the file is located and run the following command:

docker-compose up -d

This command starts the containers in the background (-d flag) and displays the container logs.

Step 5: Manage Docker Compose Services:

Docker Compose provides several commands to manage your services. Here are some useful commands to get you started:

  • docker-compose up -d: Starts the services defined in your docker-compose.yml file.
  • docker-compose down: Stops and removes the containers defined in your docker-compose.yml file.
  • docker-compose ps: Lists the running containers associated with your Docker Compose project.
  • docker-compose logs: Displays the logs of the services defined in your docker-compose.yml file.

Conclusion:

Congratulations! You have successfully installed Docker Compose on Rocky Linux 9 and learned how to use it to deploy and manage containerized applications. Docker Compose simplifies the process of managing multi-container applications, allowing you to define services, networks, and volumes in a single file. Explore more about Docker Compose and its capabilities to orchestrate complex applications using containers. Remember to regularly update Docker Compose to benefit from the latest features and bug fixes. Happy containerizing with Docker Compose!


Leave a Reply

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