Apptainer tutorial¶
In this tutorial you will learn
- Basic usage of Apptainer (previously Singularity) containers
Slides
To run these exercises on Roihu, use sinteractive or open a compute node
shell in the Roihu web interface.
Getting started¶
-
Download a test container image from Allas:
-
The file we downloaded is a container image. It contains all the software and data of the container in a single file. In this case, the container is very bare-bones and thus quite small, about 50 MB.
- Actual application containers are typically larger since they also contain the software installation and may in some cases include reference data, etc.
Basic usage¶
There are three basic ways to run software in an Apptainer container:
Apptainer exec¶
-
To execute a command inside the container, use
apptainer exec: -
Compare the outputs of the following commands:
- The first command is run on the host, the second command is run inside the container.
The tutorial container is based on Ubuntu 18.04. The host and the container use the same kernel, but the rest of the system can vary.
- This means that a container can be based on a different Linux distribution than the host (as long as they are kernel-compatible), but it can't run a totally different OS, such as Windows or macOS.
In batch jobs¶
apptainer exec is the run method you would typically use in batch job
scripts.
-
Create a file called
test.sh: -
Copy the following contents into the file and replace
<project>with your actual CSC project, e.g.project_2001234:#!/bin/bash #SBATCH --job-name=test # Name of the job visible in the queue. #SBATCH --account=<project> # Choose the billing project. Has to be defined! #SBATCH --partition=test # Job queues (CPU): interactive, test, small, medium, large, longrun, hugemem, hugemem_longrun #SBATCH --time=00:01:00 # Maximum duration of the job. Max: depends on the partition. #SBATCH --mem=1G # How much RAM is reserved for job per node. #SBATCH --ntasks=1 # Number of tasks. Max: depends on partition. #SBATCH --cpus-per-task=1 # How many processors work on one task. Max: Number of CPUs per node. apptainer exec tutorial.sif hello_world -
Submit the job to the queue with:
For more information about batch jobs, see the
batch jobs section.
Apptainer run¶
When containers are created, a standard action called the
runscript is
defined. Depending on the container, it may simply print out a message, or it
may launch a program or service inside the container.
If you are using a container created by someone else, you will need to check
the documentation provided by the creator for details.
-
In our test container the
runscriptprints out a simple message: -
Give the container image execution rights so that you can run it directly:
-
You can see the actual script with the command:
Apptainer shell¶
-
Open a shell inside the container:
-
Notice that the command prompt changed. You can now run any software inside the container interactively:
-
Exit the container with:
More information¶
This tutorial is meant as a brief introduction to get you started.
When searching online for instructions, make sure that the instructions
are for the same version of Apptainer as you are using. There have been some
command syntax changes etc. between versions, so older instructions may not
work as is. Also note that Apptainer was formerly known as Singularity.
For more detailed instructions, see the official
Apptainer documentation.