Apptainer: Difference between revisions

From Alliance Doc
Jump to navigation Jump to search
No edit summary
(Marked this version for translation)
 
(143 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{Draft}}
<languages />
=Notices=
<translate>
=Forewords= <!--T:1-->


This page is '''draft work-in-progress'''. At the time of writing (August 2022) Apptainer version 1.1 is in release candidate status and the full release may well induce further updates to this page. (This page's current content is correct for Apptainer version 1.0.)
==Official Apptainer documentation== <!--T:2-->


==Official Apptainer Documentation==
<!--T:3-->
This page does not describe all features of Apptainer and does not replace [http://apptainer.org/docs Apptainer's official documentation].  It summarizes basic use, documents some aspects of using Apptainer on Alliance systems, and provides some relevant examples. We recommend you read the official Apptainer documentation concerning the features of Apptainer you are using.


This page is neither exhaustive nor all-features complete and does not replace [http://apptainer.org/docs|Apptainer's official documentation], rather, it summarizes basic use, documents some aspects of using Apptainer on Alliance systems, and provides some examples relevant in using Apptainer on Alliance systems. We recommend all users read the official Apptainer documentation concerning the features of Apptainer they are using.
<!--T:4-->
Should you wish to install Apptainer on your own system, [http://apptainer.org/docs/user/main/quick_start.html#quick-installation instructions appear here]. If you are using a recent Windows system, [https://learn.microsoft.com/en-ca/windows/wsl/install install WSL] first, then within such, install Apptainer. If you are using a Mac, install a Linux distribution in a virtual machine on your computer first, then install Apptainer within such.


==If Currently Using Singularity==
==If you are currently using Singularity== <!--T:5-->


We strongly recommend that you start using Apptainer instead of Singularity. SingularityCE (up to v3.9.5) was adopted by The Linux Foundation and renamed to Apptainer with these changes:
<!--T:6-->
We strongly recommend that you use Apptainer instead of Singularity. The Linux Foundation has adopted SingularityCE (up to v3.9.5) and renamed it Apptainer with these changes:


* added '''experimental''' support for [https://dmtcp.sourceforge.io/ DMTCP checkpointing],
<!--T:7-->
** NOTE: Support for such is not in Singularity.
* Added support for [https://dmtcp.sourceforge.io/ DMTCP checkpointing].
* removed support for the <code>--nvccli</code> command line option,
* Removed support for the <code>--nvccli</code> command line option.
* removed support for <code>apptainer build --remote</code>,
* Removed support for <code>apptainer build --remote</code>.
* removed support the SylabsCloud remote endpoint replacing it with a DefaultRemote endpoint with no defined server for <code>library://</code>,
* Removed support for the SylabsCloud remote endpoint, replacing it with a DefaultRemote endpoint with no defined server for <code>library://</code>.
** NOTE: If the SylabsCloud remote is needed, the [https://apptainer.org/docs/user/1.0/endpoint.html#restoring-pre-apptainer-library-behavior previous default can be restored].
** If you need the SylabsCloud remote, the [https://apptainer.org/docs/user/1.0/endpoint.html#restoring-pre-apptainer-library-behavior previous default can be restored].
* renamed all executable names, paths, etc. having <code>singularity</code> in their names to have <code>apptainer</code> in them,
* Renamed all executable names, paths, etc. having <code>singularity</code> in their names to have <code>apptainer</code> in them.
** e.g., instead of using the <code>singularity</code> command one uses the <code>apptainer</code> command
** E.g., instead of using the <code>singularity</code> command one uses the <code>apptainer</code> command.
** e.g., the <code>~/.singularity</code> directory is now <code>~/.apptainer</code>
** E.g., the <code>~/.singularity</code> directory is now <code>~/.apptainer</code>.
* renamed all environment variables having <code>SINGULARITY</code> in their names to have <code>APPTAINER</code> in them,
* Renamed all environment variables having <code>SINGULARITY</code> in their names to have <code>APPTAINER</code> in them.


Should you need to port scripts, etc. to Apptainer, know Apptainer version 1 is backwards compatible with Singularity so switching to Apptainer can be done incrementally.
<!--T:8-->
Should you need to port scripts to Apptainer, know Apptainer version 1 is backwards compatible with Singularity so switching to Apptainer can be done incrementally.


=Using Apptainer=
==Other Linux container technologies== <!--T:9-->


In order to use Apptainer one must first already have a container image, e.g., a <code>.sif</code> file or a "sandbox" directory created previously. If you don't already have a container image, see the section on [[#Building_an_Apptainer_Container/Image|building an image]] below.
<!--T:10-->
HPC clusters typically use Apptainer. Many users ask about other Linux container technologies so here are some with some comments:
* [https://podman.io/ Podman]
** Supports rootless (i.e., normal) container use, etc. similar to Apptainer.
** Is available as a package in rpm-supporting (and some other) Linux distributions.
** While Podman is a Linux container technology, there are [https://github.com/containers/podman/blob/main/docs/tutorials/mac_win_client.md instructions for installing Podman on Windows/Mac machines].
** Podman version 4 supports Apptainer .SIF files.
* [https://www.docker.com/ Docker]
** Using Docker on a multiuser cluster creates security risks, therefore we do not make Docker available on our HPC clusters.
** You can install and use Docker on your own computer and use it to create an Apptainer image, which can then be uploaded to an HPC cluster as outlined in <b>[[#Creating_an_Apptainer_container_from_a_Dockerfile|this section]]</b> later on this page.


==Loading an Apptainer Module==
==Other items== <!--T:11-->
===General===
* In order to use Apptainer you must have a container <b>image</b>, e.g., a <code>.sif</code> file or a "sandbox" directory created previously. If you don't already have an image or a sandbox, see the section on <b>[[Apptainer#Building_an_Apptainer_image|building an image]]</b> below.
* While Apptainer is installed and available for use, using Apptainer will require you to install and/or build all software you will need to make use of in your container. In many instances, <b>[[Available_software|we already have such software installed on our clusters]]</b> so there is often no need to create a container with the same installed in it.


===<code>sudo</code>=== <!--T:12-->
Many users ask about <code>sudo</code> since documentation and websites often discuss using <code>sudo</code>. Know the ability to use <code>sudo</code> to obtain superuser/root permissions is not available on our clusters. Should you require using <code>sudo</code>, consider the following options:
<!--T:13-->
* Install Linux, Apptainer, and <code>sudo</code> in a virtual machine on a system you control so you will be able to have <code>sudo</code> access within such. Build your image(s) on that machine and upload them in order to use them on Alliance systems.
* If appropriate, [[Technical Support|submit a ticket]] asking if Alliance staff would be able to help build the image(s) required needing <code>sudo</code>. This may or may not be possible, but feel free to ask in a ticket if what you wish to achieve is beyond your means. Additionally, we may respond with other ways to achieve such which may or may not involve Apptainer.
* Apptainer version 1.1.x and newer has improved support for users using <code>--fakeroot</code> implicitly and explicitly so some things may be possible that were not with Apptainer version 1.0 and Singularity. This includes being able to build some images from <code>.def</code> definition files and building some images without needing to use <code>sudo</code>. That said, not all images will be able to be built without needing to use <code>sudo</code> or superuser/root.
===Building images or overlays=== <!--T:14-->
Should you need to build your own container image(s) or overlay(s), be aware of the following:
* Avoid building a sandbox image using <code>--fakeroot</code> on networked filesystem(s): [https://apptainer.org/docs/admin/main/installation.html#fakeroot-with-uid-gid-mapping-on-network-filesystems link to Apptainer documentation].
* Explicitly set <code>APPTAINER_CACHEDIR</code> to a non-networked filesystem location before using Apptainer: [https://apptainer.org/docs/admin/main/installation.html#apptainer-cache-atomic-rename link to Apptainer documentation].
* Explicitly set <code>APPTAINER_TMPDIR</code> to a non-Lustre/GPFS filesystem location before using Apptainer: [https://apptainer.org/docs/admin/main/installation.html#lustre-gpfs link to Apptainer documentation].
* Avoid using Lustre/GPFS filesystems as they don't have the feature set required to properly support building Apptainer containers (including <code>--fakeroot</code>): [https://apptainer.org/docs/admin/main/installation.html#lustre-gpfs link to Apptainer documentation].
=Loading an Apptainer module= <!--T:15-->
In order to use the default version of Apptainer available run:
In order to use the default version of Apptainer available run:
<source lang="console">$ module load apptainer</source>
<source lang="console">$ module load apptainer</source>


<!--T:16-->
To see the available versions of Apptainer that can be loaded run:
To see the available versions of Apptainer that can be loaded run:
<source lang="console">$ module spider apptainer</source>
<source lang="console">$ module spider apptainer</source>


==Running Programs Within a Container==
=Running programs within a container= <!--T:17-->


===Important Items===
==Important command line options== <!--T:18-->
====<code>sudo</code>====


Many users ask about <code>sudo</code> since documentation and web sites often discuss using <code>sudo</code>. Know the ability to use <code>sudo</code> to obtain root permissions is not available on our clusters. Should you require using <code>sudo</code>, consider the following options:
<!--T:19-->
Software that is run inside a container is in an environment that uses different libraries and tools than what is installed on the host system. It is, therefore, important to run programs within containers by <b>not</b> using any environment settings or software defined outside of the container. Unfortunately, by default, Apptainer will run adopting the shell environment of the host and this can result in issues when running programs. To avoid such issues when using <code>apptainer run</code>, <code>apptainer shell</code>, <code>apptainer exec</code>, and/or <code>apptainer instance</code>, use one of these options:


* Install Linux, Apptainer, and <code>sudo</code> in a virtual machine on a system you control so you will be able to have <code>sudo</code> access within such. Build your image(s) on that machine and upload them in order to use them on Alliance systems.
<!--T:20-->
* If appropriate, [[Technical Support|submit a ticket]] asking if Alliance staff would be able to help build the image(s), etc. required needing <code>sudo</code>. (Understand that this may or may not be done/possible --but feel free to ask such in a ticket if what you wish to achieve is beyond your means. Additionally, we may respond with other ways to achieve such with may or may not involve Apptainer.)
{| class="wikitable"
 
|+Apptainer environment command line options
NOTE: Apptainer version 1.1, which as of August 2022 has release candidate status only, may well allow users to easily use the <code>--fakeroot</code> option with various Apptainer commands. Such should make it possible to do various things without <code>sudo</code>. If so, after Apptainer version 1.1 has been released, this web page will be updated concerning such.
 
====Important Command Line Options====
 
Software that is run inside a container runs in a different environment, libraries, and tools than what is installed on the host system. It is, therefore, wise to run programs within containers by '''not''' importing any environment settings or software defined outside of the container. By default Apptainer will run adopting the shell environment of the host but this can result in issues when running programs inside the container. To work around this when using <code>apptainer run</code>, <code>apptainer shell</code>, <code>apptainer exec</code>, and </code>apptainer instance</code>, consider using one of these options (with more preference to those options listed above other options):


{| class="wikitable"
<!--T:140-->
|+Apptainer Environment Command Line Options
|-
|-
! scope="col" |Option !! scope="col" | Description
| <code>-C</code> || Isolates the running container from <b>all filesystems</b> as well as the parent PID, IPC, and environment. Using this option will require [[#Bind_mounts|using bind mounts]] if access to filesystems outside of the container is needed.
|-
|-
| <code>-C</code> || Isolates the running container from all file systems as well as the parent PID, IPC, and environment. Using this option will require [[#Bind_Mounts|using bind mounts]] if access to filesystems outside of the container is needed.
| <code>-c</code> || Isolates the running container from <b>most filesystems</b> only using a minimal <code>/dev</code>, an empty <code>/tmp</code> directory, and an empty <code>/home</code> directory. Using this option will require [[#Bind_mounts|using bind mounts]] if access to filesystems outside of the container is needed.
|-
|-
| <code>-c</code> || Isolates the running container from most file systems only using a minimal <code>/dev</code>, an empty <code>/tmp</code> directory, and an empty <code>/home</code> directory. Using this option will require [[#Bind_Mounts|using bind mounts]] if access to filesystems outside of the container is needed.
| <code>-e</code> || Cleans (some) shell environment variables before running container commands and applies settings for increased OCI/Docker compatibility. Using this option also implies the use of options <code>--containall</code>, <code>--no-init</code>, <code>--no-umask</code>, and <code>--writable-tmpfs</code>.
|-
| <code>-e</code> || Cleans (some) shell environment variables before running container commands and applies settings for increased OCI/Docker compatibility. Using this option also implies the use of these options: <code>--containall</code>, <code>--no-init</code>, <code>--no-umask</code>, <code>--writable-tmpfs</code>.
|-
| &nbsp; || When no options are used the environment variables from the parent shell exist as-is inside the container (which can cause issues to occur) and (virtually) all filesystems are also present inside the container.
|}
|}


There is another important option, <code>-W</code> or <code>--workdir</code>, one should consider using. On Alliance clusters and on most Linux systems, <code>/tmp</code> and similar filesystems use RAM --not disk space. Since jobs typically run on our clusters with limited RAM amounts, this can easily result in jobs getting killed because they consume too much RAM relative to what was requested for the job. A suitable work-around for this is to tell Apptainer to use actual disk space location for its "workdir". This is done by passing the <code>-W</code> option followed by a path to a disk space location where Apptainer can read/write temporary files, etc. For example, suppose one wanted to run a command <code>myprogram</code> in a using an Apptainer container image called <code>myimage.sif</code> with its "workdir" set to <code>$HOME/aworkdir</code> in the filesystem:
<!--T:21-->
Another important option is the <code>-W</code> or <code>--workdir</code> option. On our clusters and on most Linux systems, <code>/tmp</code> and similar filesystems use RAM, not disk space. Since jobs typically run on our clusters with limited RAM amounts, this can result in jobs getting killed because they consume too much RAM relative to what was requested for the job. A suitable workaround for this is to tell Apptainer to use a real disk location for its working directory (<code>workdir</code>). This is done by passing the <code>-W</code> option followed by a path to a disk location where Apptainer can read/write temporary files For example, to run a command called <code>myprogram</code> in an Apptainer container image called <code>myimage.sif</code> with its working directory set to <code>/path/to/a/workdir</code> in the filesystem, you would use


<source lang="console">$ mkdir -p $HOME/aworkdir
<!--T:22-->
$ apptainer run -C -B /home -W $HOME/aworkdir myimage.sif myprogram</source>
<source lang="console">apptainer run -C -B /project -W /path/to/a/workdir myimage.sif myprogram</source>


<!--T:23-->
where:
where:
* The workdir directory can be removed if there are no live containers using it.
* <code>workdir</code> can be removed if there are no live containers using it.
* When using Apptainer in an <code>salloc</code> or an <code>sbatch</code> job, use <code>${SLURM_TMPDIR}</code> for the "workdir" location, e.g., <code>-W ${SLURM_TMPDIR}</code>.
* When using Apptainer in an <code>salloc</code>, in an <code>sbatch</code> job, or when using [[JupyterHub]] on our clusters, use <code>${SLURM_TMPDIR}</code> for the working directory's location, e.g., <code>-W ${SLURM_TMPDIR}</code>.
** ASIDE: One should '''not''' be running programs (including using Apptainer) on a login node: use an interactive <code>salloc</code> job.
** ASIDE: One should <b>not</b> be running programs (including Apptainer) on a login node.  Use an interactive <code>salloc</code> job.
* When using bind mounts, see the [[Bind_Mounts|section on bind mounts]] below since not all Alliance clusters are the same concerning the exact bind mounts that are needed to access <code>/home</code>, <code>/project</code>, and <code>/scratch</code>.
* When using bind mounts, see the [[#Bind_Mounts|section on bind mounts]] below since not all of our  clusters are the same concerning the exact bind mounts needed to access <code>/home</code>, <code>/project</code>, and <code>/scratch</code>.


====Using GPUs====
==Using GPUs== <!--T:24-->


<!--T:25-->
When running software inside a container that requires the use of GPUs it is important to do the following:
When running software inside a container that requires the use of GPUs it is important to do the following:
* Ensure that you pass the <code>--nv</code> (for NVIDIA hardware) and <code>--rocm</code> (for AMD hardware) to Apptainer commands.
* Ensure that you pass the <code>--nv</code> (for NVIDIA hardware) and <code>--rocm</code> (for AMD hardware) to Apptainer commands.
** These options will ensure the appropriate <code>/dev</code> entries are bind mounted inside the container.
** These options will ensure the appropriate <code>/dev</code> entries are bind mounted inside the container.
** These options will locate and bind GPU-related libraries on the host (e.g., so such becomes bind-mounted inside the container) as well as setting the <code>LD_LIBRARY_PATH</code> environment variable to enable the aforementioned libraries will work inside the container.
** These options will locate and bind GPU-related libraries on the host, as well as set the <code>LD_LIBRARY_PATH</code> environment variable to enable the aforementioned libraries to work inside the container.
* Ensure the application using the GPU inside the container was properly compiled to use the GPU and its libraries.
* Ensure the application using the GPU inside the container was properly compiled to use the GPU and its libraries.
* When needing to use OpenCL inside the container, besides using the aforementioned options use the following bind mount: <code>--bind /etc/OpenCL</code>.
* When needing to use OpenCL inside the container, besides using the aforementioned options, use the following bind mount: <code>-B /etc/OpenCL</code>.


An example of [[#Using_NVIDIA_GPUs_Within_an_Apptainer_Container|using NVIDIA GPUs within an apptainer container]] appears later on this page.
<!--T:26-->
An example of [[#Using_NVIDIA_GPUs_Within_an_Apptainer_Container|using NVIDIA GPUs within an Apptainer container]] appears later on this page.


====Using MPI Programs====
==Using MPI programs== <!--T:27-->


If you want to run MPI programs inside a container there are things that need to be done in the host environment in order for such to work. Please see the [[#Running_MPI_Programs_Inside_an_Apptainer Container|Running MPI Programs section below]] for an example of how to run MPI programs inside a container. The official Apptainer documentation has more information concerning how MPI programs can be run inside a container.
<!--T:28-->
If you need to run MPI programs inside a container, there are things that need to be done in the host environment in order for such to work. Please see the [[#Running_MPI_Programs_Inside_an_Apptainer Container|Running MPI Programs section below]] for an example of how to run MPI programs inside a container. The [http://apptainer.org/docs/user/main/mpi.html official Apptainer documentation] has more information concerning how MPI programs can be run inside a container.


===Container-Specific Help: <code>apptainer run-help</code>===
==Container-specific help: <code>apptainer run-help</code>== <!--T:29-->


Text to come.
<!--T:30-->
Apptainer containers built from [http://apptainer.org/docs/user/main/definition_files.html Definition files] often will have a <code>%help</code> section. To see this section run:


===Running Software (Preferred): <code>apptainer run</code>===
<!--T:31-->
apptainer run-help your-container-name.sif


Text to come.
<!--T:32-->
where:
* <code>your-container-name.sif</code> is the name of your container


===Interactively Running Software: <code>apptainer shell</code>===
<!--T:33-->
It is possible your container also has "apps" defined in it, you can get help for those apps by running:


Text to come.
<!--T:34-->
apptainer run-help --app appname your-container-name.sif


===Running Software (Basic): <code>apptainer exec</code>===
<!--T:35-->
where:
* <code>appname</code> is the name of the app
* <code>your-container-name.sif</code> is the name of your container


Text to come.
<!--T:36-->
To see a list of apps installed in your container (if there are any), run:


===Running Daemons: <code>apptainer instance</code>===
<!--T:37-->
apptainer inspect --list-apps your-container-name.sif


Text to come.
<!--T:38-->
where:
* <code>your-container-name.sif</code> is the name of your container
 
==Running software: <code>apptainer run</code> or <code>apptainer exec</code>== <!--T:39-->
 
<!--T:40-->
When the <code>apptainer run</code> command launches the container, it first runs the <code>%runscript</code> defined for that container (if there is one), and then runs the specific command you specified. <br>
The <code>apptainer exec</code> command will not run the <code>%runscript</code>, even if one is defined in the container.


==Bind Mounts and Persistent Overlays==
<!--T:41-->
We suggest that you aways use <code>apptainer run</code>.


Text to come.
<!--T:42-->
For example, suppose you want to run the <code>g++</code> compiler inside your container to compile a C++ program called <code>myprog.cpp</code> and then run that program. To do this, you might use this command:


===Bind Mounts===
<!--T:43-->
apptainer run your-container-name.sif g++ -O2 -march=broadwell ./myprog.cpp
apptainer run your-container-name.sif ./a.out


Text to come.
<!--T:44-->
where:
* <code>your-container-name.sif</code> is the name of your SIF file
* <code>g++ -O2 -march=broadwell ./myprog.cpp</code> is the command you want to run inside the container


===Persistent Overlays===
<!--T:45-->
On our clusters, you will want to use a number of additional options (that appear after <code>run</code>, but before <code>your-container-name.sif</code>). These options include <code>-C</code>, <code>-c</code>, <code>-e</code>, <code>-W</code> as well as various bind mount options to make your disk space available to the programs that run in your container. For example:


Text to come.
  <!--T:46-->
apptainer run -C -W $SLURM_TMPDIR -B /project -B /scratch your-container-name.sif g++ -O2 -march=broadwell ./myprog.cpp
  apptainer run -C -W $SLURM_TMPDIR -B /project -B /scratch ./a.out


==Building an Apptainer Container/Image==
<!--T:47-->
For more information on these options see the following sections on this page:


Apptainer "images" can be created in the following formats:
<!--T:48-->
* [[#Important_command_line_options|Important command line options]]
* [[#Using_GPUs|Using GPUs]]
* [[#Bind_mounts_and_persistent_overlays|Bind mounts and persistent overlays]]


* as an <code>SIF</code> file, or,
<!--T:49-->
* as a "sandbox" directory.
as well as the [http://apptainer.org/docs/user/main/index.html official Apptainer documentation].


<code>SIF</code> files internally can contain multiple parts where each part is typically a squashfs filesystem (which are read-only and compressed). It is possible for <code>SIF</code> files to contain read-write filesystems and overlay images as well --but such is beyond the scope of this page: see the official Apptainer documentation on how to do such. Unless more advanced methods of building an "image" were used, the Apptainer <code>build</code> command produces a <code>SIF</code> file with a read-only squashfs filesystem when building images. (This is the preferred option since the resulting image remains as-is since it is read-only, and, the image is much smaller since it is compressed. Know that disk reads from that image are done very quickly.)
==Interactively running software: <code>apptainer shell</code>== <!--T:50-->


A "sandbox" directory is a normal directory in the filesystem that starts out as empty and as Apptainer builds the image it adds the files, etc. needed in the image to that directory. The contents of a "sandbox" directory should only be accessed, updated, etc. through the use of Apptainer. One might need to use a "sandbox" directory in situations where one needs to have read-write access to the image itself in order to be able to update the container image. That said, if updates are infrequent, it is typically easier and better to use an <code>SIF</code> and when updates need to be done, build a sandbox image from the <code>SIF</code> file, make the required changes, and then build a new <code>SIF</code> file, e.g.,
<!--T:51-->
The <code>apptainer run</code>, <code>apptainer exec</code>, and <code>apptainer instance</code> commands run the programs provided immediately which makes them excellent for use in BASH and SLURM job scripts. There are times when one needs to interactively do work inside a container. To run commands interactively while remaining inside a container, use the <code>apptainer shell</code> command instead.


<!--T:52-->
For example, to run commands interactively in a container one first invokes the <code>apptainer shell</code> command, e.g.,
<!--T:53-->
apptainer shell your-container-name.sif
<!--T:54-->
where:
* <code>your-container-name.sif</code> is the name of your SIF file
<!--T:55-->
Once the container starts, you will see an <code>Apptainer&gt;</code> prompt (or <code>Singularity&gt;</code> prompt if using Singularity). At this prompt you can run desired shell commands in the container. When done, type <code>exit</code> and hit the Enter/Return key to exit the container.
<!--T:56-->
On our clusters, you will want to use a number of additional options (that appear after <code>run</code> and before <code>your-container-name.sif</code>). These options will include <code>-C</code>, <code>-c</code>, <code>-e</code>, <code>-W</code> as well as various bind mount options to make your disk space available to the programs that run in your container. For example:
  <!--T:57-->
apptainer shell -C -W $SLURM_TMPDIR -B /home:/cluster_home -B /project -B /scratch your-container-name.sif
<!--T:58-->
For more information on these options see the following sections on this page:
<!--T:59-->
* [[#Important_Command_Line_Options|Important Command Line Options]]
* [[#Using_GPUs|Using GPUs]]
* [[#Bind_Mounts_and_Persistent_Overlays|Bind Mounts and Persistent Overlays]]
<!--T:60-->
as well as the [http://apptainer.org/docs/user/main/index.html official Apptainer documentation].
<!--T:61-->
<b>IMPORTANT:</b>In addition to choose to use the above options, if you are making use of a persistent overlay image (as a separate file or contained within the SIF file) and want changes to be written to that image, it is extremely important to pass the <code>-w</code> or <code>--writable</code> option to your container. If this option is not passed to it, any changes you make to the image in the <code>apptainer shell</code> session will not be saved!
==Running daemons: <code>apptainer instance</code>== <!--T:62-->
<!--T:63-->
Apptainer has been designed to be able to properly run daemons within compute jobs on clusters. Running daemons is achieved, in part, by using <code>apptainer instance</code>. See the [http://apptainer.org/docs/user/main/running_services.html official Apptainer documentation on Running Services] for the details.
<!--T:64-->
<b>NOTE 1:</b> Don't run daemons manually without using <code>apptainer instance</code> and related commands. Apptainer works properly with other tools such as the Slurm scheduler that run on our clusters. When a job is cancelled, killed, crashes, or is otherwise finished, daemons run using <code>apptainer instance</code> will not hang or result in defunct processes. Additionally by using the <code>apptainer instance</code> command you will be able to control the daemons and programs running in the same container.
<!--T:65-->
<b>NOTE 2:</b> Daemons can only run in your job while your job is running. Should the scheduler kill your job, all daemons running in that job will also be killed. Should you need to run daemons longer than the job they were started in, you may need to also have a virtual machine running in the cloud. If so please contact [[technical support]].
==Running MPI programs== <!--T:66-->
<!--T:67-->
Running MPI programs within an Apptainer container across nodes likely will require special configuration. MPI exploits cluster interconnection hardware to communicate amongst nodes much more efficiently. Normally one does not need to worry about this since it is automatically done --except when running MPI programs across cluster nodes.
<!--T:68-->
<b>NOTE:</b> When all MPI processes are running on a single shared-memory node, there is no need to use interconnection hardware and there will be no issues running MPI programs within an Apptainer container when all MPI processes run on a single cluster node, e.g., when the slurm option <code>--nodes=1</code> is used with an <code>sbatch</code> script. Unless one <b>explicitly</b> sets the maximum number of cluster nodes used to <code>1</code>, the scheduler can choose to run an MPI program over multiple nodes. If such will run from within an Apptainer container and has not been set up to properly run, then it is possible it will fail to run.
<!--T:69-->
More in preparation.
=Bind mounts and persistent overlays= <!--T:70-->
<!--T:71-->
Often, one will want to use either or both of these features in Apptainer:
* <b>bind mounts</b>, to access disk space originating outside of the container, and,
* <b>persistent overlays</b>, to overlay a writable filesystem on an otherwise immutable (i.e., read-only) container image.
==Bind mounts== <!--T:72-->
<!--T:73-->
When Apptainer is used with the <code>-C</code> or <code>-c</code> options, one will notice that they cannot access their disk space when inside the container. The remedy for this is to explicitly bind mount the disk space they wish to access. For example, suppose a user was using <code>-C</code> like this in an <code>sbatch</code> job to use Apptainer:
<!--T:74-->
apptainer run -C -W $SLURM_TMPDIR a-container.sif wc -l ./my_data_file.txt
<!--T:75-->
where <code>./my_data_file.txt</code> is a file in the current directory on the host, i.e., the file is not stored in the container at all. Because of the <code>-C</code> option, this file will not be accessible to the <code>wc</code> program inside the container and an access error will result. The fix is to bind mount the current directory, e.g.,
<!--T:76-->
apptainer run -C -B . -W $SLURM_TMPDIR a-container.sif wc -l ./my_data_file.txt
<!--T:77-->
where <code>-B .</code> will bind mount the current directory, <code>.</code>.
<!--T:78-->
While one can have multiple bind mounts specified, it is often easier to specify the top directories of the filesystems one wishes to access. For example, on our clusters one might want to use
<!--T:79-->
apptainer run -C -B /project -B /scratch -W $SLURM_TMPDIR a-container.sif wc -l ./my_data_file.txt
<!--T:80-->
where:
* <code>-B /project</code> mounts the project filesystem
* <code>-B /scratch</code> mounts the scratch filesystem
<!--T:81-->
Doing this is especially useful when:
* you need to access others' files on your research team in other locations, and/or,
* you need to access files/directories some of which are <code>symlinks</code> to different locations that would/might otherwise be broken if you did not mount the entire filesystem.
<!--T:82-->
If using these bind mounts does not work on the cluster you are using, run this next script to obtain the bind mount options you need to pass to Apptainer for /home, /project, and /scratch on that cluster
</translate>
/home/preney/public/apptainer-scripts/get-apptainer-options.sh
<translate>
<!--T:83-->
It should be mentioned that a bind mount does not need to be in the same location inside the container: one can bind mount any file or directory to be at a different location, e.g.,
<!--T:84-->
apptainer run -C -B ./my_data_file.txt:/special/input.dat -W $SLURM_TMPDIR a-container.sif wc -l /special/input.dat
<!--T:85-->
i.e., <code>-B ./my_data_file.txt:/special/input.dat</code> bind mount maps the file <code>./my_data_file.txt</code> to be the file <code>/special/input.dat</code> inside the container and the <code>wc</code> command now processes that file. This feature can be useful when programs/scripts inside the container have hard-coded paths to files and directories that must be located in certain locations.
<!--T:141-->
If you need to bind-mount the <code>/home</code> filesystem in your container, use an alternate destination directory:
<!--T:142-->
* <code>-B /home:/cluster_home</code>
<!--T:143-->
This ensures that configuration files and programs in your home directory do not interfere with the software in your container. For example, programs in <code>$HOME/bin</code> and Python packages in <code>$HOME/.local/lib/python3.x</code> could be used instead of the corresponding files in your container if you used <code>-B /home</code>.
<!--T:86-->
Finally, <b>don't mount our CVMFS paths</b> inside your containers as this is fraught with perils and defeats many reasons to use a container. The programs that have to run inside a container need to be completely inside the container. Don't introduce even more programs inside the container if they don't need to be there.
==Persistent overlays== <!--T:87-->
<!--T:88-->
Please refer to Apptainer documentation page about [https://apptainer.org/docs/user/main/persistent_overlays.html persistent overlays].
=Building an Apptainer image= <!--T:89-->
<!--T:90-->
<b>NOTE:</b> Please note and heed the advice given in section <b>[[#Building_images_or_overlays|Building images and overlays]]</b>.
<!--T:91-->
Apptainer images can be created in the following formats:
<!--T:92-->
* as an <code>SIF</code> file, or
* as a sandbox directory.
<!--T:93-->
<b><code>SIF</code> files</b> can contain multiple parts where each part is typically a <code>squashfs</code> filesystem (which are read-only and compressed). It is possible for <code>SIF</code> files to contain read-write filesystems and overlay images as well, but such is beyond the scope of this page; see [http://apptainer.org/docs Apptainer's official documentation] on how to do such. Unless more advanced methods of building an image were used, the Apptainer <code>build</code> command produces a <code>SIF</code> file with a read-only <code>squashfs</code> filesystem when building images. This is the preferred option since the resulting image remains as-is since it is read-only, and the image is much smaller because it is compressed. Know that disk reads from that image are done very quickly.
<!--T:94-->
<b>A sandbox directory</b> is a normal directory in the filesystem that starts out as empty. As Apptainer builds the image, it adds to it the files and directories needed in the image. The contents of a sandbox directory should only be accessed or updated through the use of Apptainer. One might need to use a sandbox directory in situations where one needs to have read-write access to the image itself in order to be able to update the container image. That said, if updates are infrequent, it is typically easier and better to use a <code>SIF</code> file and when updates need to be done, build a sandbox image from the <code>SIF</code> file, make the required changes, and then build a new <code>SIF</code> file, e.g.,
<!--T:95-->
<source lang="console">$ cd $HOME
<source lang="console">$ cd $HOME
$ mkdir mynewimage.dir
$ mkdir mynewimage.dir
Line 145: Line 356:
$ rm -rf mynewimage.dir</source>
$ rm -rf mynewimage.dir</source>


Using an <code>SIF</code> image is recommended as disk performance (from the container image) will be faster than storing each file, etc. separately on Alliance cluster filesystems (which are set up to handle large files and parallel I/O). Using an <code>SIF</code> file instead of a sandbox image will also only use a quota file count amount of 1 instead of thousands (e.g., images will typically contain thousands of files and directories).
<!--T:96-->
Using an <code>SIF</code> file is recommended as disk performance (from the container image) will be faster than storing each file separately on our cluster filesystems, which are set up to handle large files and parallel I/O. Using an <code>SIF</code> file instead of a sandbox image will also only use a quota file count amount of 1 instead of thousands (some images will typically contain thousands of files and directories).
 
<!--T:97-->
Many Linux distribution package managers require root permissions in order to use them. This implies that Apptainer version 1.0.x and the older Singularity cannot be used on compute clusters to build images as a normal user. Should such occur, [[Technical support|submit a ticket]] asking for help to create that image or use a computer with Apptainer installed where you have root permissions.


===Building a Sandbox Directory===
<!--T:98-->
Apptainer has a <code>--fakeroot</code> feature used to build and manipulate images. With versions prior to Apptainer 1.1, one wanting to use this feature on a cluster requires [[Technical support|submitting a ticket]] for a system administrator to consider adding that person so Apptainer's <code>--fakeroot</code> on a specific cluster, which may or not be possible. With Apptainer version 1.1, <code>--fakeroot</code> can be used without being formally added.


<!--T:99-->
Know that some containers will not build successfully without using a <code>root</code> account to build them. These images cannot be built on our clusters.
<!--T:100-->
If all you need is to use a Docker image as-is with Apptainer, often those images can be built and run without issues, e.g.,  without any need to have additional permissions or explicitly use <code>--fakeroot</code>. Should you need to modify the image after creating it, you may need elevated permissions to successfully do this, e.g., if the image's Linux distribution package manager requires such and you need to install a package using it. For this reason, the examples shown below assume one only needs to use a Docker image as-is.
==Building a SIF image== <!--T:101-->
<!--T:102-->
<b>NOTE:</b>  Please note and heed the advice concerning building images/overlays in section <b>[[#Building_Images.2FOverlays|Building images and overlays]] </b> above.
<!--T:103-->
To build an Apptainer SIF file image from Docker's latest available busybox image, use the <code>apptainer build</code> command:
<source lang="console">$ apptainer build bb.sif docker://busybox</source>
<!--T:104-->
See the [https://apptainer.org/docs Apptainer documentation] for more advanced aspects of building images.
==Building a sandbox image== <!--T:105-->
<!--T:106-->
<b>NOTE:</b>  Please note and heed the advice concerning building images/overlays in section <b>[[#Building_Images.2FOverlays|Building images and overlays]] </b> above.
<!--T:107-->
In order to build a "sandbox" directory instead of an <code>SIF</code> file instead of providing an <code>SIF</code> file name, instead provide <code>--sandbox DIR_NAME</code> or <code>-s DIR_NAME</code> where <code>DIR_NAME</code> is the name of the to-be-created-directory where you want your "sandbox" image. For example, if the <code>apptainer build</code> command to create an <code>SIF</code> file was:
In order to build a "sandbox" directory instead of an <code>SIF</code> file instead of providing an <code>SIF</code> file name, instead provide <code>--sandbox DIR_NAME</code> or <code>-s DIR_NAME</code> where <code>DIR_NAME</code> is the name of the to-be-created-directory where you want your "sandbox" image. For example, if the <code>apptainer build</code> command to create an <code>SIF</code> file was:
<source lang="console">$ apptainer build bb.sif docker://busybox</source>
<source lang="console">$ apptainer build bb.sif docker://busybox</source>
Line 154: Line 394:
<source lang="console">$ apptainer build --sandbox bb.dir docker://busybox</source>
<source lang="console">$ apptainer build --sandbox bb.dir docker://busybox</source>


<!--T:108-->
Differences between building a "sandbox" image and a (normal) <code>SIF</code> file are:
Differences between building a "sandbox" image and a (normal) <code>SIF</code> file are:


<!--T:109-->
* the <code>SIF</code> file's image will be contained in a single file, compressed, and read-only,
* the <code>SIF</code> file's image will be contained in a single file, compressed, and read-only,
* the "sandbox" image will be placed in a directory, uncompressed, may contain thousands of files (depending on what exactly is in the image), and will be able to be read-write.
* the "sandbox" image will be placed in a directory, uncompressed, may contain thousands of files (depending on what exactly is in the image), and will be able to be read-write.


<!--T:110-->
Within an account, using a "sandbox" directory will consume significant amounts of both disk space and file count quotas, thus, if read-write access to the underlying image is not normally required, you are advised to use an <code>SIF</code> instead. Additionally, using an <code>SIF</code> file will have higher disk access speeds to content contained within the <code>SIF</code> file.
Within an account, using a "sandbox" directory will consume significant amounts of both disk space and file count quotas, thus, if read-write access to the underlying image is not normally required, you are advised to use an <code>SIF</code> instead. Additionally, using an <code>SIF</code> file will have higher disk access speeds to content contained within the <code>SIF</code> file.


===Building an SIF File===
=Example use cases= <!--T:111-->
 
==Using Conda in Apptainer == <!--T:112-->
 
<!--T:144-->
We will preface this tutorial on how to use Conda inside a container with the following '''important notes''':
 
<!--T:145-->
#Even inside a container, Conda should be used only as a '''last resort'''. Priority should always be given to using [[Modules|modules]] from our [[Available_software|software stack]], and [[Python|wheels]] from our [[Available_Python_wheels|Python wheelhouse]]. These are optimized for our systems and we are better equipped to provide support if you use them. Please contact us if you need a module or a Python package that is not currently available on our systems.
#This tutorial will use the [https://mamba.readthedocs.io/en/latest/user_guide/micromamba.html micromamba] package manager instead of Conda. If you choose to use Conda instead, keep in mind that its use is subject to [https://legal.anaconda.com/policies/en?name=terms-of-service#terms-of-service Anaconda's Terms of Service] and might require a  [https://www.anaconda.com/pricing/terms-of-service-faqs commercial license].
#This tutorial shows how to create a read-only image, i.e., a one-off <tt>.sif</tt> file containing a Conda environment that has everything you need to run your application. We strongly discourage installing software interactively with Conda inside a container and will not show how to do this here.
 
<!--T:146-->
Creating an Apptainer image and using Conda to install software inside it is a 3-step process. The first step is to create a <tt>.yml</tt> file describing the Conda environment we wish to create inside the container. In the example that follows, we create the file <tt>environment.yml</tt> . This file is where we give our environment a name, then give Conda a list of packages that must be installed and the channels where to look for them. For more information [https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#create-env-file-manually see here].
 
<!--T:147-->
{{File
  |name=environment.yml
  |lang="yaml"
  |contents=
name: base
channels:
  - conda-forge
  - bioconda
  - defaults
dependencies:
  - python
  - pip
  - star
  - bwa
  - multiqc
}}
 
<!--T:148-->
Second, we create an Apptainer [https://apptainer.org/docs/user/main/definition_files.html image definition file]. This file, here called <tt>image.def</tt>, describes what are the steps Apptainer should take to create our image. These steps are:
#Pull a Docker image from DockerHub that has the micromamba package manager pre-installed.
#Create a copy of the Conda environment definition file <tt>environment.yml</tt> isnide the container
#Call micromamba and have it configure the environment defined in <tt>environment.yml</tt>.
 
<!--T:149-->
{{File
  |name=image.def
  |lang="yaml"
  |contents=
Bootstrap: docker
From: mambaorg/micromamba:latest
 
<!--T:150-->
%files
    environment.yml /environment.yml
 
<!--T:151-->
%post
    micromamba install -n base --file environment.yml && \
        micromamba clean --all --yes
}}
 
<!--T:152-->
The last step is to build the Apptainer image using the definition file above:
  module load apptainer
  APPTAINER_BIND=' ' apptainer build image.sif image.def
 
<!--T:153-->
You can test that your image provides <code>multiqc</code>, for example, like this:
{{Command
|apptainer run image.sif multiqc --help
|result=
/// MultiQC 🎃 v1.25.1
Usage: multiqc [OPTIONS] [ANALYSIS DIRECTORY]
...
}}
 
==Using Spack in Apptainer == <!--T:114-->
 
<!--T:115-->
In preparation.
 
==Using NVIDIA GPUs in Apptainer == <!--T:116-->
 
<!--T:117-->
In preparation.
 
==Using MPI in Apptainer == <!--T:118-->
 
<!--T:119-->
In preparation.
 
==Creating an Apptainer container from a Dockerfile== <!--T:120-->
 
<!--T:121-->
<b>NOTE: This section requires you to install and use Docker and Apptainer on a system where you have appropriate privileges. These instructions will <i>not</i> work on our compute clusters.</b>
 
<!--T:122-->
Unfortunately, some instructions for packages only provide a <code>Dockerfile</code> without a container image. A <code>Dockerfile</code> contains the instructions necessary for the Docker software to build that container. Our clusters do not have the Docker software installed. That said, if you've access to a system with both Docker and Apptainer installed, and, sufficient access to Docker (e.g., <code>sudo</code> or root access, or, you are in that system's <code>docker</code> group) and if needed Apptainer (e.g., <code>sudo</code> or root access, or, you have <code>--fakeroot</code> access), then you can follow the instructions below to use Docker and then Apptainer to build an Apptainer image on that system.
 
<!--T:123-->
<b>NOTE:</b> Using Docker may fail if you are not in the <code>docker</code> group. Similarly, building some containers may fail with Apptainer without appropriate <code>sudo</code>, root, or <code>--fakeroot</code> permissions. It is your responsibility to ensure you've such access on the system you are running the commands below.
 
<!--T:124-->
If one only has a Dockerfile and wishes to create an Apptainer image, run the following on a computer with Docker and Apptainer installed (where you've sufficient permissions):
 
<!--T:125-->
docker build -f Dockerfile -t your-tag-name
docker save your-tag-name -o your-tarball-name.tar
docker image rm your-tag-name
apptainer build --fakeroot your-sif-name.sif docker-archive://your-tarball-name.tar
rm your-tarball-name.tar
 
<!--T:126-->
where:
 
<!--T:127-->
* <code>your-tag-name</code> is a name you make up that will identify the container created in Docker
* <code>your-tarball-name.tar</code> is a filename you create that Docker will save the generated content of the container to
* <code>--fakeroot</code> is possibly optional (if so omit such); if <code>sudo</code> is needed instead then omit <code>--fakeroot</code> and prefix the line with <code>sudo</code>
* <code>your-sif-name.sif</code> is the name of the Apptainer SIF file for the Apptainer container
 
<!--T:128-->
After this is done, the SIF file is an Apptainer container for the <code>Dockerfile</code>. Transfer the SIF to the appropriate cluster(s) in order to use such.


'''NOTE:''' This section only discusses some basics of creating a simple compressed, read-only <code>SIF</code> file container image. See the [https://apptainer.org/docs Apptainer documentation] for more advanced aspects of building images.
<!--T:129-->
<b>NOTE:</b> It is possible that the Dockerfile pulled in more layers which means you will have to manually delete those additional layers by running:
</translate>


Text to come.
docker images


=Example Use Cases=
<translate>
<!--T:130-->
followed by running <code>docker image rm ID</code> (where ID is the image ID output from the <code>docker images</code> command) in order to free up the disk space associated with those other image layers on the system you are using.


==Using Docker Images Within an Apptainer Container==
=Miscellaneous items= <!--T:131-->


Text to come.
==Cleaning Apptainer's cache directory== <!--T:132-->


==Using Conda Within an Apptainer Container==
<!--T:133-->
Over time Apptainer's file cache will grow. To see where these files are run:
</translate>


Text to come.
apptainer cache list


==Using Spack Within an Apptainer Container==
<translate>
<!--T:134-->
and to remove those files, run:
</translate>


Text to come.
apptainer cache clean


==Using NVIDIA GPUs Within an Apptainer Container==
<translate>
==Changing Apptainer's default directories== <!--T:135-->


Text to come.
<!--T:136-->
You can override Apptainer's default temporary and cache directories by setting these environment variables before running <code>apptainer</code>:


==Running MPI Programs Inside an Apptainer Container==
<!--T:137-->
* <code>APPTAINER_CACHEDIR</code>: the directory where Apptainer will download and cache files
* <code>APPTAINER_TMPDIR</code>: the directory where Apptainer will write temporary files including when building (squashfs) images


Text to come.
<!--T:138-->
For example, to tell Apptainer to use your scratch space for its cache and temporary files (which is might be a better location), one might run:
</translate>


=FAQ=
$ mkdir -p /scratch/$USER/apptainer/{cache,tmp}
$ export APPTAINER_CACHEDIR="/scratch/$USER/apptainer/cache"
$ export APPTAINER_TMPDIR="/scratch/$USER/apptainer/tmp"


Text to come.
<translate>
<!--T:139-->
before running <code>apptainer</code>.
</translate>

Latest revision as of 18:27, 28 October 2024

Other languages:

Forewords[edit]

Official Apptainer documentation[edit]

This page does not describe all features of Apptainer and does not replace Apptainer's official documentation. It summarizes basic use, documents some aspects of using Apptainer on Alliance systems, and provides some relevant examples. We recommend you read the official Apptainer documentation concerning the features of Apptainer you are using.

Should you wish to install Apptainer on your own system, instructions appear here. If you are using a recent Windows system, install WSL first, then within such, install Apptainer. If you are using a Mac, install a Linux distribution in a virtual machine on your computer first, then install Apptainer within such.

If you are currently using Singularity[edit]

We strongly recommend that you use Apptainer instead of Singularity. The Linux Foundation has adopted SingularityCE (up to v3.9.5) and renamed it Apptainer with these changes:

  • Added support for DMTCP checkpointing.
  • Removed support for the --nvccli command line option.
  • Removed support for apptainer build --remote.
  • Removed support for the SylabsCloud remote endpoint, replacing it with a DefaultRemote endpoint with no defined server for library://.
  • Renamed all executable names, paths, etc. having singularity in their names to have apptainer in them.
    • E.g., instead of using the singularity command one uses the apptainer command.
    • E.g., the ~/.singularity directory is now ~/.apptainer.
  • Renamed all environment variables having SINGULARITY in their names to have APPTAINER in them.

Should you need to port scripts to Apptainer, know Apptainer version 1 is backwards compatible with Singularity so switching to Apptainer can be done incrementally.

Other Linux container technologies[edit]

HPC clusters typically use Apptainer. Many users ask about other Linux container technologies so here are some with some comments:

  • Podman
    • Supports rootless (i.e., normal) container use, etc. similar to Apptainer.
    • Is available as a package in rpm-supporting (and some other) Linux distributions.
    • While Podman is a Linux container technology, there are instructions for installing Podman on Windows/Mac machines.
    • Podman version 4 supports Apptainer .SIF files.
  • Docker
    • Using Docker on a multiuser cluster creates security risks, therefore we do not make Docker available on our HPC clusters.
    • You can install and use Docker on your own computer and use it to create an Apptainer image, which can then be uploaded to an HPC cluster as outlined in this section later on this page.

Other items[edit]

General[edit]

  • In order to use Apptainer you must have a container image, e.g., a .sif file or a "sandbox" directory created previously. If you don't already have an image or a sandbox, see the section on building an image below.
  • While Apptainer is installed and available for use, using Apptainer will require you to install and/or build all software you will need to make use of in your container. In many instances, we already have such software installed on our clusters so there is often no need to create a container with the same installed in it.

sudo[edit]

Many users ask about sudo since documentation and websites often discuss using sudo. Know the ability to use sudo to obtain superuser/root permissions is not available on our clusters. Should you require using sudo, consider the following options:

  • Install Linux, Apptainer, and sudo in a virtual machine on a system you control so you will be able to have sudo access within such. Build your image(s) on that machine and upload them in order to use them on Alliance systems.
  • If appropriate, submit a ticket asking if Alliance staff would be able to help build the image(s) required needing sudo. This may or may not be possible, but feel free to ask in a ticket if what you wish to achieve is beyond your means. Additionally, we may respond with other ways to achieve such which may or may not involve Apptainer.
  • Apptainer version 1.1.x and newer has improved support for users using --fakeroot implicitly and explicitly so some things may be possible that were not with Apptainer version 1.0 and Singularity. This includes being able to build some images from .def definition files and building some images without needing to use sudo. That said, not all images will be able to be built without needing to use sudo or superuser/root.

Building images or overlays[edit]

Should you need to build your own container image(s) or overlay(s), be aware of the following:

Loading an Apptainer module[edit]

In order to use the default version of Apptainer available run:

$ module load apptainer

To see the available versions of Apptainer that can be loaded run:

$ module spider apptainer

Running programs within a container[edit]

Important command line options[edit]

Software that is run inside a container is in an environment that uses different libraries and tools than what is installed on the host system. It is, therefore, important to run programs within containers by not using any environment settings or software defined outside of the container. Unfortunately, by default, Apptainer will run adopting the shell environment of the host and this can result in issues when running programs. To avoid such issues when using apptainer run, apptainer shell, apptainer exec, and/or apptainer instance, use one of these options:

Apptainer environment command line options
-C Isolates the running container from all filesystems as well as the parent PID, IPC, and environment. Using this option will require using bind mounts if access to filesystems outside of the container is needed.
-c Isolates the running container from most filesystems only using a minimal /dev, an empty /tmp directory, and an empty /home directory. Using this option will require using bind mounts if access to filesystems outside of the container is needed.
-e Cleans (some) shell environment variables before running container commands and applies settings for increased OCI/Docker compatibility. Using this option also implies the use of options --containall, --no-init, --no-umask, and --writable-tmpfs.

Another important option is the -W or --workdir option. On our clusters and on most Linux systems, /tmp and similar filesystems use RAM, not disk space. Since jobs typically run on our clusters with limited RAM amounts, this can result in jobs getting killed because they consume too much RAM relative to what was requested for the job. A suitable workaround for this is to tell Apptainer to use a real disk location for its working directory (workdir). This is done by passing the -W option followed by a path to a disk location where Apptainer can read/write temporary files For example, to run a command called myprogram in an Apptainer container image called myimage.sif with its working directory set to /path/to/a/workdir in the filesystem, you would use

apptainer run -C -B /project -W /path/to/a/workdir myimage.sif myprogram

where:

  • workdir can be removed if there are no live containers using it.
  • When using Apptainer in an salloc, in an sbatch job, or when using JupyterHub on our clusters, use ${SLURM_TMPDIR} for the working directory's location, e.g., -W ${SLURM_TMPDIR}.
    • ASIDE: One should not be running programs (including Apptainer) on a login node. Use an interactive salloc job.
  • When using bind mounts, see the section on bind mounts below since not all of our clusters are the same concerning the exact bind mounts needed to access /home, /project, and /scratch.

Using GPUs[edit]

When running software inside a container that requires the use of GPUs it is important to do the following:

  • Ensure that you pass the --nv (for NVIDIA hardware) and --rocm (for AMD hardware) to Apptainer commands.
    • These options will ensure the appropriate /dev entries are bind mounted inside the container.
    • These options will locate and bind GPU-related libraries on the host, as well as set the LD_LIBRARY_PATH environment variable to enable the aforementioned libraries to work inside the container.
  • Ensure the application using the GPU inside the container was properly compiled to use the GPU and its libraries.
  • When needing to use OpenCL inside the container, besides using the aforementioned options, use the following bind mount: -B /etc/OpenCL.

An example of using NVIDIA GPUs within an Apptainer container appears later on this page.

Using MPI programs[edit]

If you need to run MPI programs inside a container, there are things that need to be done in the host environment in order for such to work. Please see the Running MPI Programs section below for an example of how to run MPI programs inside a container. The official Apptainer documentation has more information concerning how MPI programs can be run inside a container.

Container-specific help: apptainer run-help[edit]

Apptainer containers built from Definition files often will have a %help section. To see this section run:

apptainer run-help your-container-name.sif

where:

  • your-container-name.sif is the name of your container

It is possible your container also has "apps" defined in it, you can get help for those apps by running:

apptainer run-help --app appname your-container-name.sif

where:

  • appname is the name of the app
  • your-container-name.sif is the name of your container

To see a list of apps installed in your container (if there are any), run:

apptainer inspect --list-apps your-container-name.sif

where:

  • your-container-name.sif is the name of your container

Running software: apptainer run or apptainer exec[edit]

When the apptainer run command launches the container, it first runs the %runscript defined for that container (if there is one), and then runs the specific command you specified.
The apptainer exec command will not run the %runscript, even if one is defined in the container.

We suggest that you aways use apptainer run.

For example, suppose you want to run the g++ compiler inside your container to compile a C++ program called myprog.cpp and then run that program. To do this, you might use this command:

apptainer run your-container-name.sif g++ -O2 -march=broadwell ./myprog.cpp
apptainer run your-container-name.sif ./a.out

where:

  • your-container-name.sif is the name of your SIF file
  • g++ -O2 -march=broadwell ./myprog.cpp is the command you want to run inside the container

On our clusters, you will want to use a number of additional options (that appear after run, but before your-container-name.sif). These options include -C, -c, -e, -W as well as various bind mount options to make your disk space available to the programs that run in your container. For example:

 apptainer run -C -W $SLURM_TMPDIR -B /project -B /scratch your-container-name.sif g++ -O2 -march=broadwell ./myprog.cpp
 apptainer run -C -W $SLURM_TMPDIR -B /project -B /scratch ./a.out

For more information on these options see the following sections on this page:

as well as the official Apptainer documentation.

Interactively running software: apptainer shell[edit]

The apptainer run, apptainer exec, and apptainer instance commands run the programs provided immediately which makes them excellent for use in BASH and SLURM job scripts. There are times when one needs to interactively do work inside a container. To run commands interactively while remaining inside a container, use the apptainer shell command instead.

For example, to run commands interactively in a container one first invokes the apptainer shell command, e.g.,

apptainer shell your-container-name.sif

where:

  • your-container-name.sif is the name of your SIF file

Once the container starts, you will see an Apptainer> prompt (or Singularity> prompt if using Singularity). At this prompt you can run desired shell commands in the container. When done, type exit and hit the Enter/Return key to exit the container.

On our clusters, you will want to use a number of additional options (that appear after run and before your-container-name.sif). These options will include -C, -c, -e, -W as well as various bind mount options to make your disk space available to the programs that run in your container. For example:

 apptainer shell -C -W $SLURM_TMPDIR -B /home:/cluster_home -B /project -B /scratch your-container-name.sif

For more information on these options see the following sections on this page:

as well as the official Apptainer documentation.

IMPORTANT:In addition to choose to use the above options, if you are making use of a persistent overlay image (as a separate file or contained within the SIF file) and want changes to be written to that image, it is extremely important to pass the -w or --writable option to your container. If this option is not passed to it, any changes you make to the image in the apptainer shell session will not be saved!

Running daemons: apptainer instance[edit]

Apptainer has been designed to be able to properly run daemons within compute jobs on clusters. Running daemons is achieved, in part, by using apptainer instance. See the official Apptainer documentation on Running Services for the details.

NOTE 1: Don't run daemons manually without using apptainer instance and related commands. Apptainer works properly with other tools such as the Slurm scheduler that run on our clusters. When a job is cancelled, killed, crashes, or is otherwise finished, daemons run using apptainer instance will not hang or result in defunct processes. Additionally by using the apptainer instance command you will be able to control the daemons and programs running in the same container.

NOTE 2: Daemons can only run in your job while your job is running. Should the scheduler kill your job, all daemons running in that job will also be killed. Should you need to run daemons longer than the job they were started in, you may need to also have a virtual machine running in the cloud. If so please contact technical support.

Running MPI programs[edit]

Running MPI programs within an Apptainer container across nodes likely will require special configuration. MPI exploits cluster interconnection hardware to communicate amongst nodes much more efficiently. Normally one does not need to worry about this since it is automatically done --except when running MPI programs across cluster nodes.

NOTE: When all MPI processes are running on a single shared-memory node, there is no need to use interconnection hardware and there will be no issues running MPI programs within an Apptainer container when all MPI processes run on a single cluster node, e.g., when the slurm option --nodes=1 is used with an sbatch script. Unless one explicitly sets the maximum number of cluster nodes used to 1, the scheduler can choose to run an MPI program over multiple nodes. If such will run from within an Apptainer container and has not been set up to properly run, then it is possible it will fail to run.

More in preparation.

Bind mounts and persistent overlays[edit]

Often, one will want to use either or both of these features in Apptainer:

  • bind mounts, to access disk space originating outside of the container, and,
  • persistent overlays, to overlay a writable filesystem on an otherwise immutable (i.e., read-only) container image.

Bind mounts[edit]

When Apptainer is used with the -C or -c options, one will notice that they cannot access their disk space when inside the container. The remedy for this is to explicitly bind mount the disk space they wish to access. For example, suppose a user was using -C like this in an sbatch job to use Apptainer:

apptainer run -C -W $SLURM_TMPDIR a-container.sif wc -l ./my_data_file.txt

where ./my_data_file.txt is a file in the current directory on the host, i.e., the file is not stored in the container at all. Because of the -C option, this file will not be accessible to the wc program inside the container and an access error will result. The fix is to bind mount the current directory, e.g.,

apptainer run -C -B . -W $SLURM_TMPDIR a-container.sif wc -l ./my_data_file.txt

where -B . will bind mount the current directory, ..

While one can have multiple bind mounts specified, it is often easier to specify the top directories of the filesystems one wishes to access. For example, on our clusters one might want to use

apptainer run -C -B /project -B /scratch -W $SLURM_TMPDIR a-container.sif wc -l ./my_data_file.txt

where:

  • -B /project mounts the project filesystem
  • -B /scratch mounts the scratch filesystem

Doing this is especially useful when:

  • you need to access others' files on your research team in other locations, and/or,
  • you need to access files/directories some of which are symlinks to different locations that would/might otherwise be broken if you did not mount the entire filesystem.

If using these bind mounts does not work on the cluster you are using, run this next script to obtain the bind mount options you need to pass to Apptainer for /home, /project, and /scratch on that cluster

/home/preney/public/apptainer-scripts/get-apptainer-options.sh

It should be mentioned that a bind mount does not need to be in the same location inside the container: one can bind mount any file or directory to be at a different location, e.g.,

apptainer run -C -B ./my_data_file.txt:/special/input.dat -W $SLURM_TMPDIR a-container.sif wc -l /special/input.dat

i.e., -B ./my_data_file.txt:/special/input.dat bind mount maps the file ./my_data_file.txt to be the file /special/input.dat inside the container and the wc command now processes that file. This feature can be useful when programs/scripts inside the container have hard-coded paths to files and directories that must be located in certain locations.

If you need to bind-mount the /home filesystem in your container, use an alternate destination directory:

  • -B /home:/cluster_home

This ensures that configuration files and programs in your home directory do not interfere with the software in your container. For example, programs in $HOME/bin and Python packages in $HOME/.local/lib/python3.x could be used instead of the corresponding files in your container if you used -B /home.

Finally, don't mount our CVMFS paths inside your containers as this is fraught with perils and defeats many reasons to use a container. The programs that have to run inside a container need to be completely inside the container. Don't introduce even more programs inside the container if they don't need to be there.

Persistent overlays[edit]

Please refer to Apptainer documentation page about persistent overlays.

Building an Apptainer image[edit]

NOTE: Please note and heed the advice given in section Building images and overlays.

Apptainer images can be created in the following formats:

  • as an SIF file, or
  • as a sandbox directory.

SIF files can contain multiple parts where each part is typically a squashfs filesystem (which are read-only and compressed). It is possible for SIF files to contain read-write filesystems and overlay images as well, but such is beyond the scope of this page; see Apptainer's official documentation on how to do such. Unless more advanced methods of building an image were used, the Apptainer build command produces a SIF file with a read-only squashfs filesystem when building images. This is the preferred option since the resulting image remains as-is since it is read-only, and the image is much smaller because it is compressed. Know that disk reads from that image are done very quickly.

A sandbox directory is a normal directory in the filesystem that starts out as empty. As Apptainer builds the image, it adds to it the files and directories needed in the image. The contents of a sandbox directory should only be accessed or updated through the use of Apptainer. One might need to use a sandbox directory in situations where one needs to have read-write access to the image itself in order to be able to update the container image. That said, if updates are infrequent, it is typically easier and better to use a SIF file and when updates need to be done, build a sandbox image from the SIF file, make the required changes, and then build a new SIF file, e.g.,

$ cd $HOME
$ mkdir mynewimage.dir
$ apptainer build mynewimage.dir myimage.sif
$ apptainer shell --writable mynewimage.dir
Apptainer> # Run commands to update mynewimage.dir here.
Apptainer> exit
$ apptainer build newimage.sif mynewimage.dir
$ rm -rf mynewimage.dir

Using an SIF file is recommended as disk performance (from the container image) will be faster than storing each file separately on our cluster filesystems, which are set up to handle large files and parallel I/O. Using an SIF file instead of a sandbox image will also only use a quota file count amount of 1 instead of thousands (some images will typically contain thousands of files and directories).

Many Linux distribution package managers require root permissions in order to use them. This implies that Apptainer version 1.0.x and the older Singularity cannot be used on compute clusters to build images as a normal user. Should such occur, submit a ticket asking for help to create that image or use a computer with Apptainer installed where you have root permissions.

Apptainer has a --fakeroot feature used to build and manipulate images. With versions prior to Apptainer 1.1, one wanting to use this feature on a cluster requires submitting a ticket for a system administrator to consider adding that person so Apptainer's --fakeroot on a specific cluster, which may or not be possible. With Apptainer version 1.1, --fakeroot can be used without being formally added.

Know that some containers will not build successfully without using a root account to build them. These images cannot be built on our clusters.

If all you need is to use a Docker image as-is with Apptainer, often those images can be built and run without issues, e.g., without any need to have additional permissions or explicitly use --fakeroot. Should you need to modify the image after creating it, you may need elevated permissions to successfully do this, e.g., if the image's Linux distribution package manager requires such and you need to install a package using it. For this reason, the examples shown below assume one only needs to use a Docker image as-is.

Building a SIF image[edit]

NOTE: Please note and heed the advice concerning building images/overlays in section Building images and overlays above.

To build an Apptainer SIF file image from Docker's latest available busybox image, use the apptainer build command:

$ apptainer build bb.sif docker://busybox

See the Apptainer documentation for more advanced aspects of building images.

Building a sandbox image[edit]

NOTE: Please note and heed the advice concerning building images/overlays in section Building images and overlays above.

In order to build a "sandbox" directory instead of an SIF file instead of providing an SIF file name, instead provide --sandbox DIR_NAME or -s DIR_NAME where DIR_NAME is the name of the to-be-created-directory where you want your "sandbox" image. For example, if the apptainer build command to create an SIF file was:

$ apptainer build bb.sif docker://busybox

then change bb.sif to a directory name, e.g., bb.dir, and prefix such with --sandbox:

$ apptainer build --sandbox bb.dir docker://busybox

Differences between building a "sandbox" image and a (normal) SIF file are:

  • the SIF file's image will be contained in a single file, compressed, and read-only,
  • the "sandbox" image will be placed in a directory, uncompressed, may contain thousands of files (depending on what exactly is in the image), and will be able to be read-write.

Within an account, using a "sandbox" directory will consume significant amounts of both disk space and file count quotas, thus, if read-write access to the underlying image is not normally required, you are advised to use an SIF instead. Additionally, using an SIF file will have higher disk access speeds to content contained within the SIF file.

Example use cases[edit]

Using Conda in Apptainer[edit]

We will preface this tutorial on how to use Conda inside a container with the following important notes:

  1. Even inside a container, Conda should be used only as a last resort. Priority should always be given to using modules from our software stack, and wheels from our Python wheelhouse. These are optimized for our systems and we are better equipped to provide support if you use them. Please contact us if you need a module or a Python package that is not currently available on our systems.
  2. This tutorial will use the micromamba package manager instead of Conda. If you choose to use Conda instead, keep in mind that its use is subject to Anaconda's Terms of Service and might require a commercial license.
  3. This tutorial shows how to create a read-only image, i.e., a one-off .sif file containing a Conda environment that has everything you need to run your application. We strongly discourage installing software interactively with Conda inside a container and will not show how to do this here.

Creating an Apptainer image and using Conda to install software inside it is a 3-step process. The first step is to create a .yml file describing the Conda environment we wish to create inside the container. In the example that follows, we create the file environment.yml . This file is where we give our environment a name, then give Conda a list of packages that must be installed and the channels where to look for them. For more information see here.


File : environment.yml

name: base
channels:
  - conda-forge
  - bioconda
  - defaults
dependencies:
  - python
  - pip
  - star
  - bwa
  - multiqc


Second, we create an Apptainer image definition file. This file, here called image.def, describes what are the steps Apptainer should take to create our image. These steps are:

  1. Pull a Docker image from DockerHub that has the micromamba package manager pre-installed.
  2. Create a copy of the Conda environment definition file environment.yml isnide the container
  3. Call micromamba and have it configure the environment defined in environment.yml.


File : image.def

Bootstrap: docker
From: mambaorg/micromamba:latest

%files
    environment.yml /environment.yml

%post
    micromamba install -n base --file environment.yml && \
        micromamba clean --all --yes


The last step is to build the Apptainer image using the definition file above:

  module load apptainer
  APPTAINER_BIND=' ' apptainer build image.sif image.def

You can test that your image provides multiqc, for example, like this:

Question.png
[name@server ~]$ apptainer run image.sif multiqc --help
/// MultiQC 🎃 v1.25.1
 
 Usage: multiqc [OPTIONS] [ANALYSIS DIRECTORY]
...

Using Spack in Apptainer[edit]

In preparation.

Using NVIDIA GPUs in Apptainer[edit]

In preparation.

Using MPI in Apptainer[edit]

In preparation.

Creating an Apptainer container from a Dockerfile[edit]

NOTE: This section requires you to install and use Docker and Apptainer on a system where you have appropriate privileges. These instructions will not work on our compute clusters.

Unfortunately, some instructions for packages only provide a Dockerfile without a container image. A Dockerfile contains the instructions necessary for the Docker software to build that container. Our clusters do not have the Docker software installed. That said, if you've access to a system with both Docker and Apptainer installed, and, sufficient access to Docker (e.g., sudo or root access, or, you are in that system's docker group) and if needed Apptainer (e.g., sudo or root access, or, you have --fakeroot access), then you can follow the instructions below to use Docker and then Apptainer to build an Apptainer image on that system.

NOTE: Using Docker may fail if you are not in the docker group. Similarly, building some containers may fail with Apptainer without appropriate sudo, root, or --fakeroot permissions. It is your responsibility to ensure you've such access on the system you are running the commands below.

If one only has a Dockerfile and wishes to create an Apptainer image, run the following on a computer with Docker and Apptainer installed (where you've sufficient permissions):

docker build -f Dockerfile -t your-tag-name
docker save your-tag-name -o your-tarball-name.tar
docker image rm your-tag-name
apptainer build --fakeroot your-sif-name.sif docker-archive://your-tarball-name.tar
rm your-tarball-name.tar

where:

  • your-tag-name is a name you make up that will identify the container created in Docker
  • your-tarball-name.tar is a filename you create that Docker will save the generated content of the container to
  • --fakeroot is possibly optional (if so omit such); if sudo is needed instead then omit --fakeroot and prefix the line with sudo
  • your-sif-name.sif is the name of the Apptainer SIF file for the Apptainer container

After this is done, the SIF file is an Apptainer container for the Dockerfile. Transfer the SIF to the appropriate cluster(s) in order to use such.

NOTE: It is possible that the Dockerfile pulled in more layers which means you will have to manually delete those additional layers by running:

docker images

followed by running docker image rm ID (where ID is the image ID output from the docker images command) in order to free up the disk space associated with those other image layers on the system you are using.

Miscellaneous items[edit]

Cleaning Apptainer's cache directory[edit]

Over time Apptainer's file cache will grow. To see where these files are run:

apptainer cache list

and to remove those files, run:

apptainer cache clean

Changing Apptainer's default directories[edit]

You can override Apptainer's default temporary and cache directories by setting these environment variables before running apptainer:

  • APPTAINER_CACHEDIR: the directory where Apptainer will download and cache files
  • APPTAINER_TMPDIR: the directory where Apptainer will write temporary files including when building (squashfs) images

For example, to tell Apptainer to use your scratch space for its cache and temporary files (which is might be a better location), one might run:

$ mkdir -p /scratch/$USER/apptainer/{cache,tmp}
$ export APPTAINER_CACHEDIR="/scratch/$USER/apptainer/cache"
$ export APPTAINER_TMPDIR="/scratch/$USER/apptainer/tmp"

before running apptainer.