Skip to content

Commit c041d0a

Browse files
authored
Update 01_intro_apptainer.mdx
1 parent 7c113e3 commit c041d0a

1 file changed

Lines changed: 6 additions & 197 deletions

File tree

docs/hpc/15_tutorial_apptainer/01_intro_apptainer.mdx

Lines changed: 6 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -14,210 +14,19 @@ 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-
19-
:::warning
20-
21-
Container workloads should be run on compute nodes rather than login nodes.
22-
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-
:::
25-
26-
Torch provides many prebuilt container images under:
27-
28-
```bash
29-
ls /share/apps/images/
30-
```
31-
32-
:::note
17+
## What are .sif and .sqf files
3318

3419
Torch provides container images with both `.sif` and `.sqf` extensions.
3520

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`.
37-
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`.
39-
40-
:::
41-
42-
For this tutorial, we will use the Ubuntu 24.04 image that is already available on the cluster.
43-
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
52-
```
53-
54-
Depending on how the image was built, this command may produce output, launch an application, or simply start and exit.
55-
56-
The important point is that with `apptainer run`, Apptainer executes the default action defined by the image creator.
57-
58-
Sometimes, however, we want to run a specific command instead of the image's default action. In those cases, we use `apptainer exec`.
59-
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.
63-
64-
For example:
65-
66-
```bash
67-
apptainer exec /share/apps/images/ubuntu-24.04.3.sif /bin/echo "Hello World!"
68-
```
69-
70-
Output:
71-
72-
```text
73-
Hello World!
74-
```
75-
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.
85-
86-
## Opening an Interactive Shell Within a Container
87-
88-
Sometimes it is useful to explore a container interactively. Apptainer provides the `apptainer shell` command for this purpose.
89-
90-
Launch a shell inside the Ubuntu container:
91-
92-
```bash
93-
apptainer shell /share/apps/images/ubuntu-24.04.3.sif
94-
```
95-
96-
You should see a prompt similar to:
97-
98-
```text
99-
Singularity>
100-
```
101-
102-
You can now run commands inside the container:
103-
104-
```bash
105-
whoami
106-
pwd
107-
cat /etc/os-release
108-
```
109-
110-
Example output:
111-
112-
```text
113-
PRETTY_NAME="Ubuntu 24.04.3 LTS"
114-
NAME="Ubuntu"
115-
VERSION_ID="24.04"
116-
...
117-
```
118-
119-
Notice that the prompt changes to indicate that you are working inside the container environment.
120-
121-
When you are finished, leave the container with:
122-
123-
```bash
124-
exit
125-
```
126-
127-
This returns you to your normal shell on Torch.
128-
129-
# Files in Apptainer Containers
130-
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.
131-
132-
## Accessing Your Files
133-
134-
While inside a container, check your current directory:
135-
136-
```bash
137-
pwd
138-
```
139-
140-
You can also list files in your home directory:
141-
142-
```bash
143-
ls ~
144-
```
145-
146-
The files and directories you see should match those available outside the container.
147-
148-
Files created in these mounted directories remain available after the container exits.
149-
150-
## Binding Additional Directories
151-
152-
Sometimes you may need access to additional directories that are not automatically available inside the container.
153-
154-
Apptainer allows additional directories to be mounted using the `-B` option:
155-
156-
```bash
157-
apptainer shell \
158-
-B /scratch:/scratch \
159-
/share/apps/images/ubuntu-24.04.3.sif
160-
```
161-
162-
This makes `/scratch` available inside the container.
163-
164-
You can also mount a directory at a different location:
165-
166-
```bash
167-
apptainer shell \
168-
-B /scratch:/data \
169-
/share/apps/images/ubuntu-24.04.3.sif
170-
```
171-
172-
In this example, files stored in `/scratch` on the host system are accessible through `/data` inside the container.
173-
174-
## Why This Matters
175-
176-
Container images are typically read-only. Research data, scripts, notebooks, and output files usually remain outside the container.
177-
178-
By making host directories available inside the container, Apptainer allows applications to access data stored on Torch while maintaining a reproducible software environment.
179-
180-
# Using Docker Images with Apptainer
181-
182-
So far, we have used container images that are already available on Torch under `/share/apps/images`.
183-
184-
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.
185-
186-
For example, we can pull an official PyTorch image from Docker Hub:
187-
188-
```bash
189-
apptainer pull pytorch.sif docker://pytorch/pytorch:latest
190-
```
191-
192-
During the pull process, Apptainer downloads the Docker image layers and converts them into a single SIF image:
193-
194-
```text
195-
INFO: Converting OCI blobs to SIF format
196-
INFO: Starting build...
197-
INFO: Fetching OCI image...
198-
...
199-
INFO: Creating SIF file...
200-
```
201-
202-
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.
203-
When the command completes, a new image named `pytorch.sif` will be created in the current directory.
21+
`.sif` is the standard Apptainer image format and can be used directly with commands such as `apptainer exec`, `apptainer run`, and `apptainer shell`.
20422

205-
You can verify that the image exists:
23+
Some Torch-provided application images use `.sqf` 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.
20624

207-
```bash
208-
ls -lh pytorch.sif
209-
```
25+
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.
21026

211-
The image can now be used like any other Apptainer image.
27+
## Apptainer Cache
21228

213-
For example:
29+
When Apptainer pulls an image from a remote registry, it stores downloaded image layers in a local cache. If the same image is requested again and has not changed, Apptainer can reuse the cached data instead of downloading it again. This reduces network transfers and can significantly speed up repeated image pulls.
21430

215-
```bash
216-
apptainer exec pytorch.sif python --version
217-
```
21831

219-
or
22032

221-
```bash
222-
apptainer exec pytorch.sif python -c "import torch; print(torch.__version__)"
223-
```

0 commit comments

Comments
 (0)