Singularity Containers

From IBERS Bioinformatics and HPC Wiki
Jump to: navigation, search

What is Singularity

Singularity [1] is a container system that doesn't need root access. Containers are a set of applications and a minimal operating system bundled together in a single file. They are a good way to ensure that anyone running the software is using the exact same set of libraries and dependencies. This is helpful to reproducible science where we want somebody else to be able to recreate our results. They also allow us to run newer*/different operating systems than what is installed on the HPC.

Loading singularity

Singularity has now been renamed apptainer, but the singularity program is still available if you load the apptainer module.

Run the command (or add it to your submission script):

   module load apptainer 

Obtaining Containers

Singularity Hub [2] contains a number of premade containers which you can download and use. To download these run:

   singularity pull shub://<username>/<imagename>:<tag>  


   singularity pull shub://SupercomputingWales/singularity_hub:base_image 

This will then download the file to <username>-singularity_hub-master-<image name>.simg

Running a shell in a container

The singularity shell command will run a shell inside the container. You can then execute any commands from software installed in the container. To do this run the command "singularity shell".

   singularity shell <image name>


   singularity shell SupercomputingWales-singularity_hub-master-base_image.simg 

Running the container's default actions

Most containers specify a default command which they will run. The "singularity run" command will execute this.

   singularity run <image name>

Accessing the host file system from inside the container

   singularity shell -B /ibers/ernie/home:/home <imagename>

This will mount the directory /ibers/ernie/home from the host under /home in the container. Your own home directory will be under /home/<userid>. Note that accessing the ~ or ~<userid> directories won't work.

Writing your own containers

To make your own containers you'll probably have to install singularity on your own computer, as building a container requires root access.

This example takes an ubuntu 16.04 image as a base, then installs the program cowsay. This is done when the container is built. When the container is run it executes cowsay with the arguments given on the command line.

   bootstrap: docker
       Example container for Cowsay
       #configure our locale, without this we'll get locale errors
       export LC_ALL=C
       #cowsay installs to /usr/games, but this isn't in the path by default
       export PATH=/usr/games:$PATH
       apt-get update
       apt-get -y install cowsay
       cowsay $@

To build the container save the above example in a file called Singularity and run:

    sudo singularity build cowsay.simg Singularity

This will create an image file called cowsay.simg containing all the required software to run cowsay in an ubuntu 16.04 operating system.

Publishing a Container

  • Publish the Singularity file on Github.
  • Create an account on Singularity Hub.
  • Add this repository to Singularity Hub and it will be automatically built by singularity hub and made available for download using the singularity pull command.

More Information