How to create and manage container volumes with Podman
For anyone who’s made the switch from a Ubuntu-based to an RHEL-based Linux distribution for container deployments, you’ve probably realized that Docker isn’t the easiest or best option for your new platform. Thankfully, Podman is installed by default on most RHEL-based distributions, so you can skip directly to working with your containers.
But why would you want to learn an entirely new tool? Fortunately, Podman is almost a direct 1:1 replacement for Docker, so if you know one you can use the other. I’ve already helped you take your first steps with Podman and this time around we’re going to extend that a bit by creating and managing volumes.
Why are volumes important? Simple — persistent storage. Say, for example, you deploy a container that uses data. Everything is going great until disaster strikes. The container fails and takes your data down with it. You don’t want that.
SEE: Hiring kit: Back-end Developer (TechRepublic Premium)
To avoid such a catastrophe, you’d deploy those containers using volumes. By doing this, the data is saved to a persistent state, so if the container goes down, the data is still safe and can be used by a different container. Trust me, you want to use volumes for any container that will depend on data. This is especially so if you or your business depends on the data used by that container.
With that said, how do you work with volumes in Podman? Let’s find out.
What you’ll need to create and manage volumes with Podman
The only thing you’ll need for this is a Linux distribution with Podman installed. This could be RHEL, Rocky Linux, AlmaLinux, or CentOS. That’s it.
How to create a volume with Podman
The first thing we need to do is create a volume. Log in to your Linux distribution and open a terminal window. Let’s say we’re going to create a volume for an NGINX container. Create that volume with:
podman volume create nginx-volume
The output should be simple:
nginx-volume
You can verify the volume creation with the command:
podman volume ls
The above command should print out something like this:
DRIVER VOLUME NAME
local nginx-volume
To get more information, you could issue the command:
podman volume inspect nginx-volume
The above command will print out something like this:
[
{
"Name": "nginx-volume",
"Driver": "local",
"Mountpoint": "/home/jack/.local/share/containers/storage/volumes/nginx-volume/_data",
"CreatedAt": "2022-09-26T12:52:36.125241042-04:00",
"Labels": {},
"Scope": "local",
"Options": {},
"MountCount": 0,
"NeedsCopyUp": true,
"NeedsChown": true
}
]
How to use a volume with Podman
Now that we’ve created the volume, let’s use it with an NGINX container deployment. Before we do, let’s have some fun and create a new index.html file for the NGINX web server. Change into the volume directory with the command:
cd /home/$USER/.local/share/containers/storage/volumes/nginx-volume/_data
Now, let’s create our index.html with:
nano index.html
In that file, paste the following:
<h2>Hello, TechRepublic!</h2>
Save and close the file.
Deploy the container attached to the volume with the command:
podman run -d -p 8080:80 -v nginx-volume:/usr/share/nginx/html --name nginx-volumetest nginx:latest
What we’ve done with the above command is mapped our nginx-volume to the /usr/share/nginx/html directory within the NGINX container. Now, if we point a web browser to http://IP:8080, where IP is the IP address of the hosting server, we should see our “Hello, TechRepublic!” message.
If you see an error, you’ll need to open the firewall with the following two commands:
sudo firewall-cmd --permanent --add-port 8080/tcp
sudo firewall-cmd --reload
Now, if you reload the web page, you’ll see the message (Figure A).
Figure A
Now, should your container fail, the data in the volume will remain intact. If you ever need to delete the volume, you could simply issue the command:
podman volume rm nginx-volume
And that’s all there is to managing volumes with Podman. This is a crucial feature for anyone looking to keep persistent data for their container deployments.
Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.
For all the latest Technology News Click Here
For the latest news and updates, follow us on Google News.