Skip to main content

Docker - Volumes

Greetings!

Sharing is caring. When the container is running/down or removed we need to access the data within it. Be it a database, web application logs it needs to share some form of data with host or with the other containers. Docker provides volume to achieve this.

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.

Volume commands

  • docker volume create - create a volume
  • docker volume ls - list available volumes
  • docker volume remove - remove a volume
  • docker volume prune - remove all unused volumes
  • docker volume inspect - inspect a volume

Create a volume

docker volume create my-volume

List volumes

docker volume ls

Inspect a volume

docker volume inspect my-volume

Remove a volume

docker volume rm my-volume

Remove all unused volumes

docker volume prune

Start a container with a volume

We can start a container with a volume using --mount or -v flag. As in the docs, New users should try --mount syntax which is simpler than --volume syntax.
If the volume does not exist, Docker creates the volume for us.

docker container run -d \
--name my-nginx \
--mount source=my-volume,target=/app \
nginx:latest

References

https://docs.docker.com/storage/volumes/


Comments