Skip to content

Commit 0723507

Browse files
authored
Merge pull request #366 from NYU-RTS/Amanda-dong
add a new apptainer tutorial secton
2 parents 9f4a6d8 + 74e301a commit 0723507

10 files changed

Lines changed: 332 additions & 5 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# agent memory
2+
.memsearch/*
3+
14
# pixi environments
25
.pixi
36
*.egg-info

.memsearch/memory/2026-05-14.md

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Introduction to Apptainer on Torch
2+
3+
## Why Containers?
4+
Researchers often rely on complex software stacks that include programming languages and libraries. Installing and maintaining these dependencies can be challenging when different projects require different software versions.
5+
6+
Containers provide a way to package software together with the environment it needs to run. Instead of manually installing every dependency on a system, users can run software inside a container that already includes the required libraries and tools.
7+
8+
9+
## What Is Apptainer?
10+
Apptainer is a container platform that allows users to run software inside isolated environments without requiring administrator privileges on the host system.
11+
12+
Apptainer is the continuation of the Singularity project. The open-source Singularity project was renamed to Apptainer and continues to be developed under the Linux Foundation.
13+
14+
Unlike Docker, Apptainer does not require a privileged daemon running on the system. This makes it well suited for shared HPC environments where security and multi-user access are important considerations.
15+
16+
Torch uses Apptainer as its supported container platform. Many container images distributed through Docker registries can be used directly with Apptainer, allowing researchers to take advantage of existing software environments while working on the cluster.
17+
18+
## What Are `.sif` And `.sqf` Files
19+
20+
Torch provides container images with both `.sif` and `.sqf` extensions.
21+
22+
`.sif` is the standard Apptainer image format and can be used directly with commands such as `apptainer exec`, `apptainer run`, and `apptainer shell`.
23+
24+
Some Torch-provided application images use `.sqf` (Squash File System) files and are typically accessed through wrapper scripts such as `run-anaconda3-2024.10-1.bash`. These scripts handle the details of launching the application environment and are often the recommended interface for users.
25+
26+
When available, use the wrapper script documented for that application. For general Apptainer examples in this tutorial, we use `.sif` images because they can be launched directly with standard Apptainer commands.
27+
28+
## Apptainer Image Cache
29+
30+
While Apptainer doesn’t have a local image repository in the same way as Docker, it does cache downloaded image files. Images are simply `.sif` files stored on your local disk.
31+
32+
If you delete a local `.sif` image that you have pulled from a remote image repository and then pull it again, if the image is unchanged from the version you previously pulled, you will be given a copy of the image file from your local cache rather than the image being downloaded again from the remote source. This removes unnecessary network transfers and is particularly useful for large images which may take some time to transfer over the network.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Using Apptainer to Run Commands on Torch
2+
3+
:::warning
4+
5+
Container workloads should be run on compute nodes rather than login nodes.
6+
7+
While simple commands may work on a login node, pulling images, launching software, installing packages, or building environments can consume significant CPU, memory, and storage resources. These activities should be performed within an interactive Slurm allocation or a batch job.
8+
:::
9+
10+
Torch provides many prebuilt container images under:
11+
12+
```bash
13+
ls /share/apps/images/
14+
```
15+
16+
:::note
17+
18+
Torch provides container images with both `.sif` and `.sqf` extensions.
19+
20+
`.sif` is the standard Apptainer image format. Some Torch-provided application images use `.sqf` and may be intended to be launched through wrapper scripts such as `run-anaconda3-2024.10-1.bash`.
21+
22+
When available, use the wrapper script documented for that application. For general Apptainer examples in this tutorial, we use `.sif` images because they work directly with `apptainer exec`, `apptainer run`, and `apptainer shell`.
23+
24+
:::
25+
26+
For this tutorial, we will use the Ubuntu 24.04 image that is already available on the cluster.
27+
28+
## Running Your First Container
29+
30+
Apptainer images can define a default action that runs when the container starts.
31+
32+
To launch a container and execute its default action, use `apptainer run`:
33+
34+
```bash
35+
apptainer run /share/apps/images/ubuntu-24.04.3.sif
36+
```
37+
38+
Depending on how the image was built, this command may produce output, launch an application, or simply start and exit.
39+
40+
The important point is that with `apptainer run`, Apptainer executes the default action defined by the image creator.
41+
42+
To run a specific command, use `apptainer exec`.
43+
44+
## Running Specific Commands Within a Container
45+
46+
`apptainer exec` allows us to specify exactly what command should run inside the container.
47+
48+
For example:
49+
50+
```bash
51+
apptainer exec /share/apps/images/ubuntu-24.04.3.sif /bin/echo "Hello World!"
52+
```
53+
54+
Output:
55+
56+
```text
57+
Hello World!
58+
```
59+
60+
## The Difference Between `apptainer run` and `apptainer exec`
61+
62+
Both `apptainer run` and `apptainer exec` start a container, but they serve different purposes.
63+
64+
`apptainer run` executes the default action defined by the image creator. Depending on how the image was built, this may launch an application, run a script, or perform another predefined task.
65+
66+
`apptainer exec` allows you to specify exactly which command should run inside the container. Rather than relying on the image's default behavior, you provide the command directly.
67+
68+
## Opening an Interactive Shell Within a Container
69+
70+
There are many reasons why you might want to use a container interactively.
71+
- debugging (software, bind mounts, hardware integrations, etc.)
72+
- testing software upgrades
73+
- rapid Software Prototyping
74+
- data exploration
75+
76+
Apptainer provides the `apptainer shell` command for this purpose.
77+
78+
Launch a shell inside the Ubuntu container:
79+
80+
```bash
81+
apptainer shell /share/apps/images/ubuntu-24.04.3.sif
82+
```
83+
84+
You should see a prompt similar to:
85+
86+
```text
87+
Singularity>
88+
```
89+
90+
You can now run commands inside the container:
91+
92+
```bash
93+
whoami
94+
pwd
95+
cat /etc/os-release
96+
```
97+
98+
Example output:
99+
100+
```text
101+
PRETTY_NAME="Ubuntu 24.04.3 LTS"
102+
NAME="Ubuntu"
103+
VERSION_ID="24.04"
104+
...
105+
```
106+
107+
Notice that the prompt changes to indicate that you are working inside the container environment.
108+
109+
When you are finished, leave the container with:
110+
111+
```bash
112+
exit
113+
```
114+
115+
This returns you to your normal shell on Torch.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Files in Apptainer Containers
2+
Apptainer is designed to work closely with the host filesystem. In most cases, your home directory and current working directory remain accessible from within the container.
3+
4+
## Accessing Your Files
5+
6+
While inside a container, check your current directory:
7+
8+
```bash
9+
pwd
10+
```
11+
12+
You can also list files in your home directory:
13+
14+
```bash
15+
ls ~
16+
```
17+
18+
The files and directories you see should match those available outside the container.
19+
20+
:::note
21+
Files created in these mounted directories remain available after the container exits.
22+
:::
23+
24+
## Binding Additional Directories
25+
26+
Sometimes you may need access to directories through a different path inside the container.
27+
28+
Apptainer allows additional directories to be mounted using the `-B` option:
29+
30+
```bash
31+
apptainer shell \
32+
-B /scratch/<NetID>:/data \
33+
/share/apps/images/ubuntu-24.04.3.sif
34+
```
35+
36+
In this example, your scratch directory on the host system is made available as `/data` inside the container.
37+
38+
After entering the container, you can verify the mount:
39+
40+
```bash
41+
ls /data
42+
```
43+
44+
Any files stored in `/scratch/<NetID>` on the host system will be accessible through `/data` inside the container.
45+
46+
The source and destination paths do not need to be the same. This can be useful when an application expects data to be located in a specific directory within the container.
47+
48+
:::info
49+
50+
Container images are typically read-only. Research data, scripts, notebooks, and output files usually remain outside the container.
51+
52+
By making host directories available inside the container, Apptainer allows applications to access data stored on Torch while maintaining a reproducible software environment.
53+
:::
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Using Docker Images with Apptainer
2+
3+
So far, we have used container images that are already available on Torch under `/share/apps/images`.
4+
5+
In practice, you may also want to run software that is not provided by the cluster. Apptainer can pull images directly from Docker registries and convert them into the Apptainer `SIF` format.
6+
7+
:::warning
8+
Pulling container images can require significant network bandwidth and disk space. On Torch, image downloads should be performed on compute nodes rather than login nodes.
9+
:::
10+
11+
For example, we can pull an official PyTorch image from Docker Hub:
12+
13+
```bash
14+
apptainer pull pytorch.sif docker://pytorch/pytorch:latest
15+
```
16+
17+
During the pull process, Apptainer downloads the Docker image layers and converts them into a single `SIF` image:
18+
19+
```bash
20+
INFO: Converting OCI blobs to SIF format
21+
INFO: Starting build...
22+
INFO: Fetching OCI image...
23+
...
24+
INFO: Creating SIF file...
25+
```
26+
27+
The output shows that Apptainer is downloading the Docker image layers and converting them into a single `SIF` image. Once the conversion completes, the resulting `SIF` file can be used without Docker. When the command completes, a new image named `pytorch.sif` will be created in the current directory.
28+
29+
You can verify that the image exists:
30+
31+
```bash
32+
ls -lh pytorch.sif
33+
```
34+
35+
The image can now be used like any other Apptainer image.
36+
37+
For example:
38+
39+
```bash
40+
apptainer exec pytorch.sif python --version
41+
```
42+
43+
or
44+
45+
```bash
46+
apptainer exec pytorch.sif python -c "import torch; print(torch.__version__)"
47+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Why Customize a Container Environment?
2+
Prebuilt container images provide a convenient starting point, but they may not contain all of the software required for a particular project.
3+
4+
Most Apptainer images are distributed as `.sif` files, which are typically read-only. If you need to install additional software or make persistent changes to a container, Apptainer provides overlays, which add a writable layer on top of the original image while leaving the `.sif` file unchanged.
5+
6+
## Example: Creating a Custom Directory with an Overlay
7+
8+
9+
Pull a Python container image from Docker Hub:
10+
11+
```bash
12+
apptainer pull python.sif docker://python:3.10
13+
```
14+
15+
Create a writable overlay:
16+
17+
```bash
18+
apptainer overlay create --size 1024 python_overlay.ext3
19+
```
20+
21+
Launch the container with the overlay attached:
22+
23+
```bash
24+
apptainer shell \
25+
--overlay python_overlay.ext3:rw \
26+
python.sif
27+
```
28+
29+
Verify that `numpy` is not installed in the base image:
30+
31+
```bash
32+
python -c "import numpy"
33+
```
34+
35+
You should see:
36+
37+
```text
38+
ModuleNotFoundError: No module named 'numpy'
39+
```
40+
41+
Install `numpy`:
42+
43+
```bash
44+
pip install numpy
45+
```
46+
47+
Verify that the packages are available:
48+
49+
```bash
50+
python -c "import numpy; print(numpy.__version__)"
51+
```
52+
53+
Exit the container:
54+
55+
```bash
56+
exit
57+
```
58+
59+
The next time you want to use the customized environment, launch the container using the same overlay:
60+
61+
```bash
62+
apptainer shell \
63+
--overlay python_overlay.ext3:rw \
64+
python.sif
65+
```
66+
67+
This demonstrates that software installed within the overlay persists across sessions, while the original `.sif` image remains unchanged.
68+
69+
:::info
70+
In the examples above we started a shell session with the overlay in read-write mode (`:rw`).
71+
::::warning
72+
Only open the overlay in writable mode when you are adding packages to it. When not loading packages you should use read-only mode (`:ro`).
73+
74+
If you leave the overlay open in read-write mode no other process will be able to open it in either read-write or read-only mode.
75+
::::
76+
read-only overlays can be used by multiple processes which is why they are useful for parallel processing.
77+
:::
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"label": "Tutorial: Apptainer on Torch"
3+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
- Some of your questions may be already answered here
44
- [Tutorial: Introduction to Using the Shell on Torch](../12_tutorial_intro_shell_hpc/01_intro.mdx)
55
- [Tutorial: Introduction to High-Performance Computing](../13_tutorial_intro_hpc/01_intro_hpc.mdx)
6-
- Consider to sign up for Training and Workshop. You can find the list of available HPC coruses [can be viewed at nyu.libcal.com](https://nyu.libcal.com/calendar?cid=1564&t=d&d=0000-00-00&cal=1564&ct=6016).
7-
- Consider signing up for ACCESS workshops. As part of the Advanced Cyberinfrastructure Coordination Ecosystem: Services & Support program, NSF provides tutorials for HPC, OpenOnDemand, etc. Here's a list of upcoming workshops: [link](https://support.access-ci.org/events).
6+
- Consider to sign up for Training and Workshop. You can find the list of available HPC courses [can be viewed at nyu.libcal.com](https://nyu.libcal.com/calendar?cid=1564&t=d&d=0000-00-00&cal=1564&ct=6016).
7+
- Consider signing up for ACCESS workshops. As part of the Advanced Cyberinfrastructure Coordination Ecosystem: Services & Support program, NSF provides tutorials for HPC, `OpenOnDemand`, etc. Here's a list of upcoming workshops: [link](https://support.access-ci.org/events).
88
- [Introductory HPC Video Playlist](https://www.youtube.com/watch?v=0pP_TeKH1MI&list=PL5l6Qz3Xhfi9Jn9-iMKJisYsSW5tRzPSd&t=3s).
99

1010
- NYU HPC offers personalized help through **personal consultations** for simple and advanced cases:

0 commit comments

Comments
 (0)