Apptainer tutorial, part 2¶
In this tutorial you will learn
- How to access host files from inside a container
- How to pass environment variables to a container
- How to find programs inside a container
This tutorial continues from part 1 and uses the same
tutorial.sif
container image. Run the commands in the directory where you downloaded it.
Using files¶
Apptainer containers have their own internal file system that is separated
from the host file system.
- The internal file system is always read-only when the container is run with normal user privileges.
In most real use cases you might want to access the host file system to read
and write files.
- To do this, you must bind a writable directory on the host file system to the internal file system of the container.
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.
Try it out:
-
To run these exercises on Roihu, use
sinteractiveor open a compute node shell in the Roihu web interface: -
Try listing the contents of your project's
/projappldirectory (edit the path as needed) from inside the container without--bind: -
The container cannot see the host directory, so you will get a
No such file or directoryerror. -
Try binding the host directory
/projapplto the directory/projapplinside the container: -
This time, the host directory is linked to the container directory and the command shows what the container sees inside
/projappl.If the path is the same in both the host and the container, you can simplify the command a bit. This does the same as the above command:
apptainer exec --bind /projappl tutorial.sif ls /projappl/<project> # replace <project> with your CSC project, e.g. project_2001234You can use
--bindto let the container find, for example, input data or configuration files in a certain directory. -
Bind a host directory in
/projapplto a directory called/configinside the container:apptainer exec --bind /projappl/<project>:/config tutorial.sif ls /config # replace <project> with your CSC project, e.g. project_2001234You can use
--bind="$(csc-common-bind)"to automatically take care of most common bind use cases.
Environment variables¶
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.
-
Set some test variables:
-
Compare the outputs of:
env | grep TEST apptainer exec tutorial.sif env | grep TEST apptainer exec --cleanenv tutorial.sif env | grep TEST- The
envcommand lists all set environment variables. - The first command is run on the host and we see
$TEST1and$APPTAINERENV_TEST2. - The second command is run inside the container and we see
$TEST1(inherited from the host) and$TEST2(specifically set inside the container by setting$APPTAINERENV_TEST2on the host). - The third command is also run inside the container, but this time we
omitted the host environment variables so we only see
$TEST2.
- The
-
Note that any command-line variables on the host are substituted by their values when passed to the container:
- The first line prints the value set on the host.
- The second line results in an empty output because a variable called
$TEST2has not been set on the host. It wasAPPTAINERENV_TEST2="value2", remember?
-
If you need to pass environment variables to a container, in most cases it is easiest just to set them on the host. If this is not possible, you need to make sure that variable names instead of their values are passed on to the container, e.g.:
In this example we run the
bashshell inside the container and use the-coption to give commands to run as a string. Since the string is enclosed in single quotes, any variables are passed literally instead of being substituted for their values.
Exploring containers¶
Our test container includes the program
hello2, but it has not been added
to the $PATH variable.
-
One way to find it is to try running
findinside the container: -
You can now run it by providing the full path:
-
Or you could add it to
$PATHinside the container:
If you can't locate the desired binary with
find, you can always use
apptainer shell to explore the container.
More information¶
This tutorial is meant as a brief introduction to get you started.
When searching online for instructions, make sure that the instructions
are for the same version of Apptainer as you are using. There have been some
command syntax changes etc. between versions, so older instructions may not
work as is. Also note that Apptainer was formerly known as Singularity.
For more detailed instructions, see the official
Apptainer documentation.