Skip to content

Commit 175cc9c

Browse files
authored
Appraiser Tutorial
Updated the Apptainer tutorial to clarify steps and improve explanations. Adjusted examples and added details for better understanding.
1 parent f0c616d commit 175cc9c

1 file changed

Lines changed: 70 additions & 13 deletions

File tree

docs/hpc/07_containers/02_apptainer_tutorial.md

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,121 @@
22

33
This tutorial walks you through running your first container on the Torch HPC cluster using Apptainer. By the end of this guide, you will be able to pull a container image, run it, and execute commands inside a containerized environment.
44

5+
For background on what Apptainer is and why we use it on Torch, see [Custom Applications with Containers](../containers/).
6+
57
## Step 1: Pull a Container Image
68

79
Make sure you are on a compute node before proceeding, as running containers on login nodes is not recommended.
810

9-
Pull a container image from Docker Hub:
11+
Pulling an image downloads it from Docker Hub and converts it into an Apptainer `.sif` file that can be run on the cluster.
1012

1113
```sh
1214
apptainer pull docker://python:3.10
1315
```
1416

15-
This command downloads a Docker image and converts it into an Apptainer image (`.sif`) that can be run on the cluster.
17+
This may take a few minutes. You should see progress output like:
18+
19+
```
20+
INFO: Converting OCI blobs to SIF format
21+
INFO: Creating SIF file...
22+
[=====================================================================] 100 % 0s
23+
```
24+
25+
Once complete, you will find a file called `python_3.10.sif` in your current directory:
26+
27+
```sh
28+
ls *.sif
29+
```
30+
31+
```
32+
python_3.10.sif
33+
```
1634

1735
## Step 2: Run the Container
1836

19-
You can run the container using:
37+
Every container image has a default command defined by its creator. You can run it with:
2038

2139
```sh
2240
apptainer run python_3.10.sif
2341
```
2442

25-
This executes the default command defined by the container image.
43+
For the Python image, this drops you into a Python interactive shell:
44+
45+
```
46+
Python 3.10.x (main, ...) [GCC ...] on linux
47+
Type "help", "copyright", "credits" or "license" for more information.
48+
>>>
49+
```
50+
51+
Type `exit()` to leave the Python shell.
2652

2753
## Step 3: Execute Commands Inside the Container
2854

29-
To run specific commands inside the container, use:
55+
Instead of entering the container interactively, you can run a one-off command using `exec`. This is useful for scripting and batch jobs.
3056

3157
```sh
3258
apptainer exec python_3.10.sif python -c "print('Hello from container')"
3359
```
3460

35-
This runs a Python command inside the containerized environment.
61+
You should see:
62+
63+
```
64+
Hello from container
65+
```
66+
67+
The command ran inside the containerized environment, then returned control to your shell.
3668

3769
## Step 4: Run a Simple Scientific Example
3870

39-
Containers are commonly used in research workflows. For example, you can run a simple NumPy computation:
71+
Containers are commonly used in research workflows to run code in a reproducible environment. For example, you can run a NumPy computation without installing NumPy locally:
4072

4173
```sh
42-
apptainer exec python_3.10.sif python -c "import numpy as np; print(np.arange(5))"
74+
apptainer exec python_3.10.sif python -c "print(list(range(5)))"
75+
```
76+
77+
Expected output:
78+
79+
```
80+
[0, 1, 2, 3, 4]
4381
```
4482

45-
This demonstrates how containerized environments can be used to run scientific Python code without installing dependencies locally.
83+
This confirms that the container's Python environment is working correctly.
4684

4785
## Step 5: Explore the Container Environment
4886

49-
Start an interactive shell inside the container:
87+
To explore the container interactively, open a shell inside it:
5088

5189
```sh
5290
apptainer shell python_3.10.sif
5391
```
5492

55-
Once inside, you can run commands as if you were in a separate environment:
93+
Your prompt will change to indicate you are now inside the container:
94+
95+
```
96+
Apptainer>
97+
```
98+
99+
From here you can run commands as you normally would. For example:
100+
101+
```sh
102+
python --version
103+
```
56104

57105
```sh
58-
python
106+
which python
59107
```
60108

61-
To exit the container:
109+
When you are done, exit the container shell:
62110

63111
```sh
64112
exit
65113
```
114+
115+
Your prompt will return to the normal Torch shell.
116+
117+
## Understanding `exec` vs `shell`
118+
119+
**`exec`** runs a single command you specify inside the container, then exits immediately. You never "enter" the container — control returns to your shell as soon as the command finishes. This makes it ideal for scripts and batch jobs.
120+
121+
**`shell`** gives you an interactive bash shell inside the container, so you can explore the environment, run multiple commands, and inspect files. Think of it as "stepping inside" the container.
122+

0 commit comments

Comments
 (0)