Attach Persistent Storage to a Container

Red Hat Enterprise LinuxBeginner
Practice Now

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 /data on the host system. This directory will be used as the host path for the volume mount.
  • Run a container using the nginx image. The container must be named my-nginx-container.
  • Mount the /data host directory to the /usr/share/nginx/html directory inside the container.
  • Inside the running container, create a file at /usr/share/nginx/html/my-file.txt with the exact content This 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 mkdir command to create the /data directory.
  • You may need to change ownership using sudo chown and permissions using sudo chmod to ensure the container process can write to it.
  • The -v or --volume flag for the podman run command 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(/.*)?" and sudo restorecon -R /data to 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).
✨ Check Solution and Practice

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.