docker-persist-share-data
Persisting Data
Overview
Sometimes you want to share data between container or you want to persist data even docker is deleted
, there there two ways for it.
- bind mount
- volume
Volumes are the preferred mechanism for persisting data generated by and used by Docker containers
. While bind mounts are dependent on the directory structure of the host machine, volumes are completely managed by Docker(volume is dir on host but managed by docker itself
)
Volumes have several advantages over bind mounts:
- Volumes are easier to back up or migrate than bind mounts.
- You can manage volumes using Docker CLI commands or the Docker API.
- Volumes work on both Linux and Windows containers.
- Volumes can be more safely shared among multiple containers.
- Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.
- New volumes can have their content pre-populated by a container.
Ignore tmpfs(in memory), it's for non-persisting data, data will be disappear if docker is stop or restart
Note: volume dir is at /var/lib/docker/volumes/, controlled by docker cli, so volume is independent of container, even you delete container, volume is still there if not delete explicitly by cli
docker option for persisting data
There are two options you can use to do this, one is -v(–volume ), the other is –mount, new user should use –mount
–mount:
Consists of multiple key-value pairs, separated by commas and each consisting of a <key>=<value> tuple
. The –mount syntax is more verbose than -v or –volume, but the order of the keys is not significant, and the value of the flag is easier to understand.- The
type
of the mount, which can bebind, volume, or tmpfs
. This topic discusses volumes, so the type is always volume. - The
source
of the mount. For named volumes, this is thename of the volume
. For anonymous volumes, this field is omitted. May be specified assource or src
. - The
destination
takes as its value the path where the file or directory is mounted in the container. May be specified asdestination, dst, or targe
t. - The
readonly
option, if present, causes the bind mount to be mounted into the container as read-only. - The volume-opt option, which can be specified more than once, takes a key-value pair consisting of the option name and its value.
- The
–mount needs docker version >=17.06, check it $docker version
bind mount
1 | # For type=bind, must specify the source(from host) |
volume
1 | $ docker volume create my-vol |
volume should be deleted manually even if it’s created automatically sometime
mount block device into container
1 | # create a raw disk |
tmpfs
tmpfs mounts only for linux. When you create a container with a tmpfs mount, the container can create files outside the container’s writable layer
This is useful to `temporarily store` sensitive files that you don’t want to persist in either the host or the container writable layer.Unlike volumes and bind mounts, you can’t share tmpfs mounts between containers
1 | $ docker run -d \ |
Volume size(mount) and Container size(RW Layer)
There is no limitation for volume when creating it
, you can’t set limit for it, the size of volume is determined by the disk(block) of host it resides
. same thing for tmpfs as well, its size is determined by host tmpfs system(default it’s size half of total memory). volume, bind mount, tmpfs, block device appear same in the container(dst directory)
, that means inside a container, you see a directory, the size or data that can be stored at that directory depends on the size of block on host. but if you use fdisk, lsblk
inside the container, you see the host block size, not the size can be used by the container.
The storage size can be used by container has two parts.
- volume size determined by host block size, check volume source
docker inspect
, then check which block as source - writable layer determined by host block where the writable layer exists(
default /var/lib/docker, check which block for /var
)