Skip to content

Instantly share code, notes, and snippets.

@sampollard
Last active March 15, 2019 20:05
Show Gist options
  • Save sampollard/5dbd8444232960194e93deae1fcd1bed to your computer and use it in GitHub Desktop.
Save sampollard/5dbd8444232960194e93deae1fcd1bed to your computer and use it in GitHub Desktop.
Singularity: Did You Bring Enough to Share with the Class?

Singularity: Did You Bring Enough to Share with the Class?

Motivation

I wanted to use singularity for my own selfish purposes; when presenting my research I want it to be easy for someone else to try.

Installing Singularity

The Easy Way

sudo apt install singularity.

The Hard Way

I wanted a module. You need the following packages:

build-essential libssl-dev uuid-dev libgpgme11-dev libseccomp-dev pkg-config squashfs-tools
golang-1.10

(I only tried on Ubuntu)

Then you download the source in just the right place:

export GOPATH=/where/I/keep/source
PREFIX=/packages/singularity/3.1.0

mkdir -p ${GOPATH}/src/github.com/sylabs && \
  cd ${GOPATH}/src/github.com/sylabs && \
  git clone https://github.com/sylabs/singularity.git && \
  cd singularity
git checkout v3.1.0
./mconfig -p $PREFIX && \
  cd ./builddir && \
  make && \
  sudo make install

Then, I made a module file like so:

#%Module########################################
## Singularity
set _module_name  [module-info name]
module-whatis   "Singularity version 3.1.0 built using GO language version 1.10.1 linux/amd64"
set     SWTOP       /usr/local/packages/singularity/3.1.0
prepend-path    PATH    $SWTOP/bin

Creating a Singularity Definition File

sudo singularity build epg.sif Singularity

Watch it fail, then try again and again.

But I Have an Expensive Compiler I Don't Want Someone Else to Pay For

You can bind that directory so when creating the singularity image, it can be seen:

sudo singularity build --bind /opt/local/bin:/packages/something/bin ...

will mount /packages/... into /opt/local/bin into the container.

  • /home/$USER, /tmp, and $(pwd) are bound by default.

Using Singularity

singularity shell epg.sif
singularity exec epg.sif

How to Fake Writeable Images

Singularity images are immutable. This is nice for reproducibility, but what if I made a mistake on the image and you want to change it a bit? Well hopefully you can just edit the definition file, but if not you must create an overlay.

mkdir overlay
sudo singularity shell --overlay overlay/ ubuntu.sif
<do something>
# Then you need to use the --overlay flag every time

Some Gotchas

  1. Each section starts you in a different directory!
    • %post - starts in /
    • %runscript - starts in your current working directory
    • %environment - ends up setting stuff in /.singularity.d/env/ or something? I'm not sure, but one thing I am sure of is you can't "source" things (e.g. . script.sh doesn't work)
    • %test - Starts in current working directory
  2. You're running sh not bash. As such, you can't do export VAR=val. You have to do
VAR1=val1
VAR2=val2
export VAR1 VAR2
  1. You can see some stuff from your local filesystem. Namely, /tmp, $HOME, /proc , /sys, /dev, and $(pwd).
    • But you can't write to any of them while inside the container! Which is nice, that's what containers are for. You can write but everything gets lots when you exit.
    • Note /usr/local/bin isn't included in this list.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment