docker view directory within container

Meir Snyder picture Meir Snyder · Oct 31, 2017 · Viewed 29.7k times · Source

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

Answer

Stefano picture Stefano · Oct 31, 2017

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