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 ?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With