it4i logoDocumentation
SoftwareTools

Apptainer

Apptainer (formerly known as Singularity) is a containerization tool designed for High-Performance Computing (HPC). Unlike Docker, it allows running containers without requiring root privileges, making it ideal for secure environments like supercomputers and university clusters.

Official Resources

Video Tutorials

Basic Usage

If you have issues with using/creating an Apptainer image, unload the XALT module from your environment. To clear the entire environment, run: ml -f purge. To unload only the XALT module, use: ml -f -XALT.

Running a Container

apptainer run docker://ubuntu

This will pull an Ubuntu image from DockerHub and run it.

Shell Access

apptainer shell docker://ubuntu

Opens an interactive shell inside the container.

Execute a Command

apptainer exec docker://ubuntu echo "Hello from Apptainer!"

Building and Editing Images

Creating an Apptainer Definition File

Example my_container.def file:

Bootstrap: docker
From: ubuntu:20.04

%post
    apt update && apt install -y python3

%runscript
    echo "This is my custom Apptainer container!"

Building an Image

apptainer build my_container.sif my_container.def

Editing an Image

Apptainer images (.sif format) are immutable, so you need to create a writable sandbox:

apptainer build --sandbox my_container/ my_container.def

Modify it:

apptainer shell --writable my_container/

Rebuild it into a .sif file:

apptainer build my_container_final.sif my_container/

Using MPI in Apptainer

Running MPI Inside a Container

Your definition file should install MPI:

Bootstrap: docker
From: ubuntu:20.04

%post
    apt update && apt install -y mpich

Build and run with MPI:

mpirun -np 4 apptainer exec my_mpi_container.sif mpi_hello_world

Running MPI From Host Into Container

Ensure your host has an MPI runtime and mount it into the container:

mpirun -np 4 apptainer exec --bind /usr/lib/x86_64-linux-gnu/openmpi/ my_mpi_container.sif mpi_hello_world

GPU Support

Installing CUDA Inside a Container

For CUDA support, install the necessary drivers inside the image:

Bootstrap: docker
From: nvidia/cuda:12.0-runtime-ubuntu20.04

Run with GPU:

apptainer exec --nv my_cuda_container.sif nvidia-smi

Tips and Tricks

  • Use --fakeroot to build without sudo:

    apptainer build --fakeroot my_container.sif my_container.def
  • Use --bind /host/path:/container/path to access host directories.

  • Convert Docker images to Apptainer format for offline use:

    apptainer pull ubuntu.sif docker://ubuntu
  • Debug with increased verbosity:

    SINGULARITY_DEBUG=1 apptainer run my_container.sif

Conclusion

Apptainer is a powerful tool for running containers in HPC environments. With proper setup, it can efficiently run MPI workloads, GPU-accelerated applications, and custom software stacks while maintaining security and performance.

© 2025 IT4Innovations – All rights reserved.

On this page