Skip to content

Commit d2630ff

Browse files
authored
Update 11_apptainer on Torch
1 parent 3443137 commit d2630ff

1 file changed

Lines changed: 52 additions & 239 deletions

File tree

docs/hpc/13_tutorial_intro_hpc/11_apptainer on Torch

Lines changed: 52 additions & 239 deletions
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,74 @@ Unlike Docker, Apptainer does not require a privileged daemon running on the sys
1414

1515
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.
1616

17-
# Using Apptainer to run commands on Torch
18-
## Running Specific Commands Within a Container
17+
# Using Apptainer to Run Commands on Torch
18+
19+
:::warning
20+
21+
Container workloads should be run on compute nodes rather than login nodes.
1922

20-
In the previous episode, we launched a container using a prebuilt Ubuntu image available on Torch. By default, a container may be configured to run a specific application when it starts. What if we want to run a different command inside the container?
23+
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.
24+
:::
2125

22-
If we know the path of an executable we want to run, we can use the `apptainer exec` command. For example, using the Ubuntu container image provided on Torch:
26+
Torch provides many prebuilt container images under:
2327

2428
```bash
25-
apptainer exec /share/apps/images/ubuntu-24.04.3.sif /bin/echo "Hello World!"
29+
ls /share/apps/images/
2630
```
2731

28-
Output:
32+
:::note
2933

30-
```text
31-
Hello World!
32-
```
34+
Torch provides container images with both `.sif` and `.sqf` extensions.
3335

34-
Here, Apptainer starts a container from the Ubuntu image and executes the `/bin/echo` command inside the container. Once the command finishes, the container exits.
36+
`.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`.
3537

36-
We can also run other commands inside the container. For example:
38+
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`.
3739

38-
```bash
39-
apptainer exec /share/apps/images/ubuntu-24.04.3.sif cat /etc/os-release
40-
```
40+
:::
4141

42-
Example output:
42+
For this tutorial, we will use the Ubuntu 24.04 image that is already available on the cluster.
4343

44-
```text
45-
PRETTY_NAME="Ubuntu 24.04.3 LTS"
46-
NAME="Ubuntu"
47-
VERSION_ID="24.04"
48-
...
44+
## Running Your First Container
45+
46+
Apptainer images can define a default action that runs when the container starts.
47+
48+
To launch a container and execute its default action, use `apptainer run`:
49+
50+
```bash
51+
apptainer run /share/apps/images/ubuntu-24.04.3.sif
4952
```
5053

51-
This confirms that the command is being executed within the Ubuntu container environment.
54+
Depending on how the image was built, this command may produce output, launch an application, or simply start and exit.
5255

53-
## The Difference Between `apptainer run` and `apptainer exec`
56+
The important point is that with `apptainer run`, Apptainer executes the default action defined by the image creator.
5457

55-
Above we used the `apptainer exec` command. In the previous episode we used `apptainer run`. To clarify, the difference between these two commands is:
58+
Sometimes, however, we want to run a specific command instead of the image's default action. In those cases, we use `apptainer exec`.
5659

57-
### `apptainer run`
60+
## Running Specific Commands Within a Container
61+
62+
Unlike `apptainer run`, which executes the image's default action, `apptainer exec` allows us to specify exactly what command should run inside the container.
5863

59-
This starts a container and executes the default application configured within the image. You do not specify a command. Instead, Apptainer runs whatever application the image creator configured as the default.
64+
For example:
6065

6166
```bash
62-
apptainer run image.sif
67+
apptainer exec /share/apps/images/ubuntu-24.04.3.sif /bin/echo "Hello World!"
6368
```
6469

65-
### `apptainer exec`
66-
67-
This starts a container and runs a command that you provide on the command line.
70+
Output:
6871

