Back to Docker Course

Docker Course

Data Persistence with Docker

  • Volumes are used to persist data generated by and used by Docker containers.
  • Volumes are used to avoid data loss.
  • Using Volumes we can make container as statefull container
  • They are stored outside the container's filesystem, allowing data to persist even if the container is removed.

Managing Volumes

  • Creating and using volumes:
  1. To create a volume, use: docker volume create myvolume
  2. To use a volume with a container, run: docker run -d -v myvolume:/data myapp
  • Backup and restore:
  1. To backup a volume, run: docker run --rm -v myvolume:/data -v $(pwd):/backup busybox tar czf /backup/backup.tar.gz /data
  2. To restore from a backup, use: docker run --rm -v myvolume:/data -v $(pwd):/backup busybox tar xzf /backup/backup.tar.gz -C /data

Additional Commands

  • To inspect a volume and get detailed information, run: docker volume inspect myvolume
  • To list all available volumes, use: docker volume ls
  • To remove a specific volume, execute: docker volume rm myvolume
  • To remove all unused volumes, run: docker volume prune
  • For volumes with specific requirements, you can create them with a custom driver: docker volume create --driver mydriver myvolume