Docker 创建后卷为空
Docker Volume empty after it's created
我有一个简单的 Dockerfile
FROM alpine
RUN apk add --no-cache lsyncd
CMD ["ls", "-al", "/etc/lsyncd"]
我构建了图像并且它工作正常,如果我 运行 它工作正常:
docker run -i -t -P <NAME_OF_THE_IMAGE>
我得到了包含预期文件的文件夹列表,因为 CMD 会这样做。
如果我这样调:
docker run -i -t -P -v /docker/dcm/tst:/etc/lsyncd <NAME_OF_THE_IMAGE>
它创建了空的“/docker/dcm/tst”文件夹,ls 命令也returns 空。
如果我的理解是正确的,如果本地计算机上的文件夹“/docker/dcm/tst”不存在,将创建一个新文件夹并复制文件夹“/etc/lsyncd”的内容在新文件夹中。
我的理解对吗?是什么导致了我所看到的问题?
uname -a
Linux docker 4.9.53-5.ph2-esx #1-photon SMP Thu Oct 26 02:44:24 UTC 2017 x86_64 Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz GenuineIntel GNU/Linux
Bind mount
用于将主机上的文件或目录挂载到容器中
关于Mounting into a non-empty directory on the container
,是这样说的
If you bind-mount into a non-empty directory on the container, the directory’s existing contents are obscured by the bind mount.
For detail refer this.
这意味着bind mount
只会覆盖容器目录中的内容。
我认为您对 volume
感到困惑,它会在第一次创建时将容器目录中的内容复制到 volume
中。但是,您使用的是 bind mount
但它不是那样工作的。
命名卷和主机卷的行为不同。主机卷,又名绑定挂载,将目录映射到容器中,就像它存在于主机上一样。没有初始化过程。
命名卷支持在容器启动时该卷为空时初始化该卷的内容。会初始化为所选位置的图片内容,包括file/directoryuid/gid和权限
要获取具有主机目录的命名卷,您可以使用以下选项之一定义绑定安装的命名卷:
# create the volume in advance
$ docker volume create --driver local \
--opt type=none \
--opt device=/home/user/test \
--opt o=bind \
test_vol
# create on the fly with --mount
$ docker run -it --rm \
--mount type=volume,dst=/container/path,volume-driver=local,volume-opt=type=none,volume-opt=o=bind,volume-opt=device=/home/user/test \
foo
# inside a docker-compose file
...
volumes:
bind-test:
driver: local
driver_opts:
type: none
o: bind
device: /home/user/test
据我所知,还有一个行为更改涉及指向绑定安装的命名卷,docker 如果目录不存在,则不会创建该目录。相反,容器创建将因卷创建错误而失败。
我有一个简单的 Dockerfile
FROM alpine
RUN apk add --no-cache lsyncd
CMD ["ls", "-al", "/etc/lsyncd"]
我构建了图像并且它工作正常,如果我 运行 它工作正常:
docker run -i -t -P <NAME_OF_THE_IMAGE>
我得到了包含预期文件的文件夹列表,因为 CMD 会这样做。
如果我这样调:
docker run -i -t -P -v /docker/dcm/tst:/etc/lsyncd <NAME_OF_THE_IMAGE>
它创建了空的“/docker/dcm/tst”文件夹,ls 命令也returns 空。
如果我的理解是正确的,如果本地计算机上的文件夹“/docker/dcm/tst”不存在,将创建一个新文件夹并复制文件夹“/etc/lsyncd”的内容在新文件夹中。
我的理解对吗?是什么导致了我所看到的问题?
uname -a
Linux docker 4.9.53-5.ph2-esx #1-photon SMP Thu Oct 26 02:44:24 UTC 2017 x86_64 Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz GenuineIntel GNU/Linux
Bind mount
用于将主机上的文件或目录挂载到容器中
关于Mounting into a non-empty directory on the container
,是这样说的
If you bind-mount into a non-empty directory on the container, the directory’s existing contents are obscured by the bind mount.
For detail refer this.
这意味着bind mount
只会覆盖容器目录中的内容。
我认为您对 volume
感到困惑,它会在第一次创建时将容器目录中的内容复制到 volume
中。但是,您使用的是 bind mount
但它不是那样工作的。
命名卷和主机卷的行为不同。主机卷,又名绑定挂载,将目录映射到容器中,就像它存在于主机上一样。没有初始化过程。
命名卷支持在容器启动时该卷为空时初始化该卷的内容。会初始化为所选位置的图片内容,包括file/directoryuid/gid和权限
要获取具有主机目录的命名卷,您可以使用以下选项之一定义绑定安装的命名卷:
# create the volume in advance
$ docker volume create --driver local \
--opt type=none \
--opt device=/home/user/test \
--opt o=bind \
test_vol
# create on the fly with --mount
$ docker run -it --rm \
--mount type=volume,dst=/container/path,volume-driver=local,volume-opt=type=none,volume-opt=o=bind,volume-opt=device=/home/user/test \
foo
# inside a docker-compose file
...
volumes:
bind-test:
driver: local
driver_opts:
type: none
o: bind
device: /home/user/test
据我所知,还有一个行为更改涉及指向绑定安装的命名卷,docker 如果目录不存在,则不会创建该目录。相反,容器创建将因卷创建错误而失败。