Navigation

Setting up Jupyterhub with a Jupyter-Notebook Image for Deep-Learning

Hi folks,

today I would like to show you how you can use a Jupyter Notebook with Tensorflow, Pytorch and Keras with GPU support and make it run with Jupyterhub running in a Docker container.

You need to have docker and nvidia-docker with GPU support up and running. You can look at our previous blog post here.

Most of the code is borrowed from this repo.

And the images used are adjusted from this organization and this repo which actually is a fork of  jupyter/docker-stacks.

However I adjusted everything to my needs and all the code is provided at our gitHub repo.

So ok let’s go.

I have decided to set up Jupterhub as a Docker Service and Spawn Jupyter Notebooks as Docker Containers from that instance. Many other methods are possible.

You can pull https://github.com/FAU-DLM/GPU-Jupyterhub if you like or build everything yourself according the Dockerfiles provided.

First you need to build the Jupyter Notebook image

from within

GPU_Jupyterhub/Jupyter_Image/base-notebook/

execute:

docker build -t base-notebook-gpu .

This image already provides GPU support with CUDA 9 and CUDNN7

now navigate to:

GPU_Jupyterhub/Jupyter_Image/tensorflow-notebook-gpu/

execute:

docker build -t hub-deep-learning-notebook-gpu .

If you want you can also pull the image from here –> digitalanatomist/hub-deep-learning-notebook-gpu.

Now let’s set up the Jupyterhub container.

But first we need to create the external volumes mentioned in the docker-compose.yml file

look here:

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

# JupyterHub docker-compose configuration file
version:                          "2"

services:
  hub:
    depends_on:
      - hub-db
    build:
      context:                    .
      dockerfile:                 Dockerfile.jupyterhub
      args:
        JUPYTERHUB_VERSION:       ${JUPYTERHUB_VERSION}
    restart:                      always
    image:                        jupyterhub
    container_name:               jupyterhub
    # not needed if nvidia-docker 2 is installed
    #devices:                     #this is required
    #  - /dev/nvidiactl
    #  - /dev/nvidia-uvm
    #  - /dev/nvidia0 #in general: /dev/nvidia# where # depends on which gpu card is wanted to be used
    volumes:
      # use a volume mapping into the container if you like
      - "/notebook_dir:${DOCKER_NOTEBOOK_DIR}"
      # Bind Docker socket on the host so we can connect to the daemon from
      # within the container
      - "/var/run/docker.sock:/var/run/docker.sock:rw"
      # Bind Docker volume on host for JupyterHub database and cookie secrets
      - "data:${DATA_VOLUME_CONTAINER}"
      # not needed if nvidia-docker 2 is installed
      #- "nvidia_driver_387.34:/usr/local/nvidia:ro"
    ports:
      - "443:443"
    links:
      - hub-db
    environment:
      # this is just to show how to use with self signed keys use which ever you like
      #SSL_KEY:                   "./ssl/privkey.pem"
      #SSL_CERT:                  "./ssl/cert.pem"
      # All containers will join this network
      DOCKER_NETWORK_NAME:        ${DOCKER_NETWORK_NAME}
      # JupyterHub will spawn this Notebook image for users
      DOCKER_NOTEBOOK_IMAGE:      ${LOCAL_NOTEBOOK_IMAGE}
      # Notebook directory inside user image
      DOCKER_NOTEBOOK_DIR:        ${DOCKER_NOTEBOOK_DIR}
      # Using this run command (optional)
      DOCKER_SPAWN_CMD:           ${DOCKER_SPAWN_CMD}
      CONFIGPROXY_AUTH_TOKEN:     ${CONFIGPROXY_AUTH_TOKEN}
      ## needed for nvidia-docker version 2
      NVIDIA_VISIBLE_DEVICES:     all
      NVIDIA_DRIVER_CAPABILITIES: compute,utility
      # Postgres db info
      POSTGRES_DB:                ${POSTGRES_DB}
      POSTGRES_HOST:              hub-db
      POSTGRES_PASSWORD:          ${POSTGRES_PASSWORD}
    command:                      >
      jupyterhub -f /srv/jupyterhub/jupyterhub_config.py
      
  # postgres database
  hub-db:
    image:                        postgres:9.5
    container_name:               jupyterhub-db
    restart:                      always
    environment:
      POSTGRES_DB:                ${POSTGRES_DB}
      PGDATA:                     ${DB_VOLUME_CONTAINER}
      POSTGRES_PASSWORD:          ${POSTGRES_PASSWORD}
    # I have the postgres related settings stored in the .env file but you could store them here or in a docker volume using dockersecrets
    #env_file:
      #- secrets/postgres.env
    volumes:
      - "db:${DB_VOLUME_CONTAINER}"

  #PHP-Adminer
  adminer:
    links:
      - hub-db
    image:                        clue/adminer:latest
    ports:
      - 7000:80


networks:
  default:
    external:
      name:                       ${DOCKER_NETWORK_NAME}


volumes:
  data:
    external:
      name:                       ${DATA_VOLUME_HOST} ## need to be created first

  db:
    external:
      name:                       ${DB_VOLUME_HOST} ## need to be created first

  # not needed if nvidia-docker 2 is installed
  #nvidia_driver_387.34:
  #  external:                    true

After this you can start your Jupyterhub Container with the following command:

docker-compose up --build

You should have a look into the .yml file because you definitely do not need to set everything up as shown inside.

Especially the database and adminer services are not needed for a simple set up.

I have tried to comment on most of the lines as much as possible.

Also look into the jupyterhub_config.py and .env file.

Most of the lines are explained.

If you have trouble setting things up you can drop me a message.

Code as always in the repo.