|
2 | 2 |
|
3 | 3 | 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. |
4 | 4 |
|
| 5 | +For background on what Apptainer is and why we use it on Torch, see [Custom Applications with Containers](../containers/). |
| 6 | + |
5 | 7 | ## Step 1: Pull a Container Image |
6 | 8 |
|
7 | 9 | Make sure you are on a compute node before proceeding, as running containers on login nodes is not recommended. |
8 | 10 |
|
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. |
10 | 12 |
|
11 | 13 | ```sh |
12 | 14 | apptainer pull docker://python:3.10 |
13 | 15 | ``` |
14 | 16 |
|
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 | +``` |
16 | 34 |
|
17 | 35 | ## Step 2: Run the Container |
18 | 36 |
|
19 | | -You can run the container using: |
| 37 | +Every container image has a default command defined by its creator. You can run it with: |
20 | 38 |
|
21 | 39 | ```sh |
22 | 40 | apptainer run python_3.10.sif |
23 | 41 | ``` |
24 | 42 |
|
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. |
26 | 52 |
|
27 | 53 | ## Step 3: Execute Commands Inside the Container |
28 | 54 |
|
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. |
30 | 56 |
|
31 | 57 | ```sh |
32 | 58 | apptainer exec python_3.10.sif python -c "print('Hello from container')" |
33 | 59 | ``` |
34 | 60 |
|
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. |
36 | 68 |
|
37 | 69 | ## Step 4: Run a Simple Scientific Example |
38 | 70 |
|
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: |
40 | 72 |
|
41 | 73 | ```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] |
43 | 81 | ``` |
44 | 82 |
|
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. |
46 | 84 |
|
47 | 85 | ## Step 5: Explore the Container Environment |
48 | 86 |
|
49 | | -Start an interactive shell inside the container: |
| 87 | +To explore the container interactively, open a shell inside it: |
50 | 88 |
|
51 | 89 | ```sh |
52 | 90 | apptainer shell python_3.10.sif |
53 | 91 | ``` |
54 | 92 |
|
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 | +``` |
56 | 104 |
|
57 | 105 | ```sh |
58 | | -python |
| 106 | +which python |
59 | 107 | ``` |
60 | 108 |
|
61 | | -To exit the container: |
| 109 | +When you are done, exit the container shell: |
62 | 110 |
|
63 | 111 | ```sh |
64 | 112 | exit |
65 | 113 | ``` |
| 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