Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I copy a directory or file to a docker image from localhost on ubuntu14.04?

Tags:

docker

when i add a file or directory to docker image by a Dockerfile, I use ADD or COPY , but the file path i want to add or copy must a relative path to the Dockerfile's directory. Is there a method for adding a file or directory from localhost to docker image by using absolute path? by the way, why the "docker cp" can only support copying file from docker image to localhost? on the contrary , it doesn't work ?

like image 865
lightning-li Avatar asked Oct 24 '14 10:10

lightning-li


People also ask

How do I copy a folder from host to container?

Docker to host file copy commandAfter you specify the container name or ID, simply provide the full path to the file you want to copy, and then provide the name of the destination folder on the host system.


Video Answer


2 Answers

The short answer is that it is not supported.

From the Docker docs:

The <src> 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.

Furthermore, symbolic links are not supported so it is not possible to trick the build by linking to a different location on the localhost.

However, URL:s are supported so one way to get around the problem is to to serve the files over HTTP if you enable a web server on your localhost (or in a docker container).

The command docker cp (unfortunately) is only supported from the container to the host, not vice versa. This is described in the docs. The way to go around this is to use docker volumes where the data can be shared.

like image 144
wassgren Avatar answered Oct 28 '22 15:10

wassgren


The solution for those who use composer is to use a volume pointing to the folder:

#docker-composer.yml

foo:
  build: foo
  volumes:
    - /local/path/:/path/:ro

But I'm pretty sure the can be done playing with volumes in Dockerfile.

like image 2
wikier Avatar answered Oct 28 '22 14:10

wikier