Skip to content

Singularity & Apptainer

Singularity/Apptainer is an open source container platform. It is similar to docker but favors integration over isolation e.g. in Apptainer the system's user ids are not isolated from the containers user ids (by default). The container instance runs with the users privileges - in addition the home directory is also automatically mounted into the container instance.

Apptainer was built with HPC systems in mind and comes with out of the box support for graphic accelerators (GPUs) and high-performance interconnects such as e.g. InfiniBand and Omni-Path.

In 2021 the open source project Singularity was renamed to Apptainer.

Info

Unless you have a specific use case you should prefer the more recent apptainer modules instead of the singularity modules. The apptainer package still supports using the singularity commands as well as the SINGULARITY_ environment variable.s

Basic commands

singularity pull
singularity exec
singularity shell
singularity run
singularity build

Basic examples

Loading the Apptainer module on VSC-4/VSC-5:

# VSC-4
(skylake) $ module load apptainer/1.1.9-gcc-12.2.0-vflkcfi

# VSC-5
(zen) $ module load apptainer/1.1.6-gcc-12.2.0-xxfuqni

Execute a command in a container

To execute a command inside a container the exec command can be used:

(zen) $ apptainer exec centos_latest.sif cat /etc/redhat-release

Convert a Docker container

To get started you can convert a docker container into the singularity format:

zen $ apptainer build python_latest.sif docker://python:latest

Running an application using Python from the container

First create a simple example python script.

$ cat hello.py
import sys
print("Hello World: The Python version is %s.%s.%s" % sys.version_info[:3])

If you run the example outside of the container you will see the system's default python version:

zen $ python3 hello.py
Hello World: The Python version is 3.6.8

Running the script inside the container using exex will give the following result:

zen $ apptainer exec python_latest.sif python3 ./hello.py
Hello World: The Python version is 3.13.0

Getting a Shell running in the container

zen $ apptainer shell python_latest.sif
Apptainer> cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

Run the default command

When using the run command apptainer will run the default command of the container. For our example python container this is the python executable.

zen $ apptainer run python_latest.sif
Python 3.13.0 (main, Nov 12 2024, 06:05:34) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Basic job script examples on VSC

VSC-4:

#!/bin/bash
#
#SBATCH -J myjob
#SBATCH -o output.%j
#SBATCH -p skylake_0096
#SBATCH -N 1

module load apptainer/1.1.9-gcc-12.2.0-vflkcfi
apptainer exec python_latest.sif python ./hello.py

VSC-5:

#!/bin/bash
#
#SBATCH -J myjob
#SBATCH -o output.%j
#SBATCH -p zen3_0512
#SBATCH -N 1

module load apptainer/1.1.6-gcc-12.2.0-xxfuqni
apptainer exec python_latest.sif python ./hello.py