【发布时间】:2018-12-30 08:18:04
【问题描述】:
如果你绑定挂载一个不存在的文件(在主机上),docker 会很高兴地在它的位置创建一个目录并与容器共享它。在“修复”错误后(即停止容器、用文件替换目录并启动容器),您将收到以下错误:
docker:来自守护进程的错误响应:oci 运行时错误:container_linux.go:262:启动容器进程导致“process_linux.go:339:容器初始化导致”rootfs_linux.go:57:将“[snip]”挂载到 [snip ]" at "[snip]" 导致 "不是目录""" :您是否尝试将目录挂载到文件上(反之亦然)?检查指定的主机路径是否存在并且是预期的类型。
以下是从控制台重现的一些步骤:
# ensure test file does not exist
$ rm -f /test.txt
# run hello-world container with test.txt bind mount
$ docker run --name testctr -v /test.txt:/test.txt hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
<snip>
# let's say what's in /
$ ls -l /
<snip>
dr-xr-xr-x 13 root root 0 Jul 17 01:08 sys
drwxr-xr-x 2 root root 4096 Jul 22 20:54 test.txt
drwxrwxrwt 1 root root 4096 Jul 17 09:01 tmp
<snip>
# let's correct the mistake and run the container again
$ rm -rf /test.txt
$ touch /test.txt
$ docker start testctr
Error response from daemon: OCI runtime create failed: container_linux.go:348: starting
container process caused "process_linux.go:402: container init caused \"rootfs_linux.go:58:
mounting \\\"/test.txt\\\" to rootfs \\\"/var/lib/docker/overlay2/
26fd6981e919e5915c31098fc74551314c4f05ce6c5e175a8be6191e54b7f807/merged\\\" at \\\"/var/lib/
docker/overlay2/26fd6981e919e5915c31098fc74551314c4f05ce6c5e175a8be6191e54b7f807/merged/
test.txt\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory
onto a file (or vice-versa)? Check if the specified host path exists and is the expected
type
Error: failed to start containers: testctr
请注意,即使我们在启动现有容器时遇到此错误,创建一个新容器实际上也可以。
所以我的问题是,我该如何解决这个问题?我可以看到两种不同的可能性:
- 重新创建容器:
- 以某种方式将创建容器 (
docker run ...) 的命令导出到变量 (?) 中 - 删除旧容器
- 第1步生成的运行命令
- 以某种方式将创建容器 (
- 以某种方式调整现有容器以修复挂载
- 这可能无法通过 docker 完成,因为显然绑定挂载不是由 docker 管理的
PS:这个问题也应该解决this one。
【问题讨论】:
-
我很确定您需要删除并重新创建容器。这是非常常规的,如果您的
docker run命令特别涉及将其写入脚本或 Docker Compose YAML 文件之类的东西,那么这是一个好主意。 -
感谢您的回复。这里的问题是我需要对一些任意容器执行此过程。因此,如果我选择选项 1,我需要能够生成最初创建容器的命令(
docker run ...或至少是docker create ...)。