Docker volumes are filesystems docker use to store and persist data generated by Docker containers.
Volumes are stored on the host machine this allows the user to back up data and share data between containers easily.
Docker volumes are a better choice than persisting data in a container's writable layer because volumes do not increase the size of the container using it as they exist outside the container's lifecycle.
Creating volumes
If a name is not specified, Docker will generate a random name - 10a3b232055d3ae3f26532e2a7848c4d88ac984d413634719ae6df500c5b6a9...
docker volume create [volume-name]
Listing docker volumes
docker volume ls
Inspecting Docker volumes
Use docker inspect [volume-name] to verify that the volume was created and mounted correctly
docker volume inspect [volume-name]
mounting data volumes
Docker volumes can be mounted using different approaches when running the container using the -v or --mount flag the mount flag is usually more verbose but both flags produce the same results
--mount
option
docker run -d \
--name [name-of-the-container] \
--mount source=absolute-path,target=/usr/app \
nginx:latest
-v
option
docker run -it โv absolute-path:/usr/app \
โv /usr/app/node_modules \
--name [name-of-the-container] \
-p 3000:3000 [image-name]
#git bash shortcut
docker run -it --name my-first-container -v pwd -W:/usr/app -v /usr/app/node_modules my-first-image
Avoid copying the command to the terminal causes an interesting error: ๐
Removing Docker volumes
This command removes only the specified container
docker volume rm [volume-name]
Removing unused volumes
This command removes all the volumes not being used by at least one container
docker volume prune
๐ That's all for now about volumes