Skip to content

Singularity: III. Advanced skills

In this tutorial, we will learn how to create containers from a recipe file, called Singularity (which is equivalent to Dockerfile)

Cache setting

By default, Singularity uses a temporary directory to save Docker files as tarballs:

1
2
3
4
5
6
7
8
$ ls ~/.singularity
cache/  docker/  metadata/
$ ls .singularity/docker/
sha256:0efe07335a049e6afcd757db2d17ba37a12b717eb807acb03ddf3cd756b9fc2a.tar.gz
sha256:43a98c1873995475a895f3d79f405232ef5230076b3f610c949c2e8341743af7.tar.gz
sha256:6b1bb01b3a3b72463ae8ac5666d57b28f1a21d5256271910ac8df841aa04ecd1.tar.gz
sha256:c2ca09a1934b951505ecc4d6b2e4ab7f9bf27bcdfb8999d0181deca74daf7683.tar.gz
sha256:d6c3619d2153ffdefa4a9c19f15c5d566ce271b397a84537baa9ee45b24178f2.tar.gz

You can change these by theses cache directories by specifying the location on your localhost as following:

1
2
3
$ mkdir -p $SCRATCH/singularity_tmp
$ mkdir -p $SCRATCH/singularity_scratch
$ SINGULARITY_TMPDIR=$SCRATCH/singularity_scratch SINGULARITY_CACHEDIR=$SCRATCH/singularity_tmp singularity --debug pull --name ubuntu-tmpdir.sif docker://ubuntu

Building containers

Creating writable containers with --sandbox options

If you want to build a container within a writable directory (called a sandbox), you can do that with --sandbox option. We’ve already covered how you can pull an existing container from Docker Hub, but we can also build a Singularity container from docker using the build command:

1
$ singularity build --sandbox ubuntu-latest/  docker://ubuntu

With --sandbox option, you can changes and write files within the directory, but those changes will not remain when you finished using the container. To make those changes persistent, use --writable option when you start a container.

1
$ singularity shell --writable ubuntu-latest/

Building containers from Singularity definition files

To build containers, Singularity uses a file Singularity which is equivalent to Dockerfile in Docker. You can use different name but it is better practice to put it in a director and name it Singularity because it will be helpful later on when developing on repositories.

For detailed information on writing Singularity recipe files, please refer to the container definition docs.

To create a container using a custom Singularity file, you use build command:

1
$ singularity build ubuntu-latest.sif Singularity

You would get an error message because you do not have a Singularity file yet.

Let's assume that you already have the following Singularity definition file, called "lowcow.def" and you want ot use it to build a SIF container.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Bootstrap: docker
From: ubuntu:16.04

%post
    apt-get -y update
    apt-get -y install fortune cowsay lolcat

%environment
    export LC_ALL=C
    export PATH=/usr/games:$PATH

%runscript
    fortune | cowsay | lolcat