从派生的 docker 图像在特定 /home 文件夹中创建文件夹不起作用

creation of a folder in a specific /home folder from a derived docker image doesn't work

Dockerfile 中,我有来自派生图像 /home/root 的现有文件夹。我无法在 /home 目录中创建文件夹:

RUN mkdir -p /home/TEST_3

未显示任何错误消息,但我可以看到未创建文件夹。 同样的命令在 /root 目录

中运行良好

两个文件夹在权限方面似乎相似:

drwxr-xr-x   2 root root 4096 Apr 24  2018 home
drwx------   1 root root 4096 Jun 20 08:38 root

硬 link 计数不同。有问题的文件夹 /home 有更多权限

另一件奇怪的事情是我可以使用以下方法在 /home 中创建一个文件夹:

WORKDIR /home/TEST_2

但是我在此文件夹中创建的任何文件似乎都不存在。

这里有一个小 Dockerfile 来重新创建这个 issue/feature

# Specify the base image and tag
FROM gcr.io/deeplearning-platform-release/pytorch-cpu:m27

RUN ls -la
RUN ls -la /home/
RUN ls -la /root/

## creation ofthe folder is working and the folder exist (and I can create a file in the new folder)
RUN mkdir -p /root/TEST_1
RUN ls -la /root/TEST_1
RUN touch /root/TEST_1/new_file.txt
RUN ls -la /root/TEST_1

## creation of the folder is working and the folder exist but the file created doesn't exist
WORKDIR /home/TEST_2
WORKDIR /
RUN ls -la /home/TEST_2
RUN touch /home/TEST_2/new_file.txt
RUN ls -la /home/TEST_2

## creation of the folder succeed (no error messages), but the folder doesn't exist
RUN mkdir -p /home/TEST_3
RUN ls -la /home/TEST_3
RUN touch /home/TEST_3/new_file.txt
RUN ls -la /home/TEST_3

这里是输出:

 docker build -f Dockerfile_test .
Sending build context to Docker daemon  37.89kB
Step 1/17 : FROM gcr.io/deeplearning-platform-release/pytorch-cpu:m27
 ---> 7eb6565a8cc6
Step 2/17 : RUN ls -la
 ---> Running in 1ced0ed2bf49
total 80
drwxr-xr-x   1 root root 4096 Oct 26 12:52 .
drwxr-xr-x   1 root root 4096 Oct 26 12:52 ..
-rwxr-xr-x   1 root root    0 Oct 26 12:52 .dockerenv
drwxr-xr-x   1 root root 4096 Jun 20 08:20 bin
drwxr-xr-x   2 root root 4096 Apr 24  2018 boot
drwxr-xr-x   5 root root  340 Oct 26 12:52 dev
-rwxr-x--x   1 root root   94 Jun 19 21:51 entrypoint.sh
-rwxr-xr-x   1 root root    0 Jun 20 08:38 env.sh
drwxr-xr-x   1 root root 4096 Oct 26 12:52 etc
drwxr-xr-x   2 root root 4096 Apr 24  2018 home
drwxr-xr-x   1 root root 4096 Jun 20 08:22 lib
drwxr-xr-x   2 root root 4096 Jun 12 16:55 lib64
drwxr-xr-x   2 root root 4096 Jun 12 16:54 media
drwxr-xr-x   2 root root 4096 Jun 12 16:54 mnt
drwxr-xr-x   1 root root 4096 Jun 20 08:23 opt
dr-xr-xr-x 226 root root    0 Oct 26 12:52 proc
drwx------   1 root root 4096 Jun 20 08:38 root
drwxr-xr-x   1 root root 4096 Jun 20 08:33 run
-rwxr-x--x   1 root root  109 Jun 19 21:51 run_jupyter.sh
drwxr-xr-x   1 root root 4096 Jun 20 08:20 sbin
drwxr-xr-x   2 root root 4096 Jun 12 16:54 srv
dr-xr-xr-x  13 root root    0 Oct 26 12:52 sys
drwxrwxrwt   1 root root 4096 Jun 20 10:09 tmp
drwxr-xr-x   1 root root 4096 Jun 20 08:33 usr
drwxr-xr-x   1 root root 4096 Jun 12 16:55 var
Removing intermediate container 1ced0ed2bf49
 ---> 87dbdffa7315
Step 3/17 : RUN ls -la /home/
 ---> Running in 756f013caf7f
total 8
drwxr-xr-x 2 root root 4096 Apr 24  2018 .
drwxr-xr-x 1 root root 4096 Oct 26 12:52 ..
Removing intermediate container 756f013caf7f
 ---> 24419a25ac99
Step 4/17 : RUN ls -la /root/
 ---> Running in bde249ae0452
total 52
drwx------ 1 root root 4096 Jun 20 08:38 .
drwxr-xr-x 1 root root 4096 Oct 26 12:52 ..
-rw-r--r-- 1 root root 3106 Apr  9  2018 .bashrc
drwxr-xr-x 3 root root 4096 Jun 20 08:31 .cache
drwxrwsr-x 1 root root 4096 Jun 20 08:26 .conda
drwx------ 4 root root 4096 Jun 20 08:33 .config
-rw-r--r-- 1 root root  401 Jun 20 08:33 .gitconfig
drwxr-xr-x 3 root root 4096 Jun 20 08:38 .ipython
drwxr-xr-x 1 root root 4096 Jun 20 10:10 .jupyter
drwxr-xr-x 3 root root 4096 Jun 20 08:31 .npm
-rw-r--r-- 1 root root  148 Aug 17  2015 .profile
-rw-r--r-- 1 root root  405 Jun 20 10:10 .wget-hsts
drwxrwxrwx 1 root root 4096 Jun 20 10:09 miniconda3
Removing intermediate container bde249ae0452
 ---> f6e6b3780145
Step 5/17 : RUN mkdir -p /root/TEST_1
 ---> Running in 95ffe0bc574f
Removing intermediate container 95ffe0bc574f
 ---> a87cac09ec17
Step 6/17 : RUN ls -la /root/TEST_1
 ---> Running in b8fd10afa3ee
total 8
drwxr-xr-x 2 root root 4096 Oct 26 12:52 .
drwx------ 1 root root 4096 Oct 26 12:52 ..
Removing intermediate container b8fd10afa3ee
 ---> 87b173385d4b
Step 7/17 : RUN touch /root/TEST_1/new_file.txt
 ---> Running in cbdd9ac79955
Removing intermediate container cbdd9ac79955
 ---> 0f57f2c15855
Step 8/17 : RUN ls -la /root/TEST_1
 ---> Running in 0a7d46a44e9e
total 8
drwxr-xr-x 1 root root 4096 Oct 26 12:53 .
drwx------ 1 root root 4096 Oct 26 12:52 ..
-rw-r--r-- 1 root root    0 Oct 26 12:53 new_file.txt
Removing intermediate container 0a7d46a44e9e
 ---> 5413892c3b96
Step 9/17 : WORKDIR /home/TEST_2
 ---> Running in d0322094ae3b
Removing intermediate container d0322094ae3b
 ---> 9815f6870830
Step 10/17 : WORKDIR /
 ---> Running in f84feebf3db9
Removing intermediate container f84feebf3db9
 ---> 5c85f2fadd04
Step 11/17 : RUN ls -la /home/TEST_2
 ---> Running in 4644cbcd5c3c
total 8
drwxr-xr-x 2 root root 4096 Oct 26 12:53 .
drwxr-xr-x 3 root root 4096 Oct 26 12:53 ..
Removing intermediate container 4644cbcd5c3c
 ---> c82625e638d9
Step 12/17 : RUN touch /home/TEST_2/new_file.txt
 ---> Running in ac89ce336a51
Removing intermediate container ac89ce336a51
 ---> d9569d5ca42d
Step 13/17 : RUN ls -la /home/TEST_2
 ---> Running in 999f75b4ece1
total 8
drwxr-xr-x 2 root root 4096 Oct 26 12:53 .
drwxr-xr-x 3 root root 4096 Oct 26 12:53 ..
Removing intermediate container 999f75b4ece1
 ---> 57062c0ccf0a
Step 14/17 : RUN mkdir -p /home/TEST_3
 ---> Running in f9788ac58dc7
Removing intermediate container f9788ac58dc7
 ---> e88a503ae524
Step 15/17 : RUN ls -la /home/TEST_3
 ---> Running in 16ea32c637c6
ls: cannot access '/home/TEST_3': No such file or directory
The command '/bin/sh -c ls -la /home/TEST_3' returned a non-zero code: 2

我在 MacOS 10.14.6

上使用 Docker version 19.03.4, build 9013bf5

我的问题如下:

  1. 区别在哪里(能够在/root中创建文件夹,但不能在/home
  2. 中创建文件夹
  3. 如何解决此问题以便能够在 /home 中创建文件夹以及 也可以在新文件夹中创建文件。

TL;DR

what is the difference between /home and /root

/home 有一个关联的卷

how to get around the issue

在入口点创建文件(在构建容器之后),或者在卷内创建文件(我在长答案中使用第一个选项)

长答案:

运行 docker inspect containerId 在构建图像和容器后显示它们的与主目录关联的卷:

"Source": "/var/lib/docker/volumes/1447dbc568742221a757a650427f38fb485e42fc7c6e959f33966922fe862d13/_data",
                "Destination": "/home",

该卷是 RW(读写),因此您可以在容器内写入一次文件。 (我使用 docker exec -it containerId /bin/sh 然后 touch /home/TEST_2/new_file.txt 进行了测试)

尝试使用入口点脚本创建 folders/files。

编辑:

我想亲眼看看,但 运行 遇到了几个障碍。父容器 (gcr.io/deeplearning-platform-release/pytorch-cpu:m27) 已经有一个 entrypoint.sh 文件。

此外,添加 CMDENTRYPOINT 命令将覆盖父入口点容器(您可能需要)。

工作解决方案:

从主机上,复制 entrypoint.sh 文件(假设您想在主机上修改它)。 docker cp containerId:/entrypoint.sh .

修改入口点,添加您的 TOUCH 命令:

#!/bin/bash
touch /home/TEST_2/new_file.txt # <-- I added this
. /env.sh
. /root/miniconda3/etc/profile.d/conda.sh
conda activate base
exec "$@"

更改您的 dockerfile 以复制新的 entrypoint.sh,这将替换现有的:

COPY entrypoint.sh /entrypoint.sh

您在启动时的列表命令将是相同的,但是如果您进入容器,您将看到您的文件:

# ls home/TEST_2
new_file.txt
#