Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy/add files in user's home directory in host to container's home directory?

# 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.

like image 894
Sung-Jin Park Avatar asked May 30 '17 02:05

Sung-Jin Park


People also ask

How do I transfer files from container to host?

To summarize, follow these steps to copy a file from a Docker container to a host machine: Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container.

How do I copy files from one container to another?

You can use the docker cp command to copy the file. The first path (Source) is the path in the Docker Container and the second one is the path inside your Local System (Destination).

How do I copy multiple files from docker container to local machine?

Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH Copy files/folders between a container and the local filesystem Use '-' as the source to read a tar archive from stdin and extract it to a directory destination in a container.


1 Answers

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

like image 183
Westranger Avatar answered Oct 12 '22 23:10

Westranger