Execution

Modified

November 10, 2023

Abstract

This section describes how to use an Apptainer container image as execution environment of an application on the compute cluster.

Interactive

Start an interactive shell in a container image:

# this example uses the Python container
apptainer pull $APPTAINER_CONTAINERS/python.sif docker://python:latest

# start a shell within the container environment
apptainer shell $APPTAINER_CONTAINERS/python.sif

Use srun to allocate an interactive shell (cf. real time allocation):

» srun -p debug --pty -- apptainer shell $APPTAINER_CONTAINERS/python.sif
apptainer> which python
/usr/local/bin/python
apptainer> python --version
Python 3.8.3

Command Options

Option Description
--singularity-container Path to the Singularity container

Instead of invoking the apptainer command yourself, it is possible to specify the container via command option.

srun -p debug \
      --singularity-container=$APPTAINER_CONTAINERS/python.sif \
      --pty /bin/bash

Environment Variable

Environment Variable Description
SLURM_SINGULARITY_CONTAINER Path to the Singularity container

Alternatively export an environment variable to set the absolute path to a container image. The PATH environment variable from your local shell is used for path resolution. Unless a path is the same on the host and the container, it is required to use an absolute path to start an executable:

# Set the path to the container image...
export SLURM_SINGULARITY_CONTAINER=$APPTAINER_CONTAINERS/python.sif

# ...use absolute path the application (in this example the Python interpreter)
» srun -p debug --pty -- /usr/local/bin/python --version
Python 3.8.3

Batch Job

The simple batch script created blow prints information about its runtime environment and the available Python interpreter:

# simple script executing a couple of commands
cat > $LUSTRE_HOME/bin/batch.sh <<EOF
#!/usr/bin/env bash
uname -nr
cat /etc/os-release | head -2
which python
python \${1:-'--version'}
EOF
# make it executable
chmod +x $LUSTRE_HOME/bin/batch.sh

Executing this script on a submit node will print the Virgo host platform:

» $LUSTRE_HOME/bin/batch.sh 
lxbk0595 3.10.0-1127.18.2.el7.x86_64
NAME="CentOS Linux"
VERSION="7 (Core)"
/usr/bin/python
Python 2.7.5

Execution of the same script in a container prints the container applications environment.

# run the script in the offical Python container
» apptainer exec $APPTAINER_CONTAINERS/python.sif $LUSTRE_HOME/bin/batch.sh
lxbk0595 3.10.0-1127.18.2.el7.x86_64
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
/usr/local/bin/python
Python 3.8.3

Execute the batch script with the sbatch command (cf. batch jobs)

» sbatch -p debug \
      --singularity-container=$APPTAINER_CONTAINERS/python.sif \
      -- $LUSTRE_HOME/bin/batch.sh
Submitted batch job 11777406

# print the output
» cat slurm-11777406.out
lxbk0595 3.10.0-1127.18.2.el7.x86_64
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
/usr/local/bin/python
Python 3.8.3

Following is a simple Python script to print the version information about the Python interpreter its executed with:

cat > $LUSTRE_HOME/bin/python_version.py <<EOF
#!python
import sys
print(sys.version)
EOF
chmod +x $LUSTRE_HOME/bin/python_version.py

Use the batch script to execute the Python script on the cluster with sbatch:

» sbatch -p debug \
      --singularity-container=$APPTAINER_CONTAINERS/python.sif \
      -- $LUSTRE_HOME/bin/batch.sh $LUSTRE_HOME/bin/python_version.py
Submitted batch job 11777645

# print the output
» cat slurm-11777645.out
lxbk0595 3.10.0-1127.18.2.el7.x86_64
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
/usr/local/bin/python
3.8.3 (default, Jun  9 2020, 17:39:39)
[GCC 8.3.0]