PaulB
PaulB

Reputation: 71

Dockerfile: Copied file not found

I'm trying to add a shell script to a container, and then run it as part of my build.

Here's the relevant section of the dockerfile:

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

And here's the output during the build:

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

the file I'm copying exists in the conf folder underneath the build directory... but whatever I do it doesn't seem to be copied across.

Upvotes: 7

Views: 12247

Answers (4)

Crazy Coder
Crazy Coder

Reputation: 11

download the required file from github repo into Build folder and run the build command again, it will pickup the file during build process.

Upvotes: 0

Leon
Leon

Reputation: 438

~ isnt a context understood by Docker when you use the COPY command. It will simply create a folder named ~ as in /~/phantomjs.sh . Use $HOME to reference or an explicit mention of the path to use this in the right context.

Upvotes: 1

helmbert
helmbert

Reputation: 38064

TL;DR

Don't rely on shell expansions like ~ in COPY instructions. Use COPY conf/phantomjs.sh /path/to/user/home/phantomjs.sh instead!

Detailed explanation

Using ~ as shortcut for the user's home directory is a feature offered by your shell (i.e. Bash or ZSH). COPY instructions in a Dockerfile are not run in a shell; they simply take file paths as an argument (also see the manual).

This issue can easily be reproduced with a minimal Dockerfile:

FROM alpine
COPY test ~/test

Then build and run:

$ echo foo > test
$ docker built -t test
$ docker run --rm -it test /bin/sh

When running ls -l / inside the container, you'll see that docker build did not expand ~ to /root/, but actually created the directory named ~ with the file test in it:

/ # ls -l /~/test.txt 
-rw-r--r--    1 root     root             7 Jan 16 12:26 /~/test.txt

Upvotes: 10

yftse
yftse

Reputation: 193

not sure, but maybe ~ in the Dockerfile is referencing your $HOME, /home/$USER on the host.

i would try to change the reference from ~ or $HOME to explicit folder /root:

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

Upvotes: 3

Related Questions