You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -14,63 +14,74 @@ Unlike Docker, Apptainer does not require a privileged daemon running on the sys
14
14
15
15
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.
16
16
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.
19
22
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
+
:::
21
25
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:
Torch provides container images with both `.sif` and `.sqf` extensions.
33
35
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`.
35
37
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`.
For this tutorial, we will use the Ubuntu 24.04 image that is already available on the cluster.
43
43
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
49
52
```
50
53
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.
52
55
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.
54
57
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`.
56
59
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.
58
63
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.
This starts a container and runs a command that you provide on the command line.
70
+
Output:
68
71
69
-
```bash
70
-
apptainer exec image.sif command
72
+
```text
73
+
Hello World!
71
74
```
72
75
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.
74
85
75
86
## Opening an Interactive Shell Within a Container
So far, we have used container images that are already available on Torch under `/share/apps/images`.
121
132
122
133
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.
123
134
124
-
For example, we can pull the official Ubuntu Docker image:
135
+
For example, we can pull an official PyTorch image from Docker Hub:
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:
0 commit comments