Loading Now

How to Install and Use Docker on Rocky Linux 9

How to Install and Use Docker on Rocky Linux 9

Docker has changed the way applications are deployed, managed, and scaled by introducing a lightweight container system that performs uniformly across various environments. Rocky Linux 9, known for its stability and enterprise-level features, serves as a superb environment for running Docker containers in production. This guide details the entire installation procedure, basic operations for containers, and advanced usage scenarios, while also addressing common troubleshooting issues you may face.

Understanding Docker on Rocky Linux 9

Docker functions with a client-server model where the Docker daemon oversees containers, images, networks, and volumes. On Rocky Linux 9, Docker utilizes the systemd service manager for management and defaults to the overlay2 storage driver to enhance performance with the XFS filesystem.

The process of containerization depends on Linux kernel capabilities such as namespaces for isolating processes and cgroups for managing resources. The SELinux implementation in Rocky Linux 9 adds another layer of security, although it may require particular settings to operate seamlessly with Docker containers.

Detailed Steps to Install Docker

Ensure your Rocky Linux 9 system is updated before you start the Docker installation and remove any related packages that may cause conflicts:

sudo dnf update -y
sudo dnf remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine

Next, install the necessary dependencies and include the Docker repository:

sudo dnf install -y dnf-utils
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Proceed to install Docker Engine and its components:

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

Activate the Docker service and ensure it runs on startup:

sudo systemctl start docker
sudo systemctl enable docker

Add your user to the Docker group to execute commands without needing sudo:

sudo usermod -aG docker $USER
newgrp docker

Confirm that Docker is correctly installed by running the hello-world container:

docker run hello-world

Key Docker Commands and Their Usage

Upon successful installation, these key commands will help you manage everyday tasks:

To pull and run your first container:

docker pull nginx:alpine
docker run -d --name my-nginx -p 8080:80 nginx:alpine

To check active containers and system information:

docker ps
docker info
docker version

To manage the container lifecycle:

docker stop my-nginx
docker start my-nginx
docker restart my-nginx
docker rm my-nginx

For handling images:

docker images
docker rmi nginx:alpine
docker pull ubuntu:22.04

To execute commands within running containers:

docker exec -it my-nginx /bin/sh

Practical Examples and Applications

Here’s a practical illustration of how to set up a development environment with a web app and database:

mkdir ~/docker-project && cd ~/docker-project
    
cat > docker-compose.yml << EOF
version: '3.8'
services:
  web:
    image: php:8.1-apache
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www/html
    depends_on:
      - db
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: secure_password
      MYSQL_DATABASE: myapp
    volumes:
      - db_data:/var/lib/mysql
    ports:
      - "3306:3306"
      
volumes:
  db_data:
EOF
    
mkdir src
echo "" > src/index.php
    
docker compose up -d

For deployment in production, consider the following multi-container monitoring stack:

docker network create monitoring
    
docker run -d --name prometheus \
  --network monitoring \
  -p 9090:9090 \
  -v /etc/prometheus:/etc/prometheus \
  prom/prometheus
    
docker run -d --name grafana \
  --network monitoring \
  -p 3000:3000 \
  -e "GF_SECURITY_ADMIN_PASSWORD=admin123" \
  grafana/grafana

Comparing Docker with Alternatives

Feature Docker Podman LXC/LXD
Daemon Required Yes No Yes
Root Privileges Needed for daemon Rootless containers Needed
OCI Compliance Yes Yes Limited
Kubernetes Integration Excellent Good Limited
Resource Usage Moderate Lower Higher

Enhancing Performance and Best Practices

To optimise the Docker daemon for production, create or modify /etc/docker/daemon.json:

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json > /dev/null <

Keep an eye on Docker’s performance and resource usage:

docker stats
docker system df
docker system prune -a

Key strategies for optimisation include:

  • Utilise multi-stage builds to minimise image sizes
  • Ensure proper layer caching in Dockerfiles
  • Apply resource limits using the –memory and –cpus flags
  • Employ .dockerignore files to exclude non-essential files
  • Regularly remove unused images and containers

Common Challenges and Solutions

SELinux conflicts can occur frequently on Rocky Linux 9. If you encounter permission errors while starting containers:

sudo setsebool -P container_manage_cgroup on
sudo semanage fcontext -a -t container_file_t "/path/to/volume(/.*)?"
sudo restorecon -R /path/to/volume

For network troubles, review and reset Docker networks:

docker network ls
docker network prune
sudo systemctl restart docker

If you face issues with the storage driver, check disk space and clean up:

df -h /var/lib/docker
docker system prune -a --volumes
sudo systemctl restart docker

If the Docker daemon fails to start, review logs and service status:

sudo systemctl status docker
sudo journalctl -u docker.service
sudo dockerd --debug

Advanced Docker Features and Integrations

Utilise Docker Buildx for enhanced build capabilities and multi-platform images:

docker buildx create --name mybuilder --use
docker buildx build --platform linux/amd64,linux/arm64 -t myapp:latest --push .

Implement health checks for containers to ensure reliability in production:

docker run -d --name healthy-app \
  --health-cmd="curl -f http://localhost/ || exit 1" \
  --health-interval=30s \
  --health-timeout=10s \
  --health-retries=3 \
  nginx:alpine

Manage sensitive data with Docker secrets:

echo "mypassword" | docker secret create db_password -
docker service create --name mysql --secret db_password mysql:8.0

Docker on Rocky Linux 9 delivers a solid foundation for running container-based applications. This combination offers enterprise-level reliability while ensuring compatibility within the broader container ecosystem. For comprehensive documentation and advanced configurations, consider reviewing the official Docker installation guide as well as the Rocky Linux documentation.

The performance profile indicates that Docker on Rocky Linux 9 typically incurs a CPU overhead of 2-4% and requires 100-200MB of RAM for the daemon, with container startup times ranging from 1 to 3 seconds for standard applications. This makes it ideal for both development settings and production environments that demand consistent performance and robust security.



This article includes information and materials from various online references. We acknowledge and appreciate the contributions of all original authors, publishers, and websites. While efforts have been made to credit sources appropriately, any unintentional oversights do not constitute copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. Should you believe any content breaches your copyright, kindly contact us for prompt review and action.

This article is intended solely for informational and educational purposes and does not infringe upon the rights of copyright holders. Any copyrighted material utilized without due credit or in violation of copyright laws is unintentional, and we will act promptly upon notification. Please note that republishing, redistributing, or reproducing any portion of this content in any form is prohibited without explicit written consent from the author and website owner. For permissions or further inquiries, please contact us.