Docker / Docker Volumes and Data Management
Mounting Volumes for Persistent Storage
In this tutorial, you will learn how to mount Docker Volumes in containers for persistent storage. This will help you maintain data beyond the lifecycle of a single Docker contain…
Section overview
5 resourcesCovers data persistence and managing data in Docker containers.
Mounting Volumes for Persistent Storage
1. Introduction
This tutorial aims to guide you through the process of mounting Docker Volumes in containers for persistent storage. By the end of this tutorial, you will gain a firm understanding of Docker Volumes, how they can be used to implement persistent storage, and how to mount these volumes in Docker containers.
Prerequisites:
- Basic understanding of Docker and Docker containers
- Docker installed on your machine
2. Step-by-Step Guide
Docker Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure of the host machine, volumes are completely managed by Docker.
To mount a volume, we can use the -v or --mount flag when starting a container. Let's start a new container and mount a volume named myvol2:
Example:
docker run -d \
--name devtest \
--mount source=myvol2,target=/app \
nginx:latest
In this command, --mount source=myvol2,target=/app tells Docker to bind the volume myvol2 to /app inside the container.
3. Code Examples
Example 1: Creating a new Docker volume
docker volume create myvol
This command creates a new volume named myvol. The volume will be created in the docker volume directory.
Example 2: Listing Docker volumes
docker volume ls
This command lists all the Docker volumes in your system.
Example 3: Mounting a Docker volume
docker run -d \
--name mycontainer \
--mount source=myvol,target=/app \
nginx:latest
This command starts a new container named mycontainer and mounts the volume myvol to /app inside the container.
Expected output
<container_id>
The output will be the ID of the newly created container.
4. Summary
In this tutorial, we learned what Docker Volumes are, how to create a Docker Volume, and how to mount these volumes into Docker containers for persistent storage. You can further your understanding by exploring different aspects of Docker Volumes, such as volume drivers and backing up Docker Volumes.
Additional Resources:
- Docker Volumes Documentation
5. Practice Exercises
Exercise 1:
Create a Docker Volume named "testvol" and list all the volumes to verify its creation.
Solution:
docker volume create testvol
docker volume ls
Exercise 2:
Create a new Docker container named "testcontainer" and mount the volume "testvol" to the path "/data" inside the container.
Solution:
docker run -d \
--name testcontainer \
--mount source=testvol,target=/data \
nginx:latest
Exercise 3:
Inspect the "testvol" volume to find its mount point.
Solution:
docker volume inspect testvol
This command gives you detailed information about the volume, including its mount point.
Keep practicing with different volume and container names, and try mounting multiple volumes to a container.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article