18

I'm trying to copy some files from my docker container to my localhost, I read the documentation that the way to do this is

docker cp 'container':path/to/file dest/path 

But this requires I know the path and directory within the container that I want to get to, how can I view the container's directory? I tried docker diff and docker inspect but these don't show me the container's file directory

Meir Snyder
  • 789
  • 1
  • 6
  • 16

2 Answers2

34

You'll first need to know the name of the instance running?

$ docker ps

CONTAINER ID  IMAGE        COMMAND  CREATED  STATUS  PORTS  NAMES
36029...      image/image  ...      1 sec..  Up..    ...    some-container

Now, get inside the container and look for what you need. Assuming the container name is, some-image.

$ docker exec -it some-container /bin/bash

root@1f3420c939:/var/www/html#

If you already know the folder:

docker exec -it some-container ls /path/to/file

EDIT:

as noted by @Konrad Botor it's possible to use also the container id instead of the container name and more importantly not all images have bash installed (alpine is the most popular image not using it).

Here's an example with alpine:

docker run -d \
    --rm \
    --name my_alpine_container \
    alpine \
    sleep 3600

CONTAINER_ID=$( docker ps -q my_alpine_container )

# to "enter" the container:
docker exec -it $CONTAINER_ID sh
Stefano
  • 4,730
  • 1
  • 20
  • 28
  • what do you mean by $instance_name are you refferring to the image name, containerID or what's listed under NAMES when using docker ps? – Meir Snyder Oct 31 '17 at 18:30
  • ok, is the instance running? If yes and you need to copy the file in the instance you use the last column. EDIT: what's listed under NAMES. – Stefano Oct 31 '17 at 18:32
  • 1
    Container ID should work as well its name. Also not every image has Bash installed. For example, most of the Alpine based images do not. – Konrad Botor Oct 24 '20 at 12:49
  • If you have alpine image which does not have bash, you can use the second command mentioned above - `docker exec -it container_name ls` . Means that whatever you intend to run inside bash run it with docker container commands(as bash is not available inside the container). – Abhishek Shah Jan 21 '21 at 06:37
  • alpine is using /bin/ash. So just enter "docker exec -it ash" will do. – Peter Teoh May 13 '22 at 00:55
-7

Try this out.

Copying files from host to Docker container

"docker ps" to find the container's directory.

Aishwarya
  • 110
  • 6