Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker view directory within container

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

like image 712
Meir Snyder Avatar asked Oct 31 '17 18:10

Meir Snyder


People also ask

How do I see the directory in docker?

to find the root directory of docker. You will find the docker directory will be given in this line: "Docker Root Dir: /var/lib/docker". The docker images, they are stored inside the docker directory: /var/lib/docker/ images are stored there.

How do I view the contents of a docker image?

To list the detailed content of an image you have to run docker run --rm image/name ls -alR where --rm means remove as soon as exits form a container. Show activity on this post. This doesn't show the contents; it only shows the layers, etc., that went into building the image.


1 Answers

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
like image 112
Stefano Avatar answered Sep 29 '22 08:09

Stefano