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:
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:
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:
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:
PROGRAM hello_world
IMPLICIT NONE
PRINT *, "Hello world!"
END PROGRAM hello_world
Now use the built container to compile and run your Fortran program:
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:
#!/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
:
Use cat
to view the results of your workload after it completes:
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:
Now run your workload with srun
: