This page will talk about data management in docker. There are 2 ways to manage data in a container:

  1. Using Data Volumes – docker volume --help
  2. Mount host directory – Bind mounts.

Make sure you understand the basic concepts of docker, if not please go through the introduction tutorial.

By default, all files created inside a container are stored on a writable container layer. Which means:

  • The data doesn’t persist when that container no longer exists, and it can be difficult to get the data out of the container if another process needs it.
  • A container’s writable layer is tightly coupled to the host machine where the container is running. You can’t easily move the data somewhere else.
  • Writing into a container’s writable layer requires a storage driver to manage the filesystem. The storage driver provides a union filesystem, using the Linux kernel. This extra abstraction reduces performance as compared to using data volumes, which write directly to the host filesystem.

Choosing the right type of mounts

No matter which type of mount you choose to use, the data looks the same from within the container. It is exposed as either a directory or an individual file in the container’s filesystem.

Types of mounts
  • Volumes are stored in a part of the host filesystem which is managed by Docker (/var/lib/docker/volumes/ on Linux). Non-Docker processes should not modify this part of the filesystem. Volumes are the best way to persist data in Docker.
  • Bind mounts may be stored anywhere on the host system. They may even be important system files or directories. Non-Docker processes on the Docker host or a Docker container can modify them at any time.
  • tmpfs mounts are stored in the host system’s memory only, and are never written to the host system’s filesystem.

1. Use data Volume

Volumes are the preferred mechanism for storing data accessed (read/write) by a container. Volumes are managed by docker, and can be shared with multiple containers.

  • Create a data volume
Copy
  • View all data volume

Copy

  • Inspect a data volume

Copy

  • Use a data volume 
  • Delete a data volume
Copy

NOTE: volume lifecycle is independent of th container. Docker does not automatically delete the volume when a container is deleted and vice versa. Use docker rm -v if you want the volume to bre deleted when a container is deleted.

Unknown, or tangling volumes can take up a lot of space, use the command below to clean them up

Copy

2. Mount host directory

Use --mount to mount a host directory as a data volume.

Copy
  • The above command to load the host /src/webapp directory into a container /opt/webapp directory.  --mount parameter throws an error if the local directory does not exist.
  • --mount also can mount a single data volume.
  • You can also set readonly volume as --mount source=nginx-vol,destination=/usr/share/nginx/html,readonly.

Docker volume is the recommended approach. Use the mount host directory for testing purposes.

Let me know your thoughts on data management in docker. If anything is not clear, suggest me so in the comment below.

Check out Digital Ocean guide on sharing data between containers.