|
| 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 | +To run a specific command, use `apptainer exec`. |
| 43 | + |
| 44 | +## Running Specific Commands Within a Container |
| 45 | + |
| 46 | +`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 | +## Opening an Interactive Shell Within a Container |
| 69 | + |
| 70 | +There are many reasons why you might want to use a container interactively. |
| 71 | +- debugging (software, bind mounts, hardware integrations, etc.) |
| 72 | +- testing software upgrades |
| 73 | +- rapid Software Prototyping |
| 74 | +- data exploration |
| 75 | + |
| 76 | +Apptainer provides the `apptainer shell` command for this purpose. |
| 77 | + |
| 78 | +Launch a shell inside the Ubuntu container: |
| 79 | + |
| 80 | +```bash |
| 81 | +apptainer shell /share/apps/images/ubuntu-24.04.3.sif |
| 82 | +``` |
| 83 | + |
| 84 | +You should see a prompt similar to: |
| 85 | + |
| 86 | +```text |
| 87 | +Singularity> |
| 88 | +``` |
| 89 | + |
| 90 | +You can now run commands inside the container: |
| 91 | + |
| 92 | +```bash |
| 93 | +whoami |
| 94 | +pwd |
| 95 | +cat /etc/os-release |
| 96 | +``` |
| 97 | + |
| 98 | +Example output: |
| 99 | + |
| 100 | +```text |
| 101 | +PRETTY_NAME="Ubuntu 24.04.3 LTS" |
| 102 | +NAME="Ubuntu" |
| 103 | +VERSION_ID="24.04" |
| 104 | +... |
| 105 | +``` |
| 106 | + |
| 107 | +Notice that the prompt changes to indicate that you are working inside the container environment. |
| 108 | + |
| 109 | +When you are finished, leave the container with: |
| 110 | + |
| 111 | +```bash |
| 112 | +exit |
| 113 | +``` |
| 114 | + |
| 115 | +This returns you to your normal shell on Torch. |
0 commit comments