Start Developing with Rocky Linux as a Docker Container

I tend to default to Ubuntu as my go-to OS for Docker containers. Because I’ve been using Ubuntu for such a long time, it’s just second nature to me. That doesn’t mean, however, it’s the only operating system I use.
Case in point, Rocky Linux. Whenever I need a Red Hat Enterprise Linux-based OS, I opt for the distribution created by Gregory Kurtzer who originated CentOS. Back in 2021, Kurtzer made Rocky Linux available as a container image. Since then, the Rocky Linux Docker image has enjoyed over 10 million pulls from Docker Hub.
Clearly, Rocky Linux has become a popular container image. So, why not start developing with it? One thing to know is that the Rocky Linux container image has been stripped down to only the necessary tools for container deployment. In other words, this isn’t the same Rocky Linux you can download and use as a server OS.
Let me help you get started with this task. It’s not nearly as challenging as you might think.
What You Need
The only things you’ll need for this are an operating system that supports Docker and a user with sudo privileges. I’m going to demonstrate on Ubuntu Server 22.04, so if you’re using a non-Ubuntu distribution, you’ll need to alter the Docker installation commands as shown below.
Installing Docker
On the off-chance you haven’t already installed Docker on Ubuntu Linux, here’s how it’s done.
The first thing to be done is to download and install the official Docker GPG key with the command:
1 |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg |
Next, we must add the Docker repository so it can be installed with apt-get. The command to add the repository is:
1 |
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null |
Install a few simple dependencies with the following command:
1 |
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y |
Update apt:
1 |
sudo apt-get update |
Install Docker CE with the following:
1 |
sudo apt-get install docker-ce docker-ce-cli containerd.io -y |
With Docker installed, you’ll need to add your user to the docker group, so you can manage Docker without having to use sudo (which can be a security issue). To add your user to the group, run the command:
1 |
sudo usermod -aG docker $USER |
Finally, log out and log back in so the changes take effect.
Pulling the Rocky Linux image
Before we can deploy the container, we must pull the Rocky Linux image with the command:
1 |
docker pull rockylinux/rockylinux |
When the pull completes, verify it’s available with the command:
1 |
docker images |
You should see something like this in the output:
1 |
rockylinux/rockylinux latest 523ffac7fb2e 15 months ago 196MB |
Deploy and Access the Rocky Linux container
Next, we’ll deploy our container with the command:
1 |
docker run -it --name rocky -d rockylinux/rockylinux |
Once the container has deployed, access it with the command:
1 |
docker exec -it --user root rocky /bin/bash |
You should now find yourself at the bash prompt of the Rocky Linux container. You’ll know if the prompt starts with root@.
Install a Web Server
Let’s install the Apache web server in our running container with the command:
1 |
dnf install httpd -y |
Start the Apache daemon with the command:
1 |
httpd |
Verify the web server is running with the command:
1 |
curl localhost |
You should see the contents of the Apache welcome page (in text form) in the output that starts with:
Exit from the running container with the command:
1 |
exit |
What we’ll now do is create a new image from our running container. Instead of our image being a base Rocky Linux image, it will now include the Apache web server. Of course, before you exit the container, you could always install whatever you need that will be included in the new image.
To create the new image from the running container, you must first locate the ID of the running container with the command:
1 |
docker ps |
The ID will be a random string of characters.
Next, we create our new image with the docker commit command like so:
1 |
docker commit ID rocky-httpd-template |
Where ID is the first four characters of the running Rocky Linux container. Of course, you can name your template whatever you like. Just make sure the name clearly indicates what the image is to be used for.
You can then verify the new image has been successfully created with the command:
1 |
docker images |
The output of the above command should look something like this:
1 2 |
rocky-httpd-template latest 28dc636eb504 About a minute ago 256MB rockylinux/rockylinux latest 523ffac7fb2e 15 months ago 196MB |
As you can see, our new template has been created and is ready to use. You could then deploy a container with that template image like so:
1 |
<a href="https://thenewstack.io/now-in-beta-rancher-labs-runs-docker-natively-in-production/">docker run</a> -it --name rocky-web -d rocky-httpd-template |
And that’s all there is to get started using Rocky Linux as a development image with Docker. You can now extend your efforts beyond Ubuntu Server as a base for your container deployments.