Working with Docker volumes

Working with Docker volumes

How to create, inspect and delete docker volumes

ยท

2 min read

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

image.png

Inspecting Docker volumes

Use docker inspect [volume-name] to verify that the volume was created and mounted correctly

docker volume inspect [volume-name]

image.png

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

  1. --mount option
docker run -d \
  --name [name-of-the-container] \
  --mount source=absolute-path,target=/usr/app \
  nginx:latest
  1. -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

More info on absolute path shortcut

image.png

Avoid copying the command to the terminal causes an interesting error: ๐Ÿ˜’

image.png

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