gpt4 book ai didi

Dockerfile:找不到复制的文件

转载 作者:行者123 更新时间:2023-12-04 22:29:37 26 4
gpt4 key购买 nike

我正在尝试将 shell 脚本添加到容器中,然后将其作为构建的一部分运行。

这是 dockerfile 的相关部分:

#phantomjs install
COPY conf/phantomjs.sh ~/phantomjs.sh
RUN chmod +x ~/phantomjs.sh && ~/phantomjs.sh

这是构建期间的输出:
Step 16 : COPY conf/phantomjs.sh ~/phantomjs.sh
---> Using cache
---> 8199d97eb936
Step 17 : RUN chmod +x ~/phantomjs.sh && ~/phantomjs.sh
---> Running in 3c7ecb307bd3
[91mchmod: [0m[91mcannot access '/root/phantomjs.sh'[0m[91m: No such file or directory[0m[91m

我正在复制的文件存在于构建目录下的 conf 文件夹中……但无论我做什么,它似乎都没有被复制。

最佳答案

TL; 博士
不要依赖像 ~ 指令中的 COPY 这样的 shell 扩展。改用 COPY conf/phantomjs.sh /path/to/user/home/phantomjs.sh !
详细说明
使用 ~ 作为用户主目录的快捷方式是您的 shell(即 Bash 或 ZSH)提供的一项功能。 Dockerfile 中的 COPY 指令不在 shell 中运行;他们只是将文件路径作为参数(另见 manual )。
使用最小的 Dockerfile 可以轻松重现此问题:

FROM alpine
COPY test ~/test
然后构建并运行:
$ echo foo > test
$ docker built -t test
$ docker run --rm -it test /bin/sh
在容器内运行 ls -l / 时,您会看到 docker build 没有将 ~ 扩展为 /root/ ,而是实际创建了名为 ~ 的目录,其中包含文件 test:
/ # ls -l /~/test.txt 
-rw-r--r-- 1 root root 7 Jan 16 12:26 /~/test.txt

关于Dockerfile:找不到复制的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34826860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com