Skip to content

Keeping a persisted directory of data from a Docker container using volumes

Louis Maddox edited this page Sep 14, 2020 · 13 revisions

If a Docker container is run, the files modified during its session will not be persisted after the Docker process is shut down.

If you want to keep the changes made to its container file system locally, it can be desirable to use the host file system as 'storage' (i.e. to 'persist' the changes 'locally')

docker ps will list the processes of running docker containers with their CONTAINER_ID

This Q&A describes ways to "explore a docker container's file system"

  • Snapshotting
  • ssh
  • nsenter

More simply, since docker exec was introduced, you can docker exec <container> ls <dir path> (which seems preferable to creating a static image)

Much like virtual machines, you can mount the host's directories in a container and these will persist unlike the rest of the container's file system.

In my case I am spinning up the image coatwork/dockerimage:latest for the 2020 Combinatorial Optimisation summer school

docker run --rm -it -p 9001:9001 coatwork/dockerimage:latest

and this creates a persisted directory:

docker run --rm -it -v /home/louis/co/testing/files/:/jupyter/persist -p 9001:9001 coatwork/dockerimage:latest

I knew the working directory was /jupyter/ because I looked at the Dockerfile on Docker Hub (here) and read the line:

WORKDIR /jupyter

which is run before the Jupyter server is launched (see the sections on WORKDIR in Docker's documentation and note on best practices)

So passing /jupyter/persist as the volume location means it will be created as a directory alongside the notebook directories.

You can check the permissions for the directory with ls -ld and then convert these into the numeric code at chmod-calculator.com

sudo docker exec silly_johnson ls -ld "/jupyter/persist"

gives the permissions code for the directory ("silly_johnson" is the name of the active container found by sudo docker ps)

  • Note that if jupyter/ is used as the volume location, not jupyter/persist (which was a location I invented for this purpose), then the container will mirror that empty directory and no notebooks appear there
    • this might have happened due to file permission mismatch?

Clone this wiki locally