FedoraRHEL Based

How To Install Docker Compose on Fedora 42

Install Docker Compose on Fedora 42

Docker Compose has revolutionized the way developers create and manage multi-container applications. With Fedora 42 being the latest release of this powerful Linux distribution, many users are looking to harness the capabilities of Docker Compose for their development workflows. This guide will walk you through the complete process of installing and configuring Docker Compose on Fedora 42, offering multiple installation methods, troubleshooting tips, and best practices to ensure a smooth experience.

Understanding Docker and Docker Compose

Docker is a platform that enables developers to build, package, and distribute applications as containers. Containers are lightweight, standalone executable packages that contain everything needed to run an application, including code, runtime, system tools, libraries, and settings. This technology ensures that applications work consistently across different environments.

Docker Compose extends Docker’s functionality by allowing you to define and manage multi-container applications. Using a YAML file, you can configure all the services, networks, and volumes needed for your application, then start everything with a single command. This simplifies complex setups and ensures consistency across development, testing, and production environments.

Evolution of Docker Compose

Docker Compose has evolved significantly since its introduction. Initially released as a standalone tool (V1), it required the docker-compose command. The newer version (V2) is implemented as a plugin for the Docker CLI, using the docker compose command format. Understanding this distinction is crucial as installation methods differ between versions.

Prerequisites for Installation

Before installing Docker Compose on Fedora 42, ensure your system meets the following requirements:

  • 64-bit version of Fedora 42
  • Sufficient disk space (at least 2GB free)
  • Administrative (sudo) access
  • Updated system packages

Updating Your System

Start by updating your Fedora system to ensure all packages are current:

sudo dnf update

Removing Conflicting Packages

If you have previously installed Docker or Docker-related packages, it’s essential to remove them to avoid conflicts:

sudo dnf remove docker \
               docker-client \
               docker-client-latest \
               docker-common \
               docker-latest \
               docker-latest-logrotate \
               docker-logrotate \
               docker-selinux \
               docker-engine-selinux \
               docker-engine

This command removes any existing Docker installations that might conflict with the new installation.

Method 1: Installing Docker and Docker Compose from Fedora Repositories

Fedora’s official repositories provide Docker-related packages that are tested and optimized for the distribution. This is generally the simplest and most recommended method for most users.

Understanding Fedora’s Docker Packages

Fedora 41 and newer versions provide the following Docker-related packages:

Fedora RPM Name Docker RPM Name Description
containerd containerd Container runtime
docker-buildx docker-buildx-plugin Docker buildx plug-in
docker-cli docker-ce-cli Docker command line interface
docker-compose docker-compose-plugin Compose v2 implemented as a plugin
docker-compose-switch n/a Provides command line compatibility
moby-engine docker-ce Server component for Docker

Note that mixing RPMs from Docker with Fedora-provided RPMs is not recommended and may be blocked by dnf.

Installation Steps

1. First, install the Docker CLI and containerd:

sudo dnf install docker-cli containerd

2. Next, install Docker Compose as a plugin:

sudo dnf install docker-compose

3. If you want the docker-compose command available on the command line (instead of using docker compose):

sudo dnf install docker-switch

4. Start and enable the Docker service:

sudo systemctl enable --now docker

This installation method provides Compose v2 features and capabilities while using packages specifically optimized for Fedora.

Method 2: Installing from Docker’s Official Repository

For users who prefer to use Docker’s official packages or need specific versions, installing from Docker’s repository is an excellent option.

Adding Docker’s Repository

1. Install the required packages to set up the repository:

sudo dnf install -y dnf-plugins-core

2. Add Docker’s official repository:

sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo

Installing Docker Engine and Compose

1. Install the latest versions of Docker Engine, Docker CLI, containerd, and Docker Compose plugin:

sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

2. If prompted to accept the GPG key, verify that the fingerprint matches 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35, and if so, accept it.

3. Start and enable the Docker service:

sudo systemctl enable --now docker

Installing Specific Versions

If you need a specific version of Docker, you can list available versions and install the one you need:

