How to Deploy and Use the Rocky Linux Docker Image
When you think of deploying Rocky Linux, you most likely default to either installing it to bare metal or as a virtual machine. Both of those deployments work great for fairly straightforward use cases. But what if you'd prefer to containerize your deployments?
Containerized deployments have plenty of benefits, such as:
- Portability: Containers provide a consistent and portable environment for applications so they can run reliably across different platforms without the need for modification.
- Scalability: Containers make it easy to dynamically scale your applications based on demand.
- Isolation: Containers provide an exceptional level of isolation between applications, their dependencies, and even the hosting platform.
- Efficiency: Containers enable efficient resource utilization because you won't have to run separate virtual machines for every application. Containers also enjoy exponentially faster start times than either traditional or virtualized applications.
- DevOps ready: Containers function well within a DevOps methodology by facilitating better collaboration between development and operations teams.
- Version Control: Containers allow for easier version control of application dependencies.
- Flexibility: Containers provide a level of flexibility that traditional applications cannot match, thanks to the ability to package applications with specific runtimes, libraries, and dependencies.
Some time ago, Rocky Linux was made available as a container image, which can be found on Docker Hub. This is the official Rocky Image and is available as a Base container and a Minimal container, with microdnf and a minimal dependency set. These images are regularly updated for all active releases and can be used for free.
Let's find out how you can deploy and use Rocky Linux as a base for your containerized deployments.
What you'll need
To make this work, you'll need an operating system that supports Docker. It doesn't matter which platform you use, so long as it includes the Docker runtime. If you're a developer, looking to work with the Rocky Linux image, you might be using MacOS or Ubuntu as your dev platform. Let's assume you're using Ubuntu Server, 22.04. If that's the case, you can easily install Docker with the command:
sudo apt-get install docker.io -y
Once installed, you would then have to add your user to the docker group with:
sudo usermod -aG docker $USER
Log out and log back in for the changes to take effect.
How to pull the latest Rocky Linux image
The first step is to pull the latest Rocky Linux image, which is done with the command:
docker pull rockylinux/rockylinux
Verify the pull was successful with the command:
docker images
You should see rockylinux/rockylinux listed.
Deploy a container from the new image
Next, we can deploy our first container from the image. For that, issue the command:
docker run -it --name rocky -d rockylinux/rockylinux
Modify the running container
Let's install a web server to our running container. To do that, first access the container shell with:
docker exec -it --user root rocky /bin/bash
You will find yourself at the container's bash prompt. Install the Apache web server with:
dnf install httpd -y
Start the Apache daemon with:
httpd
Verify the daemon is running with:
curl localhost
You should see the HTML from the Apache Welcome page.
Create a new image from our running container
With the Apache web server installed, you might want to create a new Rocky Linux image based on the running container. With this taken care of, you could quickly deploy new containers that already have Apache installed (which is one less step for you to take).
To do this, exit from the running container with:
exit
Next, find the ID of the Rocky Linux container with the command:
docker ps
You'll need the first four characters of the ID string.
Let's say we're going to name our new image rocky-httpd (so it's easy to know what the image contains). To create the new image, we use the docker commit command like so:
docker commit ID rocky-httpd
Where ID is the first four characters of the Rocky Linux container ID.
Now, if you run the docker images command, you'll see your newly-created Rocky Linux image that contains the Apache web server listed as rocky-httpd. You can then stop the original container with:
docker stop ID
Where ID is the first four characters of the running container ID.
You can then deploy a container based on the newly-created image like so:
docker run -it --name rocky-web -d rocky-httpd
And that's all there is to deploying and using the Rocky Linux Docker image. Enjoy containerizing your favorite Linux distribution.