13

# Update

I just realized that ADD/COPY command doesn't permit any access to files or directories outside of current working directory in host. One more thing is that if you specify an absolute path of file/directory as a source path after ADD/COPY command, it'll also not be permitted.

Please refer to this and have happy hacking ! :)

=======================================================================

I would like to copy/add files under a user's home directory in host into the container's home directory for the same user.

First of all, a user can be changed as the user who is building a docker image with Dockerfile on each host. For instance, in my host, I have a user "test". In the other person's host, there will be a user "newbie". In each host, my Dockerfile will be built/used.

The following is my test syntax for copying/adding files.

...
RUN mkdir -p /home/${USER}/.ssh

ADD /home/${USER}/.ssh/id_rsa* /home/${USER}/.ssh/
or COPY /home/${USER}/.ssh/id_rsa* /home/${USER}/.ssh/ 
...

When I try to build this Docker file, the following error is displayed.

Step 43/44 : ADD /home/user/.ssh/id_rsa* /home/${USER}/.ssh/
No source files were specified

Please kindly guide me to do what I want to do. :) Thanks.

Sung-Jin Park
  • 149
  • 1
  • 1
  • 4

3 Answers3

3

It has now been two years sice the question has been ask, but I want to cite to official documentation here which states the same as what @Sung-Jin Park already found out.

ADD obeys the following rules:

  • The path must be inside the context of the build; you cannot ADD ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.

Dockerfile reference ADD

Westranger
  • 1,308
  • 19
  • 29
0

The only way to accomplish the end goal is to first create a symlink for the file you want copied from outside of the build directory into the build directory.

# build/.gitignore
private/
# build/
mkdir private
ln $HOME/.ssh/id_rsa private/id_rsa
# build/Dockerfile
RUN mkdir -p /home/${USER}/.ssh

ADD /home/${USER}/.ssh/id_rsa* /home/${USER}/.ssh/
or COPY /home/${USER}/.ssh/id_rsa* /home/${USER}/.ssh/ 
Jens Bodal
  • 1,707
  • 1
  • 22
  • 32
-4

you can use the following:

WORKDIR /home COPY ${pwd}/my-file.txt .

Benjamin Slabbert
  • 511
  • 2
  • 9
  • 9