π¬ Apptainer containers have their own internal file system that is separated from the host file system.
π In most real use cases you might want to access the host file system to read and write files.
π‘ This is done using the command line argument --bind
(or -B
). The basic syntax is --bind /path/inside/host:/path/inside/container
.
π Some remarks:
β The bind path does not need to exist inside the container β it is created if necessary.
β More than one bind pair can be specified.
β The option is available for all run methods described in the previous tutorial.
sinteractive
or open a compute node shell in the Puhti web interface:sinteractive --account <project> # replace <project> with your CSC project, e.g. project_2001234
--bind
:export SCRATCH=/scratch/<project>/$USER # replace <project> with your CSC project, e.g. project_2001234
apptainer exec tutorial.sif ls $SCRATCH
No such file or directory
error./scratch
to the directory /scratch
inside the container:apptainer exec --bind $SCRATCH:/scratch tutorial.sif ls /scratch
# or
apptainer exec --bind /scratch:/scratch tutorial.sif ls $SCRATCH
/scratch
.π‘ You can use --bind
to set the container, for example, to find input data or configuration files from a certain directory.
$SCRATCH
to a directory called /input
:apptainer exec --bind $SCRATCH:/input tutorial.sif ls /input
apptainer_wrapper
apptainer_wrapper
, it will automatically take care of the most common bind use cases.$SING_IMAGE
environment variable to point to the correct Apptainer image file:export SING_IMAGE=$PWD/tutorial.sif
apptainer_wrapper exec ls $SCRATCH
π‘ Note that the image file name is not needed in the apptainer_wrapper
command since $SING_IMAGE
is set.
βΌοΈ Since some modules set $SING_IMAGE
when loaded, it is a good idea to start with module purge
to make sure the correct image is used if you plan to use apptainer_wrapper
.
π¬ Some software may require environment variables to be set, e.g., to point to some reference data or a configuration file.
π¬ Most environment variables set on the host are inherited by the container.
βπ» Sometimes this may be undesired, in which case the command line option --cleanenv
can be used to prevent the host environment from being inherited by the container.
π¬ To set an environment variable specifically inside the container, you can set an environment variable $APPTAINERENV_XXX
(where XXX
is the variable name) on the host before invoking the container.
export TEST1="value1"
export APPTAINERENV_TEST2="value2"
env | grep TEST
apptainer exec tutorial.sif env | grep TEST
apptainer exec --cleanenv tutorial.sif env | grep TEST
$TEST1
and $APPTAINERENV_TEST2
.$TEST1
(inherited from the host) and $TEST2
(specifically set inside the container by setting $APPTAINERENV_TEST2
on the host).$TEST2
.apptainer exec tutorial.sif echo $TEST1
apptainer exec tutorial.sif echo $TEST2
$TEST2
has not been set on host. It was APPTAINERENV_TEST2="value2"
, remember?π¬ Our test container includes the program hello2
, but it has not been added to the $PATH
variable.
find
inside the container:apptainer exec tutorial.sif find / -type f -name "hello2" 2>/dev/null
apptainer exec tutorial.sif /found/me/hello2
$PATH
inside the container:export APPTAINERENV_PREPEND_PATH=/found/me
apptainer exec tutorial.sif hello2
π‘ If you canβt locate the desired binary with find
, you can always use apptainer shell
to explore the container.
π¬ This tutorial is meant as a brief introduction to get you started.
βπ» When searching online for instructions, pay attention that the instructions are for the same version of Apptainer as you are using. There has been some command syntax changes etc. between versions so older instructions may not work as copy/paste. Also note that Apptainer is formerly known as Singularity.
π‘ For more detailed instructions, see the Apptainer documentation.