1. List available versions:

dnf list docker-ce --showduplicates | sort -r

2. Install a specific version by its fully qualified package name:

sudo dnf install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io docker-buildx-plugin docker-compose-plugin

Replace <VERSION_STRING> with the desired version (e.g., 3:28.1.1-1.fc42).

Method 3: Manual Installation from Package

For situations where repository-based installations aren’t feasible, you can manually install Docker Compose.

Downloading the Docker Compose Binary

1. Create the plugins directory if it doesn’t exist:

DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
mkdir -p $DOCKER_CONFIG/cli-plugins

2. Download the Docker Compose binary:

curl -SL https://github.com/docker/compose/releases/download/v2.35.1/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose

3. Apply executable permissions:

chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose

For a system-wide installation (available to all users), use these commands instead:

sudo mkdir -p /usr/local/lib/docker/cli-plugins
sudo curl -SL https://github.com/docker/compose/releases/download/v2.35.1/docker-compose-linux-x86_64 -o /usr/local/lib/docker/cli-plugins/docker-compose
sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose

This method gives you more control over which version to install and where to install it.

Post-Installation Configuration

After installing Docker and Docker Compose, there are several important configuration steps to optimize your setup.

Starting and Enabling Docker Service

If you haven’t already done so during installation, start and enable the Docker service:

sudo systemctl enable docker
sudo systemctl start docker

Enabling the service ensures Docker starts automatically when your system boots.

Adding User to Docker Group

By default, Docker commands require sudo privileges. To run Docker commands without sudo, add your user to the docker group:

sudo usermod -aG docker $USER

Log out and log back in for the changes to take effect. This change allows you to run Docker commands without prefixing them with sudo.

Configuring Docker Daemon Settings

For advanced configuration, you can create or modify the Docker daemon configuration file:

sudo mkdir -p /etc/docker
sudo nano /etc/docker/daemon.json

Here’s an example configuration that sets the default address pools for Docker networks:

{
  "default-address-pools": [
    {"base": "172.17.0.0/16", "size": 24},
    {"base": "172.18.0.0/16", "size": 24}
  ]
}

After editing, restart Docker to apply the changes:

sudo systemctl restart docker

Verifying Your Installation

Before proceeding with using Docker Compose, verify that everything is installed correctly.

Checking Docker and Docker Compose Versions

Verify Docker is properly installed:

docker --version

Check the Docker Compose version:

docker compose version

If you installed the docker-compose command compatibility layer:

docker-compose --version

These commands should display the installed versions without errors.

Running a Test Container

Run the hello-world container to verify that Docker is working correctly:

docker run hello-world

This command downloads a test image and runs it in a container. If successful, it will display a message confirming that your installation is working correctly.

Creating Your First Docker Compose Project

Now that Docker Compose is installed and configured, let’s create a simple multi-container application to demonstrate its capabilities.

Project Directory Structure

First, create a directory for your project:

mkdir my-docker-project
cd my-docker-project

Creating a docker-compose.yml File

Create a docker-compose.yml file with a basic web application and database setup:

nano docker-compose.yml

Add the following content:

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./website:/usr/share/nginx/html
    depends_on:
      - db
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: mydb
      MYSQL_USER: user
      MYSQL_PASSWORD: password
    volumes:
      - db-data:/var/lib/mysql
volumes:
  db-data:

Building and Starting Containers

Create the website directory and a simple HTML file:

mkdir website
echo "<html><body><h1>Hello from Docker Compose on Fedora 42!</h1></body></html>" > website/index.html

Start the containers:

docker compose up -d

This command builds and starts the containers in detached mode. You can now access the web application at http://localhost:8080.

Manage Container Lifecycle

Here are some common Docker Compose commands for managing your containers:

  • Stop containers: docker compose stop
  • Start containers: docker compose start
  • Stop and remove containers: docker compose down
  • View logs: docker compose logs
  • Rebuild and restart containers: docker compose up -d --build

Advanced Docker Compose Usage