69-
```bash
70-
apptainer exec image.sif command
72+
```text
73+
Hello World!
7174
```
7275

73-
Any default application configured within the image is ignored, and the command you specify is executed instead.
76+
## The Difference Between `apptainer run` and `apptainer exec`
77+
78+
Both `apptainer run` and `apptainer exec` start a container, but they serve different purposes.
79+
80+
`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.
81+
82+
`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.
83+
84+
In practice, `apptainer exec` is often used when working on HPC systems because it provides more control over what is executed inside the container environment.
7485

7586
## Opening an Interactive Shell Within a Container
7687

@@ -85,7 +96,7 @@ apptainer shell /share/apps/images/ubuntu-24.04.3.sif
8596
You should see a prompt similar to:
8697

8798
```text
88-
Apptainer>
99+
Singularity>
89100
```
90101

91102
You can now run commands inside the container:
@@ -115,245 +126,47 @@ exit
115126

116127
This returns you to your normal shell on Torch.
117128

118-
## Pulling Container Images
129+
## Using Docker images with Apptainer
119130

120131
So far, we have used container images that are already available on Torch under `/share/apps/images`.
121132

122133
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.
123134

124-
For example, we can pull the official Ubuntu Docker image:
135+
For example, we can pull an official PyTorch image from Docker Hub:
125136

126137
```bash
127-
apptainer pull ubuntu-24.04.sif docker://ubuntu:24.04
138+
apptainer pull pytorch.sif docker://pytorch/pytorch:latest
128139
```
129140

130141
During the pull process, Apptainer downloads the Docker image layers and converts them into a single SIF image:
131142

132143
```text
133144
INFO: Converting OCI blobs to SIF format
134145
INFO: Starting build...
146+
INFO: Fetching OCI image...
135147
...
136148
INFO: Creating SIF file...
137149
```
138150

139-
When the command completes, a new image named `ubuntu-24.04.sif` will be created in the current directory.
151+
When the command completes, a new image named `pytorch.sif` will be created in the current directory.
140152

141153
You can verify that the image exists:
142154

143155
```bash
144-
ls -lh ubuntu-24.04.sif
156+
ls -lh pytorch.sif
145157
```
146158

147159
The image can now be used like any other Apptainer image.
148160

149161
For example:
150162

151163
```bash
152-
apptainer exec ubuntu-24.04.sif cat /etc/os-release
164+
apptainer exec pytorch.sif python --version
153165
```
154166

155167
or
156168

157169
```bash
158-
apptainer shell ubuntu-24.04.sif
159-
```
160-
161-
## Docker Images and Apptainer
162-
163-
Although Apptainer can pull images directly from Docker registries, it does not run Docker containers directly.
164-
165-
Instead, Apptainer downloads the Docker image layers and converts them into a single Apptainer image file (`.sif`). Once converted, the image can be used without Docker.
166-
167-
This allows researchers to take advantage of the large software ecosystem available through Docker Hub while continuing to use Apptainer on shared HPC systems.
168-
169-
## Torch Images vs Docker Images
170-
171-
Torch already provides many prebuilt container images:
172-
173-
```bash
174-
ls /share/apps/images/
175-
```
176-
177-
For commonly used software, it is often easier to use a cluster-provided image.
178-
179-
If the software you need is not available, you can pull an image directly from Docker Hub and run it with Apptainer.
180-
181-
## Using Containers in Batch Jobs
182-
183-
So far, we have run containers interactively from the command line. In practice, most work on Torch is submitted through Slurm batch jobs.
184-
185-
Containers can be used directly inside a Slurm job script in the same way as any other command.
186-
187-
Create a file named `container_job.sh`:
188-
189-
```bash
190-
#!/bin/bash
191-
#SBATCH --job-name=container-demo
192-
#SBATCH --account=torch_pr_XXX_XXXXX
193-
#SBATCH --time=00:05:00
194-
#SBATCH --cpus-per-task=1
195-
#SBATCH --mem=1G
196-
197-
apptainer exec \
198-
/share/apps/images/ubuntu-24.04.3.sif \
199-
cat /etc/os-release
200-
```
201-
202-
Replace the account name with your own project account.
203-
204-
Submit the job:
205-
206-
```bash
207-
sbatch container_job.sh
208-
```
209-
210-
When the job starts running, Slurm will launch the container and execute the command inside it.
211-
212-
You can monitor the job using:
213-
214-
```bash
215-
squeue --me
216-
```
217-
218-
After the job completes, inspect the output file:
219-
220-
```bash
221-
cat slurm-<jobid>.out
222-
```
223-
224-
You should see output similar to:
225-
226-
```text
227-
PRETTY_NAME="Ubuntu 24.04.3 LTS"
228-
NAME="Ubuntu"
229-
VERSION_ID="24.04"
230-
```
231-
232-
## Running Software from a Container in a Batch Job
233-
234-
Instead of running a simple command, a batch job can also execute software provided by a container image.
235-
236-
For example:
237-
238-
```bash
239-
#!/bin/bash
240-
#SBATCH --job-name=python-demo
241-
#SBATCH --account=torch_pr_XXX_XXXXX
242-
#SBATCH --time=00:05:00
243-
#SBATCH --cpus-per-task=1
244-
#SBATCH --mem=1G
245-
246-
/share/apps/images/run-anaconda3-2024.10-1.bash \
247-
python --version
248-
```
249-
250-
Submit the job:
251-
252-
```bash
253-
sbatch python_job.sh
254-
```
255-
256-
When the job completes, the output file should contain:
257-
258-
```text
259-
Python 3.12.7
260-
```
261-
262-
This approach allows you to use containerized software in batch jobs without installing packages in your home directory.
263-
264-
## Containers and Slurm
265-
266-
From Slurm's perspective, a container is simply another program being executed within the job allocation.
267-
268-
This means that containers can be used together with the same Slurm workflows discussed in previous tutorials, including:
269-
270-
- CPU jobs
271-
- GPU jobs
272-
- Interactive jobs
273-
- Job arrays
274-
275-
In most cases, the only difference is that the application is launched through Apptainer rather than directly from the host operating system.
276-
277-
## Writable Containers and Overlays
278-
279-
Apptainer images are immutable by default. This means that files inside the image cannot be modified.
280-
281-
For example, if you start a shell inside a container:
282-
283-
```bash
284-
apptainer shell \
285-
/share/apps/images/ubuntu-24.04.3.sif
286-
```
287-
288-
you can view files inside the image, but changes made to the image itself will not be saved.
289-
290-
This behavior helps ensure that container images remain reproducible and consistent across users.
291-
292-
## Why Use Overlays?
293-
294-
In some situations, you may want to install additional software or save files inside a container environment.
295-
296-
Examples include:
297-
298-
- Installing Python packages
299-
- Creating Conda environments
300-
- Saving configuration files
301-
- Building custom software
302-
303-
Rather than modifying the image itself, Apptainer provides writable overlays.
304-
305-
An overlay acts as a writable layer that sits on top of a read-only container image.
306-
307-
```text
308-
Ubuntu Image (.sif)
309-
+
310-
Writable Overlay
311-
=
312-
Custom Environment
313-
```
314-
315-
Changes are written to the overlay while the original image remains unchanged.
316-
317-
## Using an Overlay
318-
319-
A container can be started with an overlay using the `--overlay` option:
320-
321-
```bash
322-
apptainer shell \
323-
--overlay overlay.ext3:rw \
324-
/share/apps/images/ubuntu-24.04.3.sif
170+
apptainer exec pytorch.sif python -c "import torch; print(torch.__version__)"
325171
```
326172

327-
Files created during the session are stored in the overlay rather than inside the image.
328-
329-
The next time the same overlay is attached, those files will still be available.
330-
331-
## Using Fakeroot
332-
333-
Some software installation workflows expect root privileges.
334-
335-
Apptainer provides a `--fakeroot` option that gives root-like permissions inside the container without requiring administrative access on the cluster.
336-
337-
For example:
338-
339-
```bash
340-
apptainer shell \
341-
--fakeroot \
342-
--overlay overlay.ext3:rw \
343-
/share/apps/images/ubuntu-24.04.3.sif
344-
```
345-
346-
This is commonly used when creating Conda environments or installing software into a writable overlay.
347-
348-
## When Should You Use an Overlay?
349-
350-
For most users, Torch-provided container images are sufficient.
351-
352-
Consider using an overlay when:
353-
354-
- You need additional packages that are not included in an image.
355-
- You want to save changes between container sessions.
356-
- You need a custom software environment for a project.
357-
358-
Using overlays allows you to customize your environment while continuing to use centrally maintained container images.
359-

0 commit comments

Comments
 (0)