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.
Don't rely on shell expansions like ~
in COPY
instructions. Use COPY conf/phantomjs.sh /path/to/user/home/phantomjs.sh
instead!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With