Docker: Persisting User Data

Cloud

I’m busy putting together a Docker image for a multi-user Jupyter Notebook installation. I am to have an independent login for each of the users and each of them should also have their own storage space. That space should exist elsewhere from on the container though, so that even if the container stops, the data lives on. This should mitigate user rage.

There are number of options for achieving this but I’m simply going to persist the data onto the Docker host.

Adding a Volume

On the host machine you could create a new folder to hold the user content. However, it makes sense to just use /home/. Start your docker container with the -v option which binds a location on the host to a location on the container.

docker run -d -p 80:8000 -v /home/:/home jupyter:latest

Adding Users

Now make a shell connection to the container.

docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
d6a5ca03a212        jupyter:latest      "jupyterhub"        2 minutes ago       Up 2 minutes        0.0.0.0:80->8000/tcp   frosty_ride
docker exec -t -i d6a5ca03a212 /bin/bash

We’ll be using the mkpasswd command to generate encrypted passwords, so we need to install the whois package.

apt-get update && apt-get install -y whois

Now you can create users.

useradd jupyter -m -p `mkpasswd jupyter` -s /bin/bash

It’s critical that the argument to the -p flag is generated by mkpasswd, otherwise logging in as this user won’t work!

That will create a jupyter folder under /home/ on the container.

ls /home
jupyter

It will also add an entry to /etc/passwd.

grep "jupyter" /etc/passwd
jupyter:x:1000:1000::/home/jupyter:/bin/bash

If we now exit the shell connection and check the /home/ folder on the host we’ll find that it is where the home folder for the jupyter user is located.

ls /docker-home/
jupyter

Finish and klaar.

Categorically Variable