BLOG POSTS
How to Install and Use Docker on Rocky Linux 9

How to Install and Use Docker on Rocky Linux 9

Docker has revolutionized how we deploy, manage, and scale applications by providing lightweight containerization that runs consistently across different environments. Rocky Linux 9, as a stable enterprise-grade distribution, offers an excellent platform for running Docker containers in production environments. This guide walks you through the complete installation process, basic container operations, and advanced usage patterns while covering common troubleshooting scenarios you’ll likely encounter.

How Docker Works on Rocky Linux 9

Docker operates using a client-server architecture where the Docker daemon manages containers, images, networks, and volumes. On Rocky Linux 9, Docker leverages the systemd init system for service management and uses the overlay2 storage driver by default for optimal performance with the XFS filesystem.

The containerization process relies on Linux kernel features like namespaces for process isolation and cgroups for resource management. Rocky Linux 9’s SELinux implementation adds an additional security layer, though it requires specific configuration to work smoothly with Docker containers.

Step-by-Step Docker Installation

Before installing Docker, ensure your Rocky Linux 9 system is updated and remove any conflicting packages:

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

Install the required dependencies and add the Docker repository:

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

Install Docker Engine and related components:

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

Start and enable the Docker service:

sudo systemctl start docker
sudo systemctl enable docker

Add your user to the docker group to run commands without sudo:

sudo usermod -aG docker $USER
newgrp docker

Verify the installation by running the hello-world container:

docker run hello-world

Essential Docker Commands and Usage

Once Docker is installed, these fundamental commands will handle most daily operations:

Pull and run your first container:

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

Check running containers and system information:

docker ps
docker info
docker version

Manage container lifecycle:

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

Work with images:

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

Execute commands inside running containers:

docker exec -it my-nginx /bin/sh

Real-World Examples and Use Cases

Here’s a practical example of setting up a development environment with a web application 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 production deployments, consider this 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

Docker vs Alternatives Comparison

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

Performance Optimization and Best Practices

Configure Docker daemon for production use by creating or modifying /etc/docker/daemon.json:

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

Monitor Docker performance and resource usage:

docker stats
docker system df
docker system prune -a

Key optimization strategies include:

  • Use multi-stage builds to reduce image sizes
  • Implement proper layer caching in Dockerfiles
  • Set resource limits using –memory and –cpus flags
  • Use .dockerignore files to exclude unnecessary files
  • Regularly clean up unused images and containers

Common Issues and Troubleshooting

SELinux conflicts are frequent on Rocky Linux 9. If containers fail to start with permission errors:

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 networking issues, check and reset Docker networks:

docker network ls
docker network prune
sudo systemctl restart docker

Storage driver problems can be resolved by checking disk space and cleaning up:

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

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

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

Advanced Docker Features and Integration

Docker Buildx enables advanced build features and multi-platform images:

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

Implement container health checks for production reliability:

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

Use Docker secrets for sensitive data management:

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

Rocky Linux 9 with Docker provides a robust foundation for containerized applications. The combination offers enterprise-grade stability while maintaining compatibility with the broader container ecosystem. For comprehensive documentation and advanced configurations, refer to the official Docker installation guide and Rocky Linux documentation.

The performance characteristics show Docker on Rocky Linux 9 typically uses 2-4% CPU overhead and 100-200MB RAM for the daemon, with container startup times averaging 1-3 seconds for typical applications. This makes it suitable for both development environments and production deployments requiring consistent performance and security.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

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