lightning-li
lightning-li

Reputation: 121

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

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 ?

Upvotes: 12

Views: 14165

Answers (5)

wassgren
wassgren

Reputation: 19231

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.

Upvotes: 14

HelloWorld
HelloWorld

Reputation: 627

Expounding on wassgren's answer, you can also you a docker SDK to do this. I'm not sure about every SDK, but at least docker-java sends the context as an inputstream to docker. So, you specify dockerClient.withBaseDirectory() and dockerClient.withDockerFile(). You could use a more scripting approach and use gradle or groovy to do this and avoid having to "compile" java for this everytime.

Upvotes: 0

Zaz
Zaz

Reputation: 48829

To copy a file, simply use docker cp:

docker cp file.txt CONTAINER:/path/to/dest

To copy a directory, use a tar pipe:

tar -c "dir/" | docker run -i -a stdin CONTAINER tar -xC /path/to/dest

Upvotes: 0

wikier
wikier

Reputation: 2576

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.

Upvotes: 2

eljefedelrodeodeljefe
eljefedelrodeodeljefe

Reputation: 6811

There is a workaround when you use docker-compose:

api1:
  build: .
  dockerfile: cluster/manifests/Dockerfile-NodeAPI
  ports:
    - 8080
  links:
    - mongo
    - redis
  command: "nodemon lib/app.js -e js,json,scss,css,html"

This still won't allow you to do relatives paths, however basepath in the Dockerfile will be ./ instead of cluster/manifests/

Upvotes: 0

Related Questions