Python in Containers#

On some of our clusters, such as AISurrey, containers are required to run jobs. On other systems like Eureka2, containers are optional but useful for advanced use cases.

We use Apptainer (formerly Singularity), which is available on all compute nodes and compatible with Docker. For general usage instructions, see Containerisation: Docker & Apptainer.

Containers: What, Why, and When#

A container is a self-contained Linux environment that bundles:

  • A specific Python version

  • Any libraries or dependencies your code needs

  • Optional OS-level tools or system libraries (e.g. CUDA)

It behaves like a lightweight virtual machine, but runs faster and without root permissions.

Examples: Python in containers with Slurm#

Below are some examples of Slurm submission scripts that show common patterns for running Python inside containers.

Run a Python script from the current directory

Use this when your script (e.g. my_script.py) is in the same directory you submit the job from.

#!/bin/bash
#SBATCH --job-name="basic-python"
#SBATCH --partition=2080ti
#SBATCH --gpus=1
#SBATCH --mem=16G
#SBATCH --time=00-00:20:00
#SBATCH -o slurm.%N.%j.out
#SBATCH -e slurm.%N.%j.err

# This pulls a Python 3.11 image from DockerHub and runs your script
apptainer exec docker://python:3.11 python my_script.py

Explanation:

  • The container runs in a temporary environment and sees only certain directories by default.

  • Data written by the script is saved if it’s in a directory visible to the container, see next example.

Access files using --bind
Use an internal container from our own registry
Passing parameters or training with checkpointing

Note that additional examples can be found on our Gitlab instance, which requires a University of Surrey login.

Quick Tips#