I have written a Dockerfile which looks like this
FROM ubuntu:12.04 RUN apt-get update RUN apt-get install -y wget
Now I'm having a file called abc.txt
in my host machine. How can I copy it to this container. Is there any step that I can add in Dockerfile which copy from Host to Container.
The Docker cp command can be used to copy files and directories from the host machine to a container and vice-versa.
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.
Docker cp Command It can only be used to copy files between the host system and a single container.
Use COPY command like this:
COPY foo.txt /data/foo.txt # where foo.txt is the relative path on host # and /data/foo.txt is the absolute path in the image
read more details for COPY in the official documentation
An alternative would be to use ADD but this is not the best practise if you dont want to use some advanced features of ADD like decompression of tar.gz files.If you still want to use ADD command, do it like this:
ADD abc.txt /data/abc.txt # where abc.txt is the relative path on host # and /data/abc.txt is the absolute path in the image
read more details for ADD in the official documentation
For those who get this (terribly unclear) error:
COPY failed: stat /var/lib/docker/tmp/docker-builderXXXXXXX/abc.txt: no such file or directory
There could be loads of reasons, including:
context
overwrites the context of the Dockerfile. Your COPY statements now need to navigate a path relative to what is defined in docker-compose.yml instead of relative to your Dockerfile.COPY abc.txt /app #This won't work
.dockerignore
or .gitignore
files (be wary of wildcards)Sometimes WORKDIR /abc
followed by COPY . xyz/
works where COPY /abc xyz/
fails, but it's a bit ugly.
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