Skip to content

Instantly share code, notes, and snippets.

@deepakputhraya
Last active October 4, 2019 13:17
Show Gist options
  • Save deepakputhraya/38da47b697e2c75a5ad43b631fe7059b to your computer and use it in GitHub Desktop.
Save deepakputhraya/38da47b697e2c75a5ad43b631fe7059b to your computer and use it in GitHub Desktop.
Docker Image for single server multi user Jupyterhub with R support, Google Auth & Postgresql Backend
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_CALLBACK_URL=
POSTGRES_USERNAME=
POSTGRES_PASSWORD=
POSTGRES_HOST=
POSTGRES_DATABASE=
GOOGLE_HOSTED_DOMAIN=example.com
SERVICE_NAME=ACME Inc

Jupyterhub

Docker image to setup Jupyterhub on a single server for

  • Multiple users
  • Support for R
  • Jupyterlab

Easy to setup and use for a small organisation.

Build

docker build .

Run with docker compose

version: '3'
services:
  jupyter:
    image: jupyterhub-image
    restart: always
    container_name: jupyterhub
    env_file:
      - .env
    ports:
      - "8080:8000"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "data:/home"

volumes:
  data:
    external:
      name: jupyterhub-data
#!/bin/bash
exec jupyterhub --ip 0.0.0.0
FROM jupyterhub/jupyterhub
RUN apt-get update && apt-get install -y gnupg2 software-properties-common
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
RUN add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/'
RUN apt-get update
RUN apt-get install -y curl \
r-base \
libopenblas-base \
apt-utils \
wget \
unzip \
libpq-dev \
python-dev \
build-essential \
postgresql-server-dev-all \
nano \
apt-transport-https \
vim \
nmap \
tree \
libcurl4-openssl-dev \
libxml2-dev
RUN pip install jupyter_contrib_nbextensions \
oauthenticator \
jupyterlab \
ipyparallel \
psycopg2 \
dockerspawner
RUN ipcluster nbextension enable
RUN Rscript -e "install.packages(c('repr', 'IRdisplay', 'IRkernel','tidyverse','DBI','doparallel','RPostgreSQL','googlesheets','foreach','reshape2','jsonlite','httpuv,'gmailr','tableHTML'), type = 'source')"
RUN Rscript -e "IRkernel::installspec(user = FALSE)"
COPY docker-entrypoint docker-entrypoint
COPY jupyterhub_config.py jupyterhub_config.py
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
RUN jupyter contrib nbextension install --sys-prefix
RUN jupyter labextension install @krassowski/jupyterlab_go_to_definition \
@ryantam626/jupyterlab_code_formatter \
@jupyterlab/github \
@jupyterlab/git \
@oriolmirosa/jupyterlab_materialdarker \
@jupyterlab/toc
RUN chmod +x docker-entrypoint
ENTRYPOINT ["./docker-entrypoint"]
EXPOSE 8000
import os
c = get_config()
# Allow CORS
c.NotebookApp.allow_origin = '*'
# Allows multiple single-server per user
c.JupyterHub.allow_named_servers = True
# Use Google OAuthenticator for local users
c.JupyterHub.authenticator_class = 'oauthenticator.LocalGoogleOAuthenticator'
c.LocalGoogleOAuthenticator.oauth_callback_url = os.environ['GOOGLE_CALLBACK_URL']
c.LocalGoogleOAuthenticator.client_id = os.environ['GOOGLE_CLIENT_ID']
c.LocalGoogleOAuthenticator.client_secret = os.environ['GOOGLE_CLIENT_SECRET']
c.LocalGoogleOAuthenticator.hosted_domain = [os.environ['GOOGLE_HOSTED_DOMAIN']]
c.LocalGoogleOAuthenticator.login_service = os.environ['SERVICE_NAME']
c.Authenticator.add_user_cmd = ['adduser', '-q', '--gecos', '""', '--disabled-password', '--force-badname']
# create system users that don't exist yet
c.LocalAuthenticator.create_system_users = True
# Shutdown idle notebooks after 1.5h of inactivity
c.MappingKernelManager.cull_idle_timeout = 5400
c.NotebookApp.shutdown_no_activity_timeout = 5400
# Use jupyterlab by default
c.Spawner.default_url = '/lab'
# Use Postgresql database
c.JupyterHub.db_url = 'postgresql://{}:{}@{}:5432/{}'.format(
os.environ['POSTGRES_USERNAME'], os.environ['POSTGRES_PASSWORD'], os.environ['POSTGRES_HOST'], os.environ['POSTGRES_DATABASE']
)
psycopg2==2.7.7
psycopg2-binary==2.7.7
watchdog==0.9.0
SQLAlchemy==1.3.3
alembic==1.0.10
python-json-logger==0.1.10
datetime==4.3
structlog==19.1.0
requests==2.22.0
tensorflow==1.13.1
numpy==1.16.4
scipy==1.1.0
Pillow==5.3.0
tqdm==4.28.0
python-dotenv==0.10.2
json-logging==0.0.13
scikit-image==0.14.2
pandas==0.25.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment