Jupyter Notebook containerized with Singularity

By Staff

Oct 2, 2018 | Blog, How To Guides

Jupyter Notebook is an open source web application environment used to create and share documents containing code, equations, visualizations and narrative text. Use cases include data visualization, statistical modeling and machine learning.

Singularity provides a basis for repeatability with a container runtime that does not require root owned daemon or allow escalation of user privilege when ran. With it’s single file image format (SIF) Singularity is ideal for BYOE science (bring your own environment) and for the mobility of compute with deep learning applications.

We’ll use the following Singularity definition file when building Jupyter:

Bootstrap: library
From: debian:9

%help
    Container with Anaconda (Conda 4.5.11) and Jupyter Notebook 5.6.0 for Debian 9.x (Stretch).
    This installation is based on Python 2.7.15

%setup
    #Create the .condarc file where the environments/channels from conda are specified, 
    #these are pulled with preference to root
    cd /
    touch .condarc

%post
    #Installing all dependencies
    apt-get update && apt-get -y upgrade
    apt-get -y install \
      build-essential \
      wget \
      bzip2 \
      ca-certificates \
      libglib2.0-0 \
      libxext6 \
      libsm6 \
      libxrender1 \
      git

   rm -rf /var/lib/apt/lists/*
   apt-get clean

  #Installing Anaconda 2 and Conda 4.5.11
  wget -c https://repo.continuum.io/archive/Anaconda2-5.3.0-Linux-x86_64.sh
  /bin/bash Anaconda2-5.3.0-Linux-x86_64.sh -bfp /usr/local

  #Conda configuration of channels from .condarc file
  conda config --file /.condarc --add channels defaults
  conda config --file /.condarc --add channels conda-forge
  conda update conda

  #List installed environments
  conda list

%runscript
  jupyter notebook --allow-root "$@"

To build the image run the following command:

$ sudo singularity build jupyter.sif jupyter.def

The definition file above has a %post section in which all the dependencies are installed at build time. After that, you can start the container and, it will listen on localhost:8888 by default.

$ singularity run jupyter.sif

If you would like to run it on another port (e.g. 9000) instead you can do so by:

$ singularity run jupyter.sif --port=9000

This will run Jupyter Notebook on localhost:9000.

And if you would like to set another hostname use ––ip when running the image:

$ singularity run jupyter.sif --ip=1.1.1.1 --port=9000

This will run Jupyter Notebook on 1.1.1.1:9000

For the files used in this example, see our GitHub repository at:
https://github.com/sylabs/examples/tree/master/machinelearning/jupyter-notebook

Join Our Mailing List

Related Posts