Kahibaro
Discord Login Register

Introduction to Singularity and Apptainer

What Are Singularity and Apptainer?

Singularity and Apptainer are container technologies designed specifically for HPC and scientific computing. They provide:

Historically:

In practice, you’ll often see:

When using a real system, always check:

Why Containers in HPC (Specifically Singularity/Apptainer)?

Compared to general container tools (like Docker), Singularity/Apptainer are designed around HPC constraints:

This makes it practical to:

Basic Concepts and Terminology

Typical command structure:

In examples below, replace singularity with apptainer if that’s what your system provides.

Running Containers on HPC Systems

Running a container in HPC usually happens inside a batch job, but the core usage is the same on a login node (for quick testing).

Executing a Command in a Container

Use exec to run a single command inside a container image:

bash
singularity exec myimage.sif python script.py

Key aspects:

You can also run interactive shells:

bash
singularity shell myimage.sif

This drops you into a shell inside the container environment, useful for exploration and debugging.

Entry Points and `run`

If the container defines a “run script” (an entry point), you can use:

bash
singularity run myimage.sif

This will execute the container’s default command (for example, launch an application script). The behavior is defined when the image is built.

Getting Images: Pulling from Registries

Singularity/Apptainer can directly use images from common registries (e.g. Docker Hub, Sylabs Cloud, GitLab registries).

Typical example (from Docker Hub):

bash
singularity pull docker://python:3.11

This:

  1. Downloads the Docker image.
  2. Converts it into a Singularity Image Format (SIF) file, e.g. python_3.11.sif.

You can then run:

bash
singularity exec python_3.11.sif python --version

Other URI schemes you may see:

On many HPC systems, building or pulling large images is discouraged on login nodes; some centers provide prebuilt images or separate build nodes. Always check site policies.

Creating Custom Images (Overview)

Detailed build instructions typically require admin or root access (especially on personal machines), but the basic workflow is:

  1. Write a definition file, e.g. myenv.def:
text
   Bootstrap: docker
   From: ubuntu:22.04
   %post
       apt-get update && apt-get install -y \
           python3 python3-pip
       pip3 install numpy
   %environment
       export OMP_NUM_THREADS=1
   %runscript
       python3 "$@"
  1. Build the image (usually on a machine where you have appropriate permissions):
bash
   sudo singularity build myenv.sif myenv.def
  1. Transfer the .sif file to the HPC system and use it:
bash
   singularity exec myenv.sif python3 -c "import numpy; print(numpy.__version__)"

On shared HPC systems, direct root-based builds are normally not allowed. Common patterns include:

The details of building and distributing images are cluster-specific and are usually documented by the HPC center.

Bind Mounts and Accessing Data

For HPC work, data almost always lives on host filesystems (parallel filesystems, project shares, scratch). You typically do not bake large datasets into container images.

Singularity/Apptainer provides bind mounts so the container can see host paths:

bash
singularity exec \
  --bind /project/mydata:/data \
  myenv.sif \
  python script.py --input /data/input.dat

Important points:

On many clusters, some common paths ($HOME, project directories, scratch) are automatically bound; check your site’s configuration.

Using Containers with SLURM (High-Level View)

You can combine containers with SLURM job scripts. A minimal example:

bash
#!/bin/bash
#SBATCH --job-name=container_test
#SBATCH --time=00:10:00
#SBATCH --nodes=1
#SBATCH --ntasks=1
module load singularity
singularity exec myenv.sif python script.py

Key ideas for HPC integration:

bash
  singularity exec --nv mygpuimage.sif ./my_gpu_program

Use the pattern recommended by your HPC documentation for MPI and GPU usage with Singularity/Apptainer.

Singularity vs. Apptainer: Practical Differences

From a beginner’s point of view, key differences are mainly names and versions:

In practice:

Benefits for Reproducibility

In the broader context of reproducible workflows, Singularity/Apptainer help you:

Combining containers with good documentation and version control (for your code and definition files) makes it significantly easier to reproduce results months or years later, or on another HPC system.

Common Usage Patterns for Beginners

Typical early use cases include:

As you gain experience, you can gradually move from:

Views: 10

Comments

Please login to add a comment.

Don't have an account? Register now!