How to Create Volume in Docker: Simple Guide
To create a volume in Docker, use the command
docker volume create [VOLUME_NAME]. This creates a persistent storage area that containers can use to save and share data.Syntax
The basic syntax to create a Docker volume is:
docker volume create [VOLUME_NAME]Here, docker volume create is the command to make a new volume, and [VOLUME_NAME] is the name you give to your volume. If you omit the name, Docker will generate a random one.
bash
docker volume create my_volume
Output
my_volume
Example
This example shows how to create a volume named my_data and then run a container that uses this volume to store data persistently.
bash
docker volume create my_data
docker run -d --name my_container -v my_data:/app/data busybox sleep 3600
docker volume lsOutput
DRIVER VOLUME NAME
local my_data
Common Pitfalls
Common mistakes when creating Docker volumes include:
- Not specifying a volume name and then losing track of the randomly generated name.
- Forgetting to mount the volume inside the container with
-v, so data is not persisted. - Using bind mounts instead of volumes when persistent, managed storage is needed.
Always check your volume list with docker volume ls to confirm creation.
bash
docker volume create # This creates a volume with a random name docker run -d --name test_container busybox sleep 3600 # No volume mounted, data won't persist # Correct way: docker volume create test_volume docker run -d --name test_container -v test_volume:/data busybox sleep 3600
Quick Reference
| Command | Description |
|---|---|
| docker volume create [NAME] | Create a new volume with optional name |
| docker volume ls | List all Docker volumes |
| docker volume inspect [NAME] | Show details about a volume |
| docker volume rm [NAME] | Remove a volume |
| docker run -v [VOLUME_NAME]:[CONTAINER_PATH] | Mount volume inside container |
Key Takeaways
Use
docker volume create [NAME] to make a new volume for persistent data.Mount volumes in containers with
-v [VOLUME_NAME]:[PATH] to save data outside the container.Check existing volumes anytime with
docker volume ls.Always name your volumes to avoid confusion with random names.
Volumes help keep data safe even if containers are deleted.