This page will talk about data management in docker. There are 2 ways to manage data in a container:
- Using Data Volumes –
docker volume --help
- 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.
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.
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
2. Mount host directory
--mount to mount a host directory as a data volume.
- The above command to load the host
/src/webapp directory into a container
--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
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.