Dockerfile:复制的文件未找到。

6

我想将一个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

我正在复制的文件存在于build目录下的conf文件夹中......但无论我怎么做,它似乎都没有被复制过去。


1
曾遇到相同问题,按照答案中所述将 ~ 替换即可解决。 - MattC
4个回答

10

简述

不要在 COPY 指令中依赖像 ~ 这样的 shell 扩展,应该使用 COPY conf/phantomjs.sh /path/to/user/home/phantomjs.sh 代替!

详细解释

使用 ~ 作为用户主目录的快捷方式是由您的 shell(即 Bash 或 ZSH)提供的功能。 Dockerfile 中的 COPY 指令不在 shell 中运行;它们只将文件路径作为参数(也请参见手册)。

这个问题可以很容易地通过一个简单的 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

3

我不确定,但也许Dockerfile中的~引用了您主机上的$HOME,即/home/$USER

我建议将引用从~$HOME更改为显式文件夹/root

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

1

~ 在使用 COPY 命令时,Docker 不会理解此上下文。它只会简单地创建一个名为 ~ 的文件夹,例如 /~/phantomjs.sh。 请使用 $HOME 进行引用或显式指定路径以在正确的上下文中使用。


0

从 Github 仓库下载所需文件到 Build 文件夹中,然后再次运行构建命令,它将在构建过程中获取该文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接