Singularity Introduction
Note
This tutorial is adapted from the Container Camp Tutorial produced and copyrighted by CyVerse.
Singularity allows users to run software inside of containers. Another popular container system is Docker, which is interoperable with Singularity. Singularity provides many features that make it well suited for HPC applications, and therefore, it is the container system that is installed on our HPCC.
Installation
To install Singularity on your local machine, please see the installation instructions in the documentation. Singularity can only be run natively on Linux, but if you need to run it locally on a Windows or Mac computer, you can use a virtual machine provided by the creators of Singularity. See these alternative instructions for details.
Note
As of December 2024, the version of Singularity on the MSU HPCC is currently
4.1.2. For any questions that this tutorial or the Advanced
Topics page do not answer, please consult
the official documentation for this
version. All
Singularity commands are built into the system such as singularity shell
and singularity exec
, which means you can invoke these commands directly
from the command line.
Check installation
If you would like to check your installation of Singularity on your local machine or the installation on the HPCC, you can run
1 2 3 4 5 6 |
|
In the above example, we used the Singularity Hub "unique resource identifier,"
or URI, "shub://" which tells the software to run an image from Singularity
Hub. To get help, you can use the --help
flag which gives a general overview
of Singularity options and subcommands as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
You can use the help
command if you want to see the information about
subcommands. For example, to see the pull
command help,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
Downloading pre-built images
We already downloaded a pre-built image "hello-world" from shub, one of the registries, using pull command. This is the easiest way to use Singularity.
You can use the pull
command to download pre-built images from a number of
Container Registries. Here we’ll be focusing on the Singularity Hub or Docker
Hub. The following are some of container registries.
- library - images hosted on Sylabs Cloud
- shub - images hosted on the (archived) Singularity Hub
- docker - images hosted on Docker Hub
Pulling an images from Sylabs cloud library
In this example, I will pull a base Alpine container from Sylabs cloud:
1 2 3 |
|
You can rename the container using the --name
flag:
1 2 3 |
|
The above example will save the image in the current directory as my_alpine.sif
Pulling an image from Docker hub
Many programs are available as Docker containers pre-built, and many of those are available on Docker Hub. For more details on using Docker containers with Singularity, see our section on Migrating from Docker to Singularity.
Here is a quick example of pulling an Alpine Docker container for use with Singularity.
1 2 3 4 5 6 7 8 9 10 11 |
|
Interacting with images
You can interact with images via the shell
, exec
, and run
commands. To
learn how to interact with images, let's first pull an image
lolcow_latest.sif
from the library.
1 |
|
shell
The shell
command allows you to spawn a new shell within your container and
interact with it as if it is a virtual machine.
1 2 |
|
The change in prompt indicates that you have entered the container. Once inside of a container, you are the same user as you are on the host system.
1 2 |
|
You will also have access to a few directories that you can access outside the
container, most notably, the directory you ran the container from and your home
directory. Try running pwd
and ls ~
inside the container to verify this.
Anything you write to these directories will stay around after you are done with the container. This is a significant difference from Docker, where the default is to close off the files in the container and you need to use "bind mounts" to manually connect file spaces.
To exit from a container, type exit
.
1 2 |
|
exec
The exec
command allows you to execute a custom command within a container by
specifying the image file. For instance, to execute the cowsay
program within
the lolcow_latest.sif
container:
1 2 3 4 5 6 7 8 9 |
|
You can also use shell
command to run the program in the container.
1 2 3 4 5 6 7 8 9 10 |
|
run
Singularity containers contain runscripts. These are predefined scripts which
define the actions of a container when user runs it. The runscript can be
performed with the run
command, or simply by calling the container as though
it were an executable.
1 2 3 4 5 6 7 8 9 10 |
|
Submitting a Singularity job
In general, running Singularity commands is the same as running any kind of program when you prepare your SLURM script.
In this case, you put your Singularity commands (singularity exec <image>.sif
<command>
) right after the sbatch
directive lines you use to specify your
job resources. If the program needs to use multiple threads/cores on a node,
say 8, you would request 8 cores using #SBATCH --cpus-per-task=8
as you would
do with any other sbatch
script.
For situations where you are using MPI within the container (e.g., you would like to split your code over multiple nodes) or would like to use GPU resources, please see Using Singularity with MPI and GPUs on the Advanced Topics page.