0
0
Dockerdevops~5 mins

Creating and managing volumes in Docker - Step-by-Step CLI Walkthrough

Choose your learning style9 modes available
Introduction
When you run containers, their data disappears when they stop. Volumes let you save data outside containers so it stays safe and can be shared.
When you want to keep database files safe even if the container is removed
When you need to share files between multiple containers running on the same host
When you want to back up important data created by a container
When you want to separate your app code from its data for easier updates
When you want to persist logs generated by containers for later analysis
Commands
This command creates a new volume named 'my-data-volume' to store data outside containers.
Terminal
docker volume create my-data-volume
Expected OutputExpected
my-data-volume
Runs an nginx container named 'my-container' and attaches the 'my-data-volume' to the container's /app/data folder to save data persistently.
Terminal
docker run -d --name my-container -v my-data-volume:/app/data nginx
Expected OutputExpected
a1b2c3d4e5f67890123456789abcdef0123456789abcdef0123456789abcdef0
-d - Run container in background (detached mode)
--name - Assign a name to the container for easy reference
-v - Attach a volume to a container path
Lists all volumes on the Docker host to verify 'my-data-volume' exists.
Terminal
docker volume ls
Expected OutputExpected
DRIVER VOLUME NAME local my-data-volume
Shows detailed information about the 'my-data-volume' including its mount point on the host.
Terminal
docker volume inspect my-data-volume
Expected OutputExpected
[ { "CreatedAt": "2024-06-01T12:00:00Z", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/my-data-volume/_data", "Name": "my-data-volume", "Options": {}, "Scope": "local" } ]
Deletes the 'my-data-volume' when it is no longer needed to free up space.
Terminal
docker volume rm my-data-volume
Expected OutputExpected
my-data-volume
Key Concept

If you remember nothing else from this pattern, remember: volumes keep your container data safe and separate from the container lifecycle.

Common Mistakes
Not creating a volume before using it in a container
Docker will create an anonymous volume but it is hard to manage and reuse later.
Always create named volumes explicitly with 'docker volume create' before attaching them.
Removing a volume while a container is still using it
Docker will refuse to remove the volume or data loss may occur if forced.
Stop and remove containers using the volume before deleting the volume.
Mounting volumes to wrong container paths
Data will not be saved where expected and the app may fail to find its files.
Check container documentation for correct paths to mount volumes.
Summary
Create a named volume with 'docker volume create' to store data persistently.
Run containers with '-v volume_name:/container/path' to attach volumes.
Use 'docker volume ls' and 'docker volume inspect' to manage and check volumes.
Remove volumes with 'docker volume rm' only after containers using them are stopped.