Introduction
In this challenge, you will attach persistent storage to a container using podman. Containers are ephemeral by nature, which means any data created inside a container is lost when it is removed. To preserve data, you can mount a directory from the host system into the container. This mounted directory is known as a persistent volume, ensuring your data survives even if the container is stopped, removed, or recreated.
Attach Persistent Storage to a Container
In this challenge, you will create a directory on the host system to act as a persistent volume. You will then run a new container, attaching the persistent volume to it. Finally, you will verify that data written to the volume from within the container persists after the container is restarted.
Tasks
- Create a directory on the host system to act as a persistent volume.
- Run a new container, attaching the persistent volume to it.
- Verify that data written to the volume from within the container persists after the container is restarted.
Requirements
- Create a directory named
/dataon the host system. This directory will be used as the host path for the volume mount. - Run a container using the
nginximage. The container must be namedmy-nginx-container. - Mount the
/datahost directory to the/usr/share/nginx/htmldirectory inside the container. - Inside the running container, create a file at
/usr/share/nginx/html/my-file.txtwith the exact contentThis is a persistent file.
Example
After completing the tasks, you should be able to see your custom file alongside the default Nginx files inside the container's mounted directory. You can also check the host directory /data to see the same files.
[labex@host ~]$ podman exec my-nginx-container cat /usr/share/nginx/html/my-file.txt
This is a persistent file
[labex@host ~]$ podman exec my-nginx-container ls /usr/share/nginx/html
my-file.txt
Hints
- Use the
sudo mkdircommand to create the/datadirectory. - You may need to change ownership using
sudo chownand permissions usingsudo chmodto ensure the container process can write to it. - The
-vor--volumeflag for thepodman runcommand is used to mount a host directory into a container. The format is-v /host/path:/container/path. - On SELinux-enabled systems like RHEL, you need to set the proper SELinux context for the host directory. Use
sudo semanage fcontext -a -t container_file_t "/data(/.*)?"andsudo restorecon -R /datato configure SELinux labels. - If you encounter permission errors, try setting the directory ownership to match the container's user ID (typically 1000:1000 for nginx).
Summary
In this challenge, you have learned how to provide persistent storage for a container using a bind mount. By mounting a directory from the host system into a container, you ensure that the data generated by your application persists even when the container is stopped, removed, or restarted. This technique is fundamental for running stateful applications in containers.



