How to Install Docker on Rocky Linux 9?
Docker is a popular open-source platform designed to simplify the development, deployment, and running of applications by using container technology. Containers are lightweight, portable, and self-sufficient units that bundle application code with all its dependencies, ensuring it runs reliably across different environments.
In this guide, you will learn how to install Docker on a Rocky Linux 9 server, manage the Docker service, run containerized applications, and push images to a container registry. This tutorial is suitable for developers and system administrators looking to work with containerized environments on Rocky Linux.
Prerequisites
Before you begin:
- You should have a Rocky Linux 9 server instance ready.
- Access the server using SSH as a non-root user with sudo privileges.
- Make sure your system packages are up to date.
To update the system, run:
sudo dnf update -y
Step 1: Install Docker on Rocky Linux 9
Although Docker is available in the default Rocky Linux repositories, the latest version may not be. Follow the steps below to install the latest Docker version.
1. Install dnf-utils:
sudo dnf install -y dnf-utils
2. Add the Docker repository:
sudo dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo
3. Install Docker and dependencies:
sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Step 2: Manage the Docker Service
Docker runs as a systemd service. You need to enable and manage it as follows:
1. Start the Docker service:
sudo systemctl start docker
2. Enable Docker to start on boot:
sudo systemctl enable docker
3. Check Docker service status:
sudo systemctl status docker
The output should show Docker is active and running.
4. (Optional) Stop or restart Docker:
sudo systemctl stop docker
sudo systemctl restart docker
Step 3: Run a Sample Docker Container
To verify that Docker is working properly, you can pull and run a sample container.
1. Pull the Hello World image:
docker pull crccheck/hello-world
2. List available Docker images:
docker images
3. Run a container in detached mode:
docker run -d --name web-test -p 80:8000 crccheck/hello-world
4. Verify running containers:
docker ps
You should see the container web-test running and exposing port 80 on the host.
5. Test your container:
curl http://127.0.0.1:80
You should see:
Hello World
You can also access the app via your server’s IP address in a browser:
http://your-server-ip:80
Step 4: Push Docker Images to a Container Registry (Optional)
To share or deploy your custom Docker images, you can push them to a container registry such as Docker Hub or a private registry.
1. Log in to the registry:
docker login
2. Tag the image:
docker tag crccheck/hello-world:latest yourusername/hello-world:latest
3. Push the image:
docker push yourusername/hello-world:latest
You can now access your image from any machine with Docker installed.
Step 5: Deploy Container from a Remote Image
Once your image is pushed to a container registry, you can pull and deploy it on any server:
docker run -d --name web-test-remote -p 8080:8000 yourusername/hello-world:latest
Verify it with:
curl http://127.0.0.1:8080
Or visit:
http://your-server-ip:8080
Conclusion
You have successfully installed Docker on Rocky Linux 9 and deployed containerized applications. Docker is a powerful tool for application packaging, isolation, and scaling. You can now build, tag, push, and run Docker containers with ease, enabling you to streamline development and deployment processes across various environments.
For more advanced use cases, consider exploring Docker Compose, Docker Swarm, or Kubernetes for orchestrating multi-container applications and managing workloads at scale.