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 ?
4 Solutions collect form web for “How can I copy a directory or file to a docker image from localhost on ubuntu14.04?”
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).
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.
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.
There is a workaround when you use
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
To copy a file, simply use
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