13 Containers
13.1 The Concept
All credit for the concept section goes to Center for High Throughput Computing (CHTC) from University of Wisconsin-Madison, as the following sections have been copied from https://chtc.cs.wisc.edu/uw-research-computing/software-overview-htc/#container-overview on November 30th, 2026. Alternatively, check out: https://coderefinery.github.io/hpc-containers/
13.1.1 What is a container?
“A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.” – Docker
A container is a portable, self-contained operating system and can be easily executed on different computers regardless of their operating systems or programs. When building the container you can choose the operating system you want to use, and can install programs as if you were the owner of the computer.
As an analogy, you could consider a container to be like a camping backpack. Every time you plan to use it, you will need a standard set of gear, which you could pre-pack. Other items, like maps, food, or fuel would depend on where you’re going, but you would still have access to the standard gear.
In the same way, you build a container image by installing your software and any additional dependencies. Jobs that use containers can differ in their tasks or data, but they still have access to the installed software and environment.
While there are some caveats, containers are useful for deploying software on shared computing systems like CHTC, where you do not have permission to install programs directly.
“You can build a container using Apptainer on your laptop, and then run it on many of the largest HPC clusters in the world, local university or company clusters, a single server, in the cloud, or on a workstation down the hall.” – Apptainer
What is a Container Image?
A “container image” is the persistent, on-disk copy of the container. When we talk about building or moving or distributing a container, we’re actually talking about the file(s) that constitute the container. When a container is “running” or “executed”, the container image is used to create the run time environment for executing the programs installed inside of it.
13.1.1.1 Why use containers?
We recognize that learning about containers is its own challenge! However, we have found in the long run, that containers provide the best long-term user experience when running jobs in CHTC, including:
a consistent environment no matter what computer your job runs on
a consistent environment with stable software versions (good for reproducibility)
ability to fully customize the software installation
Our goal is to make finding or building your container as easy as possible, so you can have most of these benefits, without too much pain.
13.2 Our HPC (H4H)
On the cluster, we don’t have access to Docker, however we do have access to singularity/apptainer, this can be loaded with
# 1. Singularity
module load singularity
# 2. Apptainer
13.3 Local Devices
In the sections below, two ways to install apptainer on MacOS are highlighted. However, you can find Apptainer’s official guide here for installation: https://apptainer.org/docs/admin/main/installation.html#.
13.3.1 Running apptainer on MacOS with Apple Silicon (M-chip) through container
Ref: apptainer.org/docs/admin/main/installation.html#apple-container
13.3.1.1 Download Apple container
Option 1: Homebrew
brew install container
Option 2: GitHub
- Download and install the
.pkgfrom the container releases page, then start the system:
13.3.1.2 Create Apple container
Clone https://github.com/GaitiLab/containers/tree/dev-Joan or copy this folder: https://github.com/GaitiLab/containers/tree/dev-Joan/apptainer
# 1. Start system
container system start
# 2. Enter apptainer directory with Dockerfile; the directory
cd apptainer
container build -t local/apptainer-rocky:latest .
# 3. Create the machine
container machine create local/apptainer-rocky:latest --name apptainer
# 4. Test machine
container machine run -n apptainer -- apptainer --version
container machine run -n apptainer -- apptainer pull docker://rockylinux/rockylinux:9
container machine run -n apptainer -- apptainer exec rockylinux_9.sif cat /etc/os-release
13.3.1.3 Shell Integration
# 5. Add wrapper functions
cat > ~/.apptainer-machine.zsh << 'EOF'
APPTAINER_MACHINE="${APPTAINER_MACHINE:-apptainer}"
apptainer() {
if [ "$1" = "build" ]; then
container machine run -n "$APPTAINER_MACHINE" -- \
sudo apptainer "$@"
else
container machine run -n "$APPTAINER_MACHINE" -- \
apptainer "$@"
fi
}
apptainer-vm() {
container machine run -n "$APPTAINER_MACHINE"
}
EOF
source ~/.apptainer-machine.zsh
# Add to .zshrc
echo '[ -f ~/.apptainer-machine.zsh ] && . ~/.apptainer-machine.zsh' >> ~/.zshrc
# Reload .zshrc
source ~/.zshrc
# 6. Make exectuable
mkdir -p ~/.local/bin
cat > ~/.local/bin/run-singularity << 'SHIM'
#!/bin/sh
. ~/.apptainer-machine.zsh
apptainer run "$@"
SHIM
chmod +x ~/.local/bin/run-singularity
13.3.1.4 Usage
13.3.1.4.1 Test Installation
# For testing installation
# 1. pull a container with different arch
apptainer pull --arch amd64 docker://ghcr.io/r-lib/rig/ubuntu-22.04-release
# 2. Use container (interactive)
# a. Open VM
apptainer-vm
# b. Run container
apptainer run ubuntu-22.04-release_latest.sif
13.3.1.5 General use case
NOTE if you download
test.deffrom GitHub (through your browser), then update the extension to.defbecause it may be automatically downloaded as .txt.
# Building a container
# --arch amd64 needed for H4H (HPC)
apptainer build --force --arch amd64 test.sif test.def
# you may need to add: --ignore-fakeroot-command
# Use container (interactive)
# 1. Open VM
apptainer-vm
# 2. Run container
apptainer run test.sif
# apptainer run --cleanenv base.sif
13.3.2 Running apptainer on MacOS with Apple Silicon (M-chip) through lima
https://clange.ch/posts/apptainer-on-mac/#apptainer-through-lima
brew install lima
limactl start template://apptainer --cpus 4 --memory 8 --vm-type=vz --rosetta
limactl delete apptainer
alias apptainer="limactl shell apptainer apptainer"
13.4 Creating Containers
Before trying to create your own container, a number of containers have been created for specific packages/purposes and can be found here: https://github.com/GaitiLab/containers. These can also be found on H4H.
13.4.1 A Basic Walk Through
These tutorials are modified from https://chtc.cs.wisc.edu/uw-research-computing/hello-containers/
General steps:
Write a definition file (
.def`)Build the container (either locally or through a SLURM job), generates a
.sif` file.This can be done with:
apptainer build my_container.sif my_container.def # in case you're building on a MacOS system and the goal is to use the container on the HPC, please use apptainer build --arch amd64 my_container.sif my_container.defUse your container
apptainer run --cleanenv my_container.sif s
13.4.1.1 Using micromamba as backbone (base)
https://hub.docker.com/r/mambaorg/micromamba
Bootstrap: docker
From: mambaorg/micromamba:ubuntu22.04
%post
micromamba install -y -c conda-forge numpy pandas
apptainer build --arch amd64 tutorial_conda.sif tutorial_conda.def
Instead of specifying the required packages explicitly, you can use a regular conda environment yml file, e.g.
name: base
channels:
- conda-forge
- bioconda
dependencies:
- r-base=4.3.3
- r-seurat=4.4.0
- r-pakThen your new definition file becomes:
Bootstrap: docker
From: mambaorg/micromamba:ubuntu22.04
%files
tutorial_mm_with_file.yml /tutorial_mm_with_file.yml
%post
micromamba install -y -n base -f /tutorial_mm_with_file.yml && \
micromamba clean --all --yes
System packages can also be installed directly. Since the micromamba container is based on ubuntu you can install these packages with apt-get`
Bootstrap: docker
From: mambaorg/micromamba:ubuntu22.04
%post
apt-get update -y && \
atp-get install hdf5-devel
13.4.1.2 Using rig as backbone (base)
Rig is an R installation manager, that allows you to use different R versions.
For more info, see: https://github.com/r-lib/rig / https://rig.r-lib.org
Bootstrap: docker
From: ghcr.io/r-lib/rig/ubuntu-22.04-release
%post
export DEBIAN_FRONTEND=noninteractive
# Installs many of the system dependencies that may be needed for installing R-packages
apt-get update && apt-get install -y --no-install-recommends python3 cmake libcurl4-openssl-dev libssl-dev pandoc libcairo2-dev \
libx11-dev libgdal-dev gdal-bin libgeos-dev libproj-dev git xz-utils pari-gp libfftw3-dev \
make libfontconfig1-dev libfreetype6-dev perl libpng-dev libgmp3-dev zlib1g-dev libhdf5-dev \
libnetcdf-dev libglpk-dev libxml2-dev jags libjpeg-dev libsqlite3-dev libmagick++-dev \
gsfonts libzstd-dev poppler-data libpoppler-cpp-dev libtiff-dev libgsl0-dev libbz2-dev \
liblzma-dev default-jdk libicu-dev libfribidi-dev libharfbuzz-dev libudunits2-dev python3-pip pipx curl fakeroot
# Clean-up
apt-get autoremove -y && apt-get clean
# Install pkgs to make coding easier
pipx ensurepath
pipx install ptpython
pipx install radian
# Make pipx applications available system-wide
ln -s /root/.local/bin/ptpython /usr/local/bin/ptpython
ln -s /root/.local/bin/radian /usr/local/bin/radian
# For terminal
export OSH=/root/.oh-my-bash
git clone --depth=1 \
https://github.com/ohmybash/oh-my-bash.git \
"$OSH"
cp "$OSH/templates/bashrc.osh-template" \
/root/.bashrc
# Full clean-up
pip cache purge || true
13.4.1.3 Building a “multi-layer” container
You can reuse your previous containers (.sif) and add to it.
Bootstrap: localimage
From: rig_base.sif
%post
rig add 4.4.3
%runscript
exec /bin/bash "$@"
13.4.1.4 Using your container interactively (terminal)
container_name="rig_add_on_base"
apptainer run --cleanenv /Users/joankant/Documents/Coding/guides/chapters/reproducible_research/tutorial_apptainer/${container_name}.sif sh