Containers and Persistent data

Overview

Volumes are the recommended way to persist data in containers. There are three major reasons for this:

  • Volumes are independent objects that are not tied to the lifecycle of a container.
  • Volumes can be mapped to specialized external storage systems.
  • Volumes enable multiple containers on different Docker hosts to access and share the same data.

At a high level, you create a volume, then you create a container and mount the volume into it. The volume is mounted into a directory in the container’s filesystem, and anything written to that directory is stored in the volume. If you delete the container, the volume and its data will still exist.

The figure below shows a Docker volume existing outside of the container as a separate object. It is mounted into the container’s filesystem at /data, and any data written to the /data directory will be stored on the volume and will exist after the container is deleted.

Get hands-on with 1200+ tech skills courses.