Containers on Slurm
Exercises¶
- Pull and inspect a container
Pull a container image from a public registry and examine its metadata. Use an image relevant to your work, or try the official Ubuntu image as a simple test. Inspect the image to see its labels, environment, and runscript.
Hint / Solution
# Load apptainer via Lmod (the canonical HPC pattern — the
# binary lives on a shared filesystem so the same install is
# reachable from head and compute nodes).
module load apptainer/1.3.6
# Pull an image from Docker Hub (converts to SIF format)
apptainer pull ubuntu_latest.sif docker://ubuntu:latest
# Inspect the image metadata
apptainer inspect ubuntu_latest.sif
# See the image's default runscript
apptainer inspect --runscript ubuntu_latest.sif
# See the image's environment setup
apptainer inspect --environment ubuntu_latest.sif
# Quick test -- run a command inside the container
apptainer exec ubuntu_latest.sif cat /etc/os-release
slurm-training-val (Slurm 25.11.4, ParallelCluster 3.15.0).
$ module load apptainer/1.3.6 && apptainer --version
apptainer version 1.3.6
$ module load apptainer/1.3.6 && apptainer pull --force ubuntu.sif docker://public.ecr.aws/docker/library/ubuntu:latest 2>&1
INFO: Using cached SIF image
$ module load apptainer/1.3.6 && apptainer exec ubuntu.sif cat /etc/os-release
INFO: squashfuse not found, will not be able to mount SIF or other squashfs files
INFO: gocryptfs not found, will not be able to use gocryptfs
INFO: Converting SIF file to temporary sandbox...
PRETTY_NAME="Ubuntu 24.04.4 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.4 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
INFO: Cleaning up image...
- Run a containerized job
Write a Slurm batch script that runs a command inside a container. Use --bind to mount a data directory from the host into the container. Verify that the job can read input data and write output back to the host filesystem.
Hint / Solution
# Create test input data
mkdir -p /shared/$USER/container_test/input
mkdir -p /shared/$USER/container_test/output
echo "Hello from the host" > /shared/$USER/container_test/input/data.txt
# Write the job script — note the `module load apptainer` line:
# batch jobs run on compute nodes which don't have apptainer on
# PATH by default. Loading the module brings it onto PATH from
# the shared install.
cat > container_job.sh << 'EOF'
#!/bin/bash
#SBATCH --job-name=container_test
#SBATCH --output=container_%j.out
#SBATCH --time=00:10:00
#SBATCH --ntasks=1
#SBATCH --mem=2G
module load apptainer/1.3.6
DATADIR=/shared/$USER/container_test
apptainer exec \
--bind $DATADIR/input:/mnt/input \
--bind $DATADIR/output:/mnt/output \
ubuntu_latest.sif \
bash -c "cat /mnt/input/data.txt && echo 'Processed at $(date)' > /mnt/output/result.txt"
echo "Job finished. Output:"
cat $DATADIR/output/result.txt
EOF
# Submit
sbatch container_job.sh
slurm-training-val (Slurm 25.11.4, ParallelCluster 3.15.0).