【问题标题】:chown not working when coping a file in a dockerfile在dockerfile中复制文件时chown不起作用
【发布时间】:2022-01-27 13:45:33
【问题描述】:

我在 Windows 上运行 docker 引擎并尝试将我自己的文件添加到图像中。问题是当我复制文件时,它的所有权始终是 root:root 但它需要是 heartbeat:heartbeat (图像上的现有用户)。在 windows atm 上使用 -v 参数和 docker run 挂载单个文件是不可能的。这就是我尝试使用 docker 文件创建自己的图像的原因:

FROM docker.elastic.co/beats/heartbeat:7.16.3
USER root
COPY --chown=heartbeat:heartbeat yml/heartbeat.yml /usr/share/heartbeat/heartbeat.yml
RUN chown -R heartbeat:heartbeat /usr/share/heartbeat

应对后的 --chown 参数什么都不做。当我检查时它仍然是 root 并且 RUN chown 命令导致错误。这里的输出:

docker image build ./ -t custom/heartbeat:7.16.3
Sending build context to Docker daemon  10.75kB
Step 1/4 : FROM docker.elastic.co/beats/heartbeat:7.16.3
 ---> b64ad4b42006
Step 2/4 : USER root
 ---> Using cache
 ---> 922a9121e51b
Step 3/4 : COPY --chown=heartbeat:heartbeat yml/heartbeat.yml /usr/share/heartbeat/heartbeat.yml
 ---> Using cache
 ---> f30eb4934dca
Step 4/4 : RUN chown -R heartbeat:heartbeat /usr/share/heartbeat
 ---> [Warning] The requested image's platform (linux/amd64) does not match the detected host platform (windows/amd64) and no specific platform was requested
 ---> Running in 2ae3bfdd5422
The command '/bin/sh -c chown -R heartbeat:heartbeat /usr/share/heartbeat' returned a non-zero code: 4294967295: failed to shutdown container: container 2ae3bfdd5422e81461a14896db0908e4cd67af1a6f99c629abff1e588f62fc32 encountered an error during hcsshim::System::waitBackground: failure in a Windows system call: The virtual machine or container with the specified identifier is not running. (0xc0370110): subsequent terminate failed container 2ae3bfdd5422e81461a14896db0908e4cd67af1a6f99c629abff1e588f62fc32 encountered an error during hcsshim::System::waitBackground: failure in a Windows system call: The virtual machine or container with the specified identifier is not running. (0xc0370110)

欢迎所有帮助...

使用 --platform 运行:

PS C:\SynteticMonitoring> docker image build ./ -t custom/heartbeat:7.16.3
Sending build context to Docker daemon  9.728kB
Step 1/4 : FROM --platform=linux/amd64 docker.elastic.co/beats/heartbeat:7.16.3
 ---> b64ad4b42006
Step 2/4 : USER root
 ---> Using cache
 ---> 922a9121e51b
Step 3/4 : COPY --chown=heartbeat:heartbeat yml/heartbeat.yml /usr/share/heartbeat/heartbeat.yml
 ---> Using cache
 ---> f30eb4934dca
Step 4/4 : RUN chmod +r /usr/share/heartbeat/heartbeat.yml
 ---> Using cache
 ---> e9a075d2ab53
Successfully built e9a075d2ab53
Successfully tagged custom/heartbeat:7.16.3
PS C:\SynteticMonitoring> docker run --interactive --tty --entrypoint /bin/sh custom/heartbeat:7.16.3
sh-4.2# ls -l
total 106916
-rw-r--r-- 1 root root     13675 Jan  7 00:47 LICENSE.txt
-rw-r--r-- 1 root root   1964303 Jan  7 00:47 NOTICE.txt
-rw-r--r-- 1 root root       851 Jan  7 00:47 README.md
drwxrwxr-x 2 root root      4096 Jan  7 00:48 data
-rw-r--r-- 1 root root    374197 Jan  7 00:47 fields.yml
-rwxr-xr-x 1 root root 107027952 Jan  7 00:47 heartbeat
-rw-r--r-- 1 root root     69196 Jan  7 00:47 heartbeat.reference.yml
-rw-rw-rw- 1 root root      1631 Jan 26 06:49 heartbeat.yml
drwxr-xr-x 2 root root      4096 Jan  7 00:47 kibana
drwxrwxr-x 2 root root      4096 Jan  7 00:48 logs
drwxr-xr-x 2 root root      4096 Jan  7 00:47 monitors.d
sh-4.2# pwd
/usr/share/heartbeat

【问题讨论】:

  • 你使用哪个版本的 docker?标记--chown 是在版本 17.09 中添加的
  • 版本:20.10.7 API 版本:1.41 Go 版本:go1.13.15 Git 提交:40ef3b6 内置:08/19/2021 18:54:26 OS/Arch:windows/amd64 上下文:默认实验: 真
  • 对于它的价值,我已经尝试在 Linux 上重新创建并没有成功。您的 Dockerfile 应该是正确的,即使没有 chown -R。最好的猜测是它是否与 Windows 文件系统有关,可能是映射所有权,因为您以 root 身份运行容器。
  • 能否点击托盘中的docker图标切换到Linux容器,再试一次?

标签: linux docker elasticsearch dockerfile


【解决方案1】:

您不能将文件chown 提供给不存在的用户。您的基本映像中似乎不存在heartbeat 用户和组。

这就是为什么COPY --chown 什么都不做,而你得到的文件归root 所有。

您可以通过在COPYing 之前创建用户来解决此问题。为此,请在您的 COPY 语句之前添加一行,例如:

RUN addgroup heartbeat && adduser -S -H heartbeat -G heartbeat

如果您的基本映像中没有 addgroupadduser,请尝试替代方案:

RUN useradd -rUM -s /usr/sbin/nologin heartbeat

这将创建组和用户heartbeat,然后chown 将能够成功更改所有权。

【讨论】:

    【解决方案2】:

    根据Dockerfile documentation

    可选的--platform 标志可用于指定图像的平台,以防FROM 引用多平台图像。例如,linux/amd64linux/arm64windows/amd64默认使用构建请求的目标平台

    我建议尝试类似:

    FROM [--platform=<platform>] <image> [AS <name>]
    FROM --platform=linux/amd64 docker.elastic.co/beats/heartbeat:7.16.3
    

    【讨论】:

    • 感谢您的提示。不幸的是我仍然有问题
    猜你喜欢
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 2021-06-18
    • 2016-04-18
    相关资源
    最近更新 更多