Low Orbit Flux Logo 2 F

Docker How To Access Container Files

There are multiple different methods to access Docker containers files. We’re going to cover a bunch of these.

Direct Access to Docker Container Files

We can acess a containers files directly by looking inside it’s MergedDir. You can find the location of this direcotry by insepcting the container.




docker inspect --format='{{ .GraphDriver.Data.MergedDir }}' nervous_meitner


The container location will look like this.



ls /var/lib/docker/overlay2/63ec1a08b063c0226141a9071b5df7958880aae6be5dc9870a279a13ff7134ab/merged

Using docker cp Command

Copy file from container to current dir:



docker cp nervous_meitner:/junk.txt .

Copy file from current dir to a dir on a container:



docker cp test1.txt nervous_meitner:/data/

Specify a new destination file name:



docker cp test1.txt nervous_meitner:/data/new_test.txt 

Directly Connect

You also have the option to directly connect to a container and access it’s files from within.

You can exect a bash shell on the container like this ( use “exit” to logout ):



docker exec -it container1 bash

You can also attach to a container like this:



docker attach container1

Disconnect/unattach with the folowing combo, in order:



[ctrl] - p
[ctrl] - q

Log Files

If you just want the logs you can use these commands:



docker logs nervous_meitner              # view the logs
docker logs -f nervous_meitner           #  continously follow the logs and watch any updates
docker logs -f -n 100 nervous_meitner    # specify number of lines
docker logs -f -n 100 nervous_meitner    # follow and specify number of lines
docker logs -f --until=30s               # follow but only for 30 seconds

Volumes

Volumes will normally be kept within this directory:



/var/lib/docker/volumes/

For example, a volume called “vol1” might be located here:



/var/lib/docker/volumes/vol1/_data

You can inspect a volume to see where it is mounted like this:



docker volume inspect test1

This should show you the mount point.