Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 849
PaulB Avatar asked Jan 16 '16 12:01

PaulB


1 Answers

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
like image 108
helmbert Avatar answered Oct 13 '22 05:10

helmbert