Skip to content

Commit 694a2df

Browse files
authored
Create 02_Running Containers
1 parent d344e65 commit 694a2df

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
Sometimes, however, we want to run a specific command instead of the image's default action. In those cases, we use `apptainer exec`.
43+
44+
## Running Specific Commands Within a Container
45+
46+
Unlike `apptainer run`, which executes the image's default action, `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+
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.
69+
70+
## Opening an Interactive Shell Within a Container
71+
72+
Sometimes it is useful to explore a container interactively. Apptainer provides the `apptainer shell` command for this purpose.
73+
74+
Launch a shell inside the Ubuntu container:
75+
76+
```bash
77+
apptainer shell /share/apps/images/ubuntu-24.04.3.sif
78+
```
79+
80+
You should see a prompt similar to:
81+
82+
```text
83+
Singularity>
84+
```
85+
86+
You can now run commands inside the container:
87+
88+
```bash
89+
whoami
90+
pwd
91+
cat /etc/os-release
92+
```
93+
94+
Example output:
95+
96+
```text
97+
PRETTY_NAME="Ubuntu 24.04.3 LTS"
98+
NAME="Ubuntu"
99+
VERSION_ID="24.04"
100+
...
101+
```
102+
103+
Notice that the prompt changes to indicate that you are working inside the container environment.
104+
105+
When you are finished, leave the container with:
106+
107+
```bash
108+
exit
109+
```
110+
111+
This returns you to your normal shell on Torch.

0 commit comments

Comments
 (0)