Attach Persistent Storage to a Container

LinuxLinuxBeginner
Practice Now

Introduction

In this challenge, you will learn how to attach persistent storage to a container. Containers are designed to be ephemeral, meaning that any data stored within the container is lost when the container is stopped or deleted. To overcome this, you can attach persistent storage to a container, allowing the container to access and store data that persists beyond the container's lifetime.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") subgraph Lab Skills linux/echo -.-> lab-389428{{"`Attach Persistent Storage to a Container`"}} end

Attach Persistent Storage to a Container

Tasks

  • Create a persistent volume on the host system
  • Start a container and attach the persistent volume to it
  • Verify that the persistent volume is accessible within the container

Requirements

  • Create a persistent volume in the /data directory on the host system
  • Start a container using the nginx image and attach the persistent volume to the /usr/share/nginx/html directory within the container
  • Verify that the persistent volume is accessible within the container by creating a file in the /usr/share/nginx/html directory and ensuring that the file persists after the container is stopped and restarted

Example

After attaching the persistent volume to the container, you should be able to access the /usr/share/nginx/html directory within the container and see the file you created. For example:

$ docker exec -it my-nginx-container bash
root@my-nginx-container:/## ls /usr/share/nginx/html
index.html  my-file.txt

Summary

In this challenge, you learned how to attach persistent storage to a container. By creating a persistent volume on the host system and attaching it to a container, you can ensure that data stored within the container persists beyond the container's lifetime. This is an important concept in container-based applications, where you need to maintain data persistence across container deployments and restarts.

Other Linux Tutorials you may like