02 Jan 2024

Docker Volumes

By default all the changes inside the container are lost when the container stops. If we want to keep data between runs, Docker volumes and bind mounts can help.

→ A docker container runs software stack defined in an image.

→ Can be shared between containers.

Commands

  • docker volume ls shows all the volumes known to Docker
  • docker volume ls -f name=data allows us to filter volumes
  • docker volume rm data_vol removes the volume
  • docker volume prune removes all unused volumes

Bind Mounts

→ A connection from the container to a directory on the host machine. It allows the host to share it’s own file system with the container.

Docker Volumes

A bind mount uses the host file system, but Docker volumes are native to Docker. The volume has a lifecycle that’s longer than the container’s, allowing it to persist until no longer needed. Volumes can be shared between containers.

Managing Volumes

docker volume allows you to manage volumes. For example:

$ docker volume ls
DRIVER   VOLUME NAME
local    data_volume

To remove volume:

$ docker volume rm data_volume

To remove all unused volumes:

$ docker volume prune