数据卷和文件目录挂载
Docker 有两种数据管理模式:
数据卷(Volumes)
挂载主机目录 (Bind mounts)
主机目录挂载 时 -v 和 --mount 区别
对比项 | bind mount | volume |
Source位置 | 用户指定 | /var/lib/docker/volumes/ |
Source为空 | 覆盖dest为空 | 保留dest内容 |
Source非空 | 覆盖dest内容 | 覆盖dest内容 |
Source种类 | 文件或目录 | 只能是目录 |
可移植性 | 一般(自行维护) | 强(docker托管) |
宿主直接访问 | 容易(仅需chown) | 受限(需登陆root用户)* |
如果主机路径不存在 | 自动创建 | 命令报错 |
数据卷
数据卷 是一个供一个或多个容器使用的特殊目录,它绕过 UFS,可以提供很多有用的特性
数据卷 可以在容器之间共享和重用
对 数据卷 的修改会立马生效
对 数据卷 的更新,不会影响镜像
数据卷 默认会一直存在,即使容器被删除
创建数据卷
# 创建一个本地数据卷
docker volume create {卷名}
docker volume create my-vol
查看数据卷
# 显示 本地数据卷 列表
docker volume ls
# 显示 数据卷 元信息
docker volume inspect {卷名}
docker volume inspect my-vol
启动一个挂载数据卷的容器
# 启动容器,挂载数据卷 my-vol 为 容器路径 /usr/share/nginx/html
# 等同于 -v my-vol:/usr/share/nginx/html
docker run -d -P \
--name web \
--mount source=my-vol,target=/usr/share/nginx/html \
nginx:alpine
查看 数据卷 详细信息
# 获取 容器 元数据
docker inspect {容器名/镜像名}
docker inspect web
删除 数据卷
# 删除 数据卷
docker volume rm {卷名}
docker volume rm my-vol
挂载主机文件\目录
挂载一个主机目录
# 命令语法
# 默认本地为读写权限,可以增加,readonly设置只读权限
docker run -d -P \
--name {容器名} \
--mount type=bind,source={本机路径},target={容器路径}{,readonly} \
{镜像名}
# 启动容器 采用绑定(bind)模式,映射本地 /src/webapp 到 容器的 /usr/share/nginx/html
# 相当于 -v /src/webapp:/usr/share/nginx/html
docker run -d -P \
--name web \
--mount type=bind,source=/src/webapp,target=/usr/share/nginx/html \
nginx:alpine
# 只读方式映射本地路径
docker run -d -P \
--name web \
--mount type=bind,source=/src/webapp,target=/usr/share/nginx/html,readonly \
nginx:alpine
# 查看 容器 元数据
docker inspect web
无本地源目录,所以报错
创建了本地源目录,正常生成容器
容器 元数据
挂载一个主机文件
# 挂载一个主机文件到
# 启动容器 采用绑定(bind)模式,映射本地 $HOME/.bash_history 文件 到 容器的 /root/.bash_history 文件
# 相当于 -v $HOME/.bash_history:/root/.bash_history \
docker run --rm -it \
--mount type=bind,source=$HOME/.bash_history,target=/root/.bash_history \
ubuntu:18.04 \
bash
官方帮助文档
DOCKER(1) JUNE 2014 DOCKER(1)
NAME
docker-run - Run a command in a new container
SYNOPSIS
docker run
[--mount[=[MOUNT]]]
[-v|--volume[=[[HOST-DIR:]CONTAINER-DIR[:OPTIONS]]]]
IMAGE
OPTIONS
--mount type=TYPE,TYPE-SPECIFIC-OPTION[,...]
Attach a filesystem mount to the container
Current supported mount TYPES are bind, volume, and tmpfs.
e.g.
type=bind,source=/path/on/host,destination=/path/in/container
type=volume,source=my-volume,destination=/path/in/container,volume-label="color=red",volume-label="shape=round"
type=tmpfs,tmpfs-size=512M,destination=/path/in/container
Common Options:
· src, source: mount source spec for bind and volume. Mandatory
for bind.
· dst, destination, target: mount destination spec.
· ro, readonly: true or false (default).
Note: setting readonly for a bind mount does not make its submounts
read-only on the current Linux implementation. See also
bind-nonrecursive.
Options specific to bind:
· bind-propagation: shared, slave, private, rshared, rslave, or
rprivate(default). See also mount(2).
· consistency: consistent(default), cached, or delegated.
Currently, only effective for Docker for Mac.
· bind-nonrecursive: true or false (default). If set to true,
submounts are not recursively bind-mounted. This option is
useful for readonly bind mount.
Options specific to volume:
· volume-driver: Name of the volume-driver plugin.
· volume-label: Custom metadata.
· volume-nocopy: true(default) or false. If set to false, the
Engine copies existing files and directories under the
mount-path into the volume, allowing the host to access them.
· volume-opt: specific to a given volume driver.
Options specific to tmpfs:
· tmpfs-size: Size of the tmpfs mount in bytes. Unlimited by
default in Linux.
· tmpfs-mode: File mode of the tmpfs in octal. (e.g. 700 or
0700.) Defaults to 1777 in Linux.
-v|--volume[=[[HOST-DIR:]CONTAINER-DIR[:OPTIONS]]]
Create a bind mount. If you specify, -v /HOST-DIR:/CONTAINER-DIR,
Docker
bind mounts /HOST-DIR in the host to /CONTAINER-DIR in the Docker
container. If 'HOST-DIR' is omitted, Docker automatically creates
the new
volume on the host. The OPTIONS are a comma delimited list and can
be:
· [rw|ro]
· [z|Z]
· [[r]shared|[r]slave|[r]private]
· [delegated|cached|consistent]
· [nocopy]
The CONTAINER-DIR must be an absolute path such as /src/docs. The
HOST-DIR can be an absolute path or a name value. A name value must
start with an alphanumeric character, followed by a-z0-9, _
(underscore), . (period) or - (hyphen). An absolute path starts with a
/ (forward slash).
If you supply a HOST-DIR that is an absolute path, Docker bind-mounts
to the path you specify. If you supply a name, Docker creates a named
volume by that name. For example, you can specify either /foo or foo
for a HOST-DIR value. If you supply the /foo value, Docker creates a
bind mount. If you supply the foo specification, Docker creates a named
volume.
You can specify multiple -v options to mount one or more mounts to a
container. To use these same mounts in other containers, specify the
--volumes-from option also.
You can supply additional options for each bind mount following an
additional colon. A :ro or :rw suffix mounts a volume in read-only or
read-write mode, respectively. By default, volumes are mounted in
read-write mode. You can also specify the consistency requirement for
the mount, either :consistent (the default), :cached, or :delegated.
Multiple options are separated by commas, e.g. :ro,cached.
Labeling systems like SELinux require that proper labels are placed on
volume content mounted into a container. Without a label, the security
system might prevent the processes running inside the container from
using the content. By default, Docker does not change the labels set by
the OS.
To change a label in the container context, you can add either of two
suffixes :z or :Z to the volume mount. These suffixes tell Docker to
relabel file objects on the shared volumes. The z option tells Docker
that two containers share the volume content. As a result, Docker
labels the content with a shared content label. Shared volume labels
allow all containers to read/write content. The Z option tells Docker
to label the content with a private unshared label. Only the current
container can use a private volume.
By default bind mounted volumes are private. That means any mounts done
inside container will not be visible on host and vice-a-versa. One can
change this behavior by specifying a volume mount propagation property.
Making a volume shared mounts done under that volume inside container
will be visible on host and vice-a-versa. Making a volume slave enables
only one way mount propagation and that is mounts done on host under
that volume will be visible inside container but not the other way
around.
To control mount propagation property of volume one can use :[r]shared,
:[r]slave or :[r]private propagation flag. Propagation property can be
specified only for bind mounted volumes and not for internal volumes or
named volumes. For mount propagation to work source mount point (mount
point where source dir is mounted on) has to have right propagation
properties. For shared volumes, source mount point has to be shared.
And for slave volumes, source mount has to be either shared or slave.
Use df <source-dir> to figure out the source mount and then use findmnt
-o TARGET,PROPAGATION <source-mount-dir> to figure out propagation
properties of source mount. If findmnt utility is not available, then
one can look at mount entry for source mount point in
/proc/self/mountinfo. Look at optional fields and see if any
propagation properties are specified. shared:X means mount is shared,
master:X means mount is slave and if nothing is there that means mount
is private.
To change propagation properties of a mount point use mount command.
For example, if one wants to bind mount source directory /foo one can
do mount --bind /foo /foo and mount --make-private --make-shared /foo.
This will convert /foo into a shared mount point. Alternatively one can
directly change propagation properties of source mount. Say / is source
mount for /foo, then use mount --make-shared / to convert / into a
shared mount.
Note: When using systemd to manage the Docker daemon's start and
stop, in the systemd unit file there is an option to control
mount propagation for the Docker daemon itself, called
MountFlags. The value of this setting may cause Docker to not
see mount propagation changes made on the mount point. For
example, if this value is slave, you may not be able to use the
shared or rshared propagation on a volume.
To disable automatic copying of data from the container path to the
volume, use the nocopy flag. The nocopy flag can be set on bind mounts
and named volumes.
See also --mount, which is the successor of --tmpfs and --volume. Even
though there is no plan to deprecate --volume, usage of --mount is
recommended.
Docker Community Docker User Manuals DOCKER(1)