Using Containers
You can interact with SIF containers in different ways. The following sections show how to interact with images.
For demonstration, a SIF container ubuntu.sif
that was built in Building Containers page will be used.
For more information, refer to the Apptainer Documentation.
Running Containers
The %runscripts
section of a def file is user defined script that defines the actions a container should perform when you run it. For example the %runscripts
section of the def file ubuntu.def
looks like:
%runscript
python /opt/sort.py
This will be triggered when the run
command is used. For example, to run the ubuntu.sif
container, run:
apptainer run ubuntu.sif
This will run the lines specified in the runscript section python /opt/sort.py
.
Output:
[0, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
The output shows the output of the python script sort.py
.
If the runscript accepts arguments, you can pass arguments to the run command. |
You can also run SIF containers as an executable file. This behaves as the run
command. For example, to run the ubuntu.sif
container, you simply run:
./ubuntu.sif
The output is the same of the run command.
Executing Containers
To execute a custom command within a SIF container, you can use the exec
command. For example, to execute the command echo "Hello Discovery!"
using the ubuntu.sif
container, run:
apptainer exec ubuntu.sif echo "Hello Discovery"
Output:
Hello Discovery!
Shell
Apptainer allows you to spawn a new shell within your container and interact with it as a small virtual machine. This can be done using the shell
command.
$ apptainer shell ubuntu.sif
Apptainer>
This shows that you’re inside the container and you can run shell commands.
Apptainer> uname
Linux
SLURM Submission Script
You can also integrate the containers into a SLURM submission script. For more information, refer to the Slurm Commands page. For example, to submit a job that runs the ubuntu.sif
container, you need first to create a job script, named sub_container.sh
with the following content.
#!/bin/bash
## Slurm Directives
#SBATCH --job-name=test_container
#SBATCH --output=test_container-%j.out
#SBATCH --ntasks=2
#SBATCH --cpus-per-task=1
#SBATCH --mem-per-cpu=5G
#SBATCH -p normal
#SBATCH --time 00:10:00
## Load modules
## Run the program using use 'srun'.
srun apptainer run ubuntu.sif
Here, 1 CPU with 5 G memory per CPU and 10 minutes of Walltime was requested for the task (Job steps). The container will run only once (`--ntasks=1'). If the --ntasks is set to two, this means that the python program will be executed twice.
To submit the created job, you simply run:
sbatch sub_container.sh
After the job is complete, you can check the output file, which shows that the ubuntu.sif
container run once.
$ cat test_container_-54391.out
[0, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]