6

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?

meDeepakJain
  • 156
  • 1
  • 13
xava
  • 293
  • 1
  • 2
  • 16

4 Answers4

5

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
LinPy
  • 16,987
  • 4
  • 43
  • 57
  • `sudo docker exec my_container mkdir -p /my_folder` It seem like I cannot create such folder: mkdir: cannot create directory '/my_folder': Permission denied – xava Jun 29 '19 at 11:31
3

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.

mchawre
  • 10,744
  • 4
  • 35
  • 57
  • 1
    I have written my commands falsely in the post. I have corrected it. so your command is actually what I have already tried – xava Jun 29 '19 at 11:22
  • You need to ensure that `/my_folder` actually exists inside the container before you can copy a file into that directory. – John Jun 29 '19 at 11:25
  • 1
    when I do `docker exec -it my_container` and then list all folders, my_folder shows up – xava Jun 29 '19 at 11:27
  • I tried the same command I mentioned in my answer and it worked for me. Please provide more details. – mchawre Jun 29 '19 at 11:33
  • Updated my answer please try the steps at your end which I mentioned in update-2 section of my answer. – mchawre Jun 29 '19 at 11:37
0

Ensure also not having typos in the container name, otherwise the same error will show up too

weshouman
  • 632
  • 9
  • 17
0

I had the same issue on Windows, and the problem is it seems docker cp does not support trailing \, for instance:

docker cp toto.txt my_container:C:\MyFolderThatExitsOnContainer

does not work, but:

docker cp toto.txt my_container:C:\MyFolderThatExitsOnContainer\

gives me the no such directory error.

I assume there may be the same issue on Linux with /.

gluttony
  • 402
  • 6
  • 14