Use Apptainer

Apptainer can be used as a container runtime environment on Charmed HPC for running containerized workloads. This guide provides examples of using Apptainer on Charmed HPC to accomplish different tasks.

New to Apptainer?

If you’re unfamiliar with using Apptainer in your workloads, see the Apptainer user quick start guide for a high-level introduction to using Apptainer on HPC clusters.

Prerequisites

To successfully use Apptainer on your Charmed HPC cluster, you will at least need:

Once you have verified that Apptainer is integrated with your Charmed HPC, refer to the sections below for the different ways that you can use Apptainer on Charmed HPC.

Create a container image

Apptainer can create container images on your cluster that can then be used within your workloads. The sections below demonstrate the different ways Apptainer can create container images on your Charmed HPC cluster.

Using a pre-existing container image from a public container registry

Apptainer can pull pre-existing container images from public container registries. For example, to pull a Valkey container image from Dockerhub and start a local Valkey service on your cluster:

user@login:~$
apptainer pull valkey.sif docker://ubuntu/valkey:7.2.10-24.04_stable
user@login:~$
apptainer overlay create --size 1024 valkey.img
user@login:~$
apptainer instance run --overlay valkey.img valkey.sif valkey
INFO:    instance started successfully
user@login:~$
apptainer exec instance://valkey valkey-cli ping
PONG

Use apptainer help pull to see the full list of public container registries Apptainer can pull container images from.

Building your own custom container image

Before attempting to build your own container images

Check with your cluster administrator before attempting to build container images on your Charmed HPC cluster. Each site has different policies about whether you can build container images directly on your cluster, and some disallow building container images on specific cluster resources such as login nodes.

Apptainer can build container images using instructions from a container definition file. For example, to build an Ubuntu 24.04 LTS-based container image with the gfortran compiler pre-installed, create the container definition file fortran-runtime.def:

fortran-runtime.def
bootstrap: docker
from: ubuntu:24.04

%post
    apt-get -y update
    apt-get -y install gfortran

%test
    gfortran --version
    exit 0

Now use apptainer build to build the container image:

user@login:~$
apptainer build fortran-runtime.sif fortran-runtime.def

The built container image can now be used to compile and run Fortran workloads. For example, create a simple Fortran program the prints “Hello world!” in the file hello.f90:

hello.f90
PROGRAM hello_world
  IMPLICIT NONE
  PRINT *, "Hello world!"
END PROGRAM hello_world

Now use the built container to compile and run your Fortran program:

user@login:~$
apptainer exec fortran-runtime.sif gfortran --output hello hello.f90
user@login:~$
apptainer exec fortran-runtime.sif ./hello
Hello world!

Warning

Check with your cluster administrator before attempting to build container images on your Charmed HPC cluster. Each site has different policies about whether you can build container images directly on your cluster, and some disallow building container images on specific cluster resources such as login nodes.

Provide your workload’s runtime environment

Apptainer can provide the runtime environment for your workloads. The sections below demonstrate the different ways Apptainer can provide your workload’s runtime environment.

Using the apptainer command directly in your workload

The apptainer command can be called directly in scripts to perform operations inside a container instance. First, declare in your batch script the partition you want your workload to run within and call the apptainer command from directly within your script. For example, to select compute as the partition your workload will run within, and run some Python code using a containerized Python 3.13 interpreter, create the batch script job.batch:

job.batch
#!/usr/bin/env bash
#SBATCH --partition compute
#SBATCH --output job.out

apptainer pull python-3.13.sif docker://ubuntu/python:3.13-25.04
apptainer --silent exec python-3.13.sif \
  python3 -c 'import sys; print(f"Hello from Python {sys.version}!")'

Now submit the job.batch script to Slurm with sbatch:

user@login:~$
sbatch job.batch
Submitted batch job 1

Use cat to view the results of your workload after it completes:

user@login:~$
cat job.out
Hello from Python 3.13.3 (main, Aug 14 2025, 11:53:40) [GCC 14.2.0]!

Using the --container flag with srun

Jobs submitted to Slurm with srun can be run inside a container instance using Apptainer. First, declare both the partition you want your workload to run within and the container image that will be used by Apptainer to provide the runtime environment of your workload. For example, to select compute as the partition your workload will run within, and use an Ubuntu 22.04 LTS container image as the runtime environment:

user@login:~$
PARTITION=slurmd
user@login:~$
CONTAINER=docker://ubuntu:22.04

Now run your workload with srun:

user@login:~$
srun --partition $PARTITION --container $CONTAINER cat /etc/os-release | grep ^VERSION
INFO:    Converting OCI blobs to SIF format
INFO:    Starting build...
INFO:    Fetching OCI image...
INFO:    Extracting OCI image...
INFO:    Inserting Apptainer configuration...
INFO:    Creating SIF file...
VERSION_ID="22.04"
VERSION="22.04.5 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy