19

I am new to docker concept and trying to copy some file from my host to the container. Assuming that my docker name is my_docker when I run the following:

docker cp my_docker:/usr/local/src/test.txt test.txt

It copies the test.txt file from my container to the local host. But doing it the other way around is not working. Here is the command:

docker cp test.txt my_docker:/usr/local/src

Is there something that I am doing run? Any help would be much appreciated.

Thanks

8
  • Do you try to use docker cp test.txt my_docker:/usr/local/src/test.txt ? Commented Jun 20, 2016 at 16:03
  • Yes. Still not working. It was working just fine last week but not working any more. Commented Jun 20, 2016 at 16:15
  • Do you get any error message? If so, can you post it here. I tried with latest docker, command seems to be working. Commented Jun 20, 2016 at 16:18
  • 1
    @nimafl I'm using docker run -it docker_file_name to get interactive shell to my running docker and check if files are copied or not. Commented Jun 22, 2016 at 15:04
  • 4
    Ow, each time you do that, you are basically running a new docker with a new random name and unique id, so basically you are trying to copy files into one docker and checking a whole new docker for results ! Commented Jun 22, 2016 at 15:24

2 Answers 2

4

As ahajib mentioned in the comments, you're looking for the file you copied in a different container.

$ docker run --name my_container alpine
$ docker cp test.txt my_container:/tmp/test.txt
$ docker run alpine ls -l /tmp
total 0
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS               NAMES
6224c947fbbd        alpine              "ls -l /tmp"        11 seconds ago       Exited (0) 10 seconds ago                           brave_neumann
22951689a3e4        alpine              "/bin/sh"           About a minute ago   Exited (0) About a minute ago                       my_container

You copied the file into my_container, but you ran the ls -l /tmp command in brave_neumann.

If you want to copy files into a container and then use those files in the container, you either have to copy the files while the container is still running, or you copy them to a docker volume. Mounting that volume in a new container then lets it see the files you copied in.

$ docker volume create my_volume
my_volume
$ docker run --name my_container2 -v my_volume:/data alpine echo OK
OK
$ docker cp test.txt my_container2:/data/test.txt
$ docker run -v my_volume:/data alpine ls -l /data
total 4
-rw-r--r--    1 1006     1006            29 Oct  6 19:43 test.txt
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
15b36ccd535f        alpine              "ls -l /data"       7 seconds ago       Exited (0) 6 seconds ago                        agitated_khorana
b9d4c9e0902f        alpine              "echo OK"           54 seconds ago      Exited (0) 53 seconds ago                       my_container2
6224c947fbbd        alpine              "ls -l /tmp"        2 minutes ago       Exited (0) 2 minutes ago                        brave_neumann
22951689a3e4        alpine              "/bin/sh"           3 minutes ago       Exited (0) 3 minutes ago                        my_container
$ 
Sign up to request clarification or add additional context in comments.

Comments

1

In your second command you appear to be trying to copy test.txt into /usr/local with the name src. Try adding a / to the end of the destination path: docker cp test.txt my_docker:/usr/local/src/ to copy with the file name or: docker cp test.txt my_docker:/usr/local/src/newname.txt to rename it in the container.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.