Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to docker cp correctly?

Tags:

docker

I am trying to copy a file from my host to my container. I have already checked out many threads but neither of those work out for me.

File name, I'm trying to copy: ex.txt

Container folder where it needs to be: my_folder

user:~$ docker exec -it my_container bash
a5b13d9a55fd:~S ls
my_folder

What I have tried so far:

user:~$ docker cp ex.txt my_container:/my_folder/
no such directory

user:~$ docker cp ex.txt my_container:/my_folder/ex.txt
Error response from daemon: lstat /var/lib/docker/aufs/mnt/f7796d886aa3673be37b1d346190b7d6ba0ed64edf83bf62bff325f87eaec5eb/my_folder: no such file or directory

Please suggest where am I missing the code?

like image 652
xava Avatar asked Jul 31 '26 09:07

xava


2 Answers

EDIT: since the Image seems to use a none ROOT User you may try this:

docker cp ex.txt my_container:$HOME/my_folder/ex.txt

you should make sure that my_folder is already in the container, to be sure run this command at first:

docker exec my_container_name mkdir -p $HOME/my_folder
like image 72
LinPy Avatar answered Aug 03 '26 03:08

LinPy


Please go through offical documentation properly.

Also check this out.

In your case this should work.

docker cp ex.txt my_container:/my_folder/

Update-1:

In your case, I doubt /my_folder is not present inside the container, this is what the error says.

Also quoting the line mentioned in official documentation.

docker cp does not create parent directories for DEST_PATH if they do not exist.

So the /my_folder directory will not get created automatically.

Do this. docker exec -it my_container mkdir /my_folder and then run docker cp command.

Update-2:

If nothing is working then please try this, it worked for me.

$ cat /root/ex.txt
abc
$ docker run -itd alpine sh
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
921b31ab772b: Pull complete
Digest: sha256:ca1c944a4f8486a153024d9965aafbe24f5723c1d5c02f4964c045a16d19dc54
Status: Downloaded newer image for alpine:latest
35ad53b81c30f675b28a53e6a266f039cf49e90705d41e499deb4f17ab900255
$
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
35ad53b81c30        alpine              "sh"                3 seconds ago       Up 2 seconds                            mystifying_babbage
$
$ docker exec -it 35ad53b81c30 sh
/ # ls
bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var
/ # mkdir /my_folder
$
$ docker cp /root/ex.txt mystifying_babbage:/my_folder/
$
$ docker exec -it 35ad53b81c30 sh
/ # ls /my_folder/
ex.txt
/ # cat /my_folder/ex.txt
abc
/ #

Hope this helps.

like image 34
mchawre Avatar answered Aug 03 '26 04:08

mchawre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!