Once you’re comfortable with basic Docker Compose operations, you can explore more advanced features to enhance your development workflow.

Environment Variables

Docker Compose supports environment variables in your configuration. Create a .env file in your project directory:

nano .env

Add environment variables:

DB_NAME=mydb
DB_USER=user
DB_PASSWORD=securepassword

Then reference these variables in your docker-compose.yml:

db:
  image: mysql:8.0
  environment:
    MYSQL_DATABASE: ${DB_NAME}
    MYSQL_USER: ${DB_USER}
    MYSQL_PASSWORD: ${DB_PASSWORD}

Networking Between Containers

Docker Compose creates a default network for your services, allowing them to communicate with each other using service names as hostnames. You can also define custom networks:

networks:
  frontend:
  backend:

services:
  web:
    networks:
      - frontend
  db:
    networks:
      - backend
  app:
    networks:
      - frontend
      - backend

Scaling Services

Scale services to run multiple instances:

docker compose up -d --scale web=3

This command starts three instances of the web service, automatically distributing requests among them.

Upgrading Docker and Docker Compose

Keeping Docker and Docker Compose updated ensures you have the latest features and security patches.

Checking for Updates

Check for available updates:

sudo dnf check-update docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Upgrading Docker from Fedora Repository

If you installed Docker from Fedora’s repository:

sudo dnf upgrade docker-cli containerd docker-compose docker-switch

Upgrading Docker from Docker’s Repository

If you installed from Docker’s official repository:

sudo dnf upgrade docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Upgrading Manual Installations

For manually installed Docker Compose, download the new version and replace the old binary:

sudo curl -SL https://github.com/docker/compose/releases/download/v2.35.1/docker-compose-linux-x86_64 -o /usr/local/lib/docker/cli-plugins/docker-compose
sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose

Replace v2.35.1 with the latest version number.

Troubleshooting Common Issues

Even with careful installation, you might encounter issues. Here are solutions to common problems.

Permission Denied Errors

If you encounter permission errors when running Docker commands:

docker: Got permission denied while trying to connect to the Docker daemon socket.

Make sure your user is added to the docker group:

sudo usermod -aG docker $USER

Log out and log back in for the changes to take effect.

Network Connection Issues

If containers can’t access the internet after upgrading to Fedora 42, try restarting the Docker service:

sudo systemctl restart docker

If issues persist, pruning networks often helps:

sudo docker network prune -f

Recent Fedora 42 users have reported networking issues related to a bug in the iptables package. Ensure you have at least version 1.8.11-5.fc42 of the iptables-nft package:

sudo dnf install iptables-nft

For persistent network issues, you might need to temporarily switch firewalld from nftables to iptables:

sudo sed -i 's/FirewallBackend=nftables/FirewallBackend=iptables/g' /etc/firewalld/firewalld.conf
sudo systemctl restart firewalld

This is a temporary workaround until the issues are fixed in upstream packages.

Docker Service Fails to Start

If the Docker service fails to start, check the logs:

sudo journalctl -xu docker

Common issues include conflicting packages or services. Make sure you’ve removed old Docker packages and that no other container runtime (like Podman) is using the same resources.

Best Practices for Docker on Fedora

Follow these best practices to ensure a smooth and secure Docker experience on Fedora 42.

Security Considerations

  • Regularly update Docker and Docker Compose to get the latest security patches
  • Use non-root users inside containers
  • Implement resource limits for containers
  • Scan images for vulnerabilities before deployment
  • Use Docker Content Trust for image verification

Resource Management

Limit container resources in your docker-compose.yml:

services:
  web:
    image: nginx
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 512M

Regular Maintenance

Implement regular maintenance procedures:

# Remove unused containers
docker container prune

# Remove unused images
docker image prune

# Remove unused volumes
docker volume prune

# Remove all unused objects (containers, networks, images, volumes)
docker system prune

Congratulations! You have successfully installed Docker Compose. Thanks for using this tutorial for installing the Docker Compose on your Fedora 42 Linux system. For additional or useful information, we recommend you check the official Docker website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button