Docker Volume Mounting

DockerBeginner
Practice Now

Introduction

This Docker Volume Mounting Challenge will test your skills in working with Docker volumes, a crucial concept for data persistence and sharing in Docker environments. You will demonstrate your understanding of Docker volumes by creating a named volume, running a container with this volume mounted, and adding data to it. This hands-on experience will reinforce your knowledge of Docker volumes and their practical applications.

Create and Mount a Docker Volume

Tasks

  1. Create a new Docker volume named data_volume.
  2. Run a new container using the Alpine image. Mount the data_volume volume to /app inside the container. Create a file named hello.txt with the content "Hello, Docker volumes." in the /app directory. Ensure the container remains running in the background.

Requirements

To successfully complete this challenge, adhere to the following guidelines:

  • Perform all operations in the /home/labex/project directory.
  • Use the Alpine image for your container.
  • Name your container volume_mounter.
  • The content of hello.txt should be exactly "Hello, Docker volumes."
  • Use Docker commands to create volumes and run containers.
  • Mount the volume at the /app path inside the container.
  • Ensure the container is running in the background.

Example

After completing the tasks, verify your work by running the following commands:

  1. Check if the volume was created:
docker volume ls | grep data_volume

This should list the "data_volume" you created.

  1. Check the status of your container:
docker ps | grep volume_mounter

This should show your "volume_mounter" container in a running state.

  1. Inspect the container to verify the volume mounting:
docker inspect volume_mounter --format '{{ range .Mounts }}{{ if eq .Destination "/app" }}{{ .Name }}{{ end }}{{ end }}'

This should output "data_volume", confirming that the volume is correctly mounted.

  1. Check if the file you created exists:
docker exec volume_mounter cat /app/hello.txt

This should display the content of the "hello.txt" file.

Summary

This Docker Volume Mounting Challenge has reinforced your understanding of Docker volumes and their importance in persisting data across container lifecycles. You have practiced creating named volumes, running containers with mounted volumes, and writing data to these volumes.

These skills are fundamental in containerization, allowing effective data management in Docker environments. Remember that volumes are key to ensuring data persistence and sharing information between the host and containers.

✨ Check Solution and Practice