Skip to content

Instantly share code, notes, and snippets.

@mattpitkin
Last active March 15, 2023 10:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpitkin/35ac19214048e96c391e948d7ec34ca5 to your computer and use it in GitHub Desktop.
Save mattpitkin/35ac19214048e96c391e948d7ec34ca5 to your computer and use it in GitHub Desktop.
Singularity & Docker in jupyter

Use Singularity and Docker to run a kernel in a jupyter notebook

This is an extension to this post about creating a kernel in a Jupyter notebook that runs a Singularity container.

Download Singularity (see here).

Create a Singularity file, e.g., (making sure to install the ipykernel module in it):

Bootstrap: docker
From: ubuntu:xenial

%runscript
    echo "Hello... I am a new Singularity container"

%labels
    AUTHOR pitkin@gmail.com

%post
    apt-get update && apt-get install -y python-pip python-dev build-essential
    pip install --upgrade pip
    pip install --trusted-host pypi.python.org numpy
    pip install --trusted-host pypi.python.org ipykernel

Build the image with:

sudo singularity build --writable test.simg Singularity

where the --writable command is important.

Create jupyter notebook kernel using the image. To do this, in the ${HOME}/.local/share/jupyter/kernels create a new directory, e.g. myimage, and add a kernel.json file containing:

{
 "language": "python",
 "argv": ["/usr/bin/singularity",
   "exec",
   "-w",
   "/home/matthew/singularity/test.simg",
   "/usr/bin/python",
   "-m",
   "ipykernel",
   "-f",
   "{connection_file}"
 ],
 "display_name": "Python 2 (Singularity)"
}

where adding the -w option is important. Then start a jupyter notebook with

jupyter notebook &

and there should be a usable Python 2 (Singularity) kernel option!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment