CentOSRHEL Based

How To Install Docker on CentOS Stream 10

Install Docker on CentOS Stream 10

In this tutorial, we will show you how to install Docker on CentOS Stream 10. Docker has revolutionized the way developers build, ship, and run applications. Its containerization technology allows for consistent environments across different stages of development and deployment. In this comprehensive guide, we’ll walk you through the process of installing Docker on CentOS Stream 10, the latest cutting-edge distribution from the CentOS project.

Prerequisites

Before we dive into the installation process, ensure that your system meets the following requirements:

  • A CentOS Stream 10 system with root or sudo access
  • A stable internet connection
  • At least 2GB of RAM (4GB recommended)
  • 20GB of free disk space

System Preparation

To ensure a smooth installation process, we’ll start by preparing our CentOS Stream 10 system.

Update System Packages

First, let’s update all existing packages to their latest versions:

sudo dnf update -y

This command will fetch the latest package information and upgrade all installed packages to their newest versions.

Install Required Dependencies

Docker requires certain packages to function correctly. Install them using the following command:

sudo dnf install -y yum-utils device-mapper-persistent-data lvm2

These packages provide necessary utilities and support for the device mapper storage driver, which Docker uses by default.

Configure Firewall Settings

If you have firewalld enabled, you’ll need to configure it to allow Docker traffic. Run the following commands:

sudo firewall-cmd --permanent --zone=public --add-masquerade
sudo firewall-cmd --reload

This configuration allows Docker to manage iptables and ensure proper network functionality for containers.

Remove Conflicting Packages

CentOS Stream 10 might come with podman and buildah pre-installed. These container tools can conflict with Docker, so it’s best to remove them:

sudo dnf remove -y podman buildah

Docker Installation Process

Now that our system is prepared, let’s proceed with the Docker installation.

Add Docker Repository

To install Docker, we need to add the official Docker repository to our system. Execute the following command:

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

This command adds the Docker CE repository to your system’s package manager configuration.

Install Docker Engine

With the repository added, we can now install Docker Engine, Docker CLI, and containerd:

sudo dnf install -y docker-ce docker-ce-cli containerd.io

This command will download and install the latest stable versions of Docker Engine and its components.

Install Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. Install it with:

sudo dnf install -y docker-compose-plugin

Install Docker Buildx

Buildx is a Docker CLI plugin that extends the docker build command with many new features. Install it using:

sudo dnf install -y docker-buildx-plugin

Post-Installation Configuration

After installing Docker, there are a few additional steps to optimize its configuration and ensure it runs smoothly.

Start Docker Service

Start the Docker service and enable it to run at boot:

sudo systemctl start docker
sudo systemctl enable docker

Configure Docker Daemon

Create a daemon.json file to customize Docker’s behavior:

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<EOF
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  },
  "default-ulimits": {
    "nofile": {
      "Name": "nofile",
      "Hard": 64000,
      "Soft": 64000
    }
  }
}
EOF

This configuration sets up log rotation and increases the file descriptor limit for containers.

Set Up Logging

To manage Docker logs effectively, we’ll use logrotate. Create a new configuration file:

sudo tee /etc/logrotate.d/docker <<EOF
/var/lib/docker/containers/*/*.log {
  rotate 7
  daily
  compress
  size=10M
  missingok
  delaycompress
  copytruncate
}
EOF

This configuration will rotate Docker container logs daily, keeping them for a week.

User Management and Security

Proper user management and security practices are crucial when working with Docker.

Create Docker Group

Create a docker group to allow non-root users to run Docker commands:

sudo groupadd docker

Add Users to Docker Group

Add your user to the docker group:

sudo usermod -aG docker $USER

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

Set Proper Permissions

Ensure that the Docker socket has the correct permissions:

sudo chmod 666 /var/run/docker.sock

Security Best Practices

Implement these security measures:

  • Use official Docker images from trusted sources
  • Regularly update Docker and your container images
  • Implement resource limits for containers
  • Use Docker Content Trust for image verification

Verification and Testing

Let’s verify that Docker is installed correctly and functioning as expected.

Check Docker Status

Verify the Docker service status:

sudo systemctl status docker

You should see “active (running)” in the output.

Run Hello-World Container

Test Docker by running the hello-world container:

docker run hello-world

If successful, you’ll see a welcome message indicating that Docker is working correctly.

Verify Docker Version

Check the installed Docker version:

docker version

This command displays detailed version information for both the client and server components.

Test Basic Docker Commands

Try some basic Docker commands to ensure full functionality:

docker images
docker ps
docker info

These commands list images, running containers, and system-wide information respectively.

Docker Management Basics

Now that Docker is installed and verified, let’s cover some essential management commands.

Essential Docker Commands

Familiarize yourself with these fundamental Docker commands:

  • docker pull: Download an image from a registry
  • docker build: Build an image from a Dockerfile
  • docker run: Create and start a container
  • docker stop: Stop a running container
  • docker rm: Remove a container
  • docker rmi: Remove an image

Container Management

Learn to manage containers effectively:

# List running containers
docker ps

# List all containers (including stopped ones)
docker ps -a

# Start a stopped container
docker start container_name

# Stop a running container
docker stop container_name

# Remove a container
docker rm container_name

Image Management

Manage Docker images with these commands:

# List downloaded images
docker images

# Pull an image from Docker Hub
docker pull image_name

# Remove an image
docker rmi image_name

# Build an image from a Dockerfile
docker build -t image_name:tag .

Network Configuration

Docker provides powerful networking capabilities. Here are some basic network commands:

# List Docker networks
docker network ls

# Create a new network
docker network create network_name

# Connect a container to a network
docker network connect network_name container_name

# Inspect network details
docker network inspect network_name

Troubleshooting Common Issues

Even with a smooth installation, you might encounter some issues. Here are solutions to common problems:

Permission Denied Errors

If you encounter “permission denied” errors, ensure your user is in the docker group and the Docker socket has the correct permissions. If issues persist, try:

sudo chmod 666 /var/run/docker.sock

Network Connectivity Issues

For network-related problems, check your firewall settings and ensure Docker’s network bridge is functioning:

sudo firewall-cmd --zone=public --add-masquerade --permanent
sudo firewall-cmd --reload
sudo systemctl restart docker

Service Startup Problems

If Docker fails to start, check the system logs:

sudo journalctl -u docker

Look for error messages that might indicate the root cause.

Repository Errors

If you encounter repository errors during installation, ensure your system’s date and time are correct:

sudo timedatectl set-ntp true
sudo dnf clean all
sudo dnf update

Then retry the Docker installation process.

Performance Optimization

To get the best performance out of Docker on CentOS Stream 10, consider these optimizations:

Docker Daemon Configuration

Optimize the Docker daemon by editing the /etc/docker/daemon.json file:

{
  "storage-driver": "overlay2",
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  },
  "default-ulimits": {
    "nofile": {
      "Name": "nofile",
      "Hard": 64000,
      "Soft": 64000
    }
  }
}

This configuration uses the overlay2 storage driver for better performance and sets up log rotation to manage disk space.

Resource Allocation

When running containers, use resource constraints to prevent a single container from consuming all system resources:


docker run --cpu-shares=512 --memory=1g your_image

This command limits the container to 512 CPU shares and 1GB of memory.

Storage Driver Selection

The overlay2 storage driver is recommended for CentOS Stream 10. Verify it’s in use with:

docker info | grep "Storage Driver"

If it’s not overlay2, update your daemon.json file and restart Docker.

Networking Optimization

For better network performance, consider using the host network mode for containers that require high throughput:

docker run --network host your_image

Be cautious with this option, as it bypasses Docker’s network isolation.

Maintenance and Updates

Regular maintenance ensures your Docker installation remains secure and up-to-date.

Updating Docker Engine

To update Docker, simply run:


sudo dnf update docker-ce docker-ce-cli containerd.io

Backup Considerations

Regularly backup your Docker data directory and configuration files:


sudo tar -czvf docker_backup.tar.gz /var/lib/docker /etc/docker

Monitoring Docker System

Use Docker’s built-in commands to monitor your system:


docker system df  # Check Docker disk usage
docker system events  # Get real-time events from the server
docker system info  # Display system-wide information

Regular Maintenance Tasks

Implement these maintenance tasks:

  • Regularly prune unused objects: docker system prune
  • Check for and remove dangling images: docker image prune
  • Review and update container resource limits
  • Monitor and rotate Docker logs

Congratulations! You have successfully installed Docker. Thanks for using this tutorial for installing the Docker container development on your CentOS Stream 10 system. For additional help 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