Module Apptainer/Singularity

Module Apptainer/Singularity

The commands "apptainer" and "singularity" are almost identical in functionality, as of this writing, as installed on the Athena cluster.  As Apptainer seems to be gaining more popularity, and is open-source as well, the command "apptainer" will be used throughout this page.

Interactive job

Open an interactive session to a compute node. Adjust the number of CPUs (--cpus-per-task 1) and the amount of memory (--mem 4G) needed for your job. Increase the number of --cpus-per-task only when using code that can run in parallel.

srun --cpus-per-task 1 --mem 4G --ntasks=1 --pty bash

Load the Apptainer module

module load apptainer

Now that Apptainer is loaded, it can be used to run software in a secure, containerized environment.  For example, to work with the ROOT data processing framework first developed at CERN, commands such as the following can be used to run an image of ROOT.  This is even though it's not installed yet on the machine the Apptainer container is being created on.  First, set up a remote endpoint to be used:

apptainer remote use SylabsCloud

Then run the software, using the remote image of it from that endpoint, inside of the container:

apptainer exec docker://rootproject/root root -b

Inside the ROOT environment, enter '.q' to exit that when you're done, then leave the Athena node when you're ready as usual.

root [0] .q
[user@athena1 ~]$ srun --cpus-per-task 1 --mem 4G --ntasks=1 --pty bash ## start on the head node [user@athena153 ~]$ module load apptainer ## now on a compute node [user@athena153 ~]$ apptainer remote use SylabsCloud INFO: Remote "SylabsCloud" now in use. [user@athena153 ~]$ apptainer exec docker://rootproject/root root -b INFO: Using cached SIF image INFO: gocryptfs not found, will not be able to use gocryptfs ------------------------------------------------------------------ | Welcome to ROOT 6.30/02 https://root.cern | | (c) 1995-2023, The ROOT Team; conception: R. Brun, F. Rademakers | | Built for linuxx8664gcc on Nov 27 2023, 19:50:38 | | From tags/v6-30-02@ | | With c++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 | | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q' | ------------------------------------------------------------------ root [0] 1 == 2 (bool) false root [1] cout << "Running in ROOT environment..." << endl; Running in ROOT environment... root [2] .q [user@athena153 ~]$ exit ## leave the compute node exit [user@athena1 ~]$ ## now back on the head node

Creating a Container of Your Own

This process can be useful to preserve legacy software that ran fine on older machines, but will be difficult to build or install on newer ones.  Or to just get some software running under your account.  Your workflow can be maintained much easier by simply putting the software in a container that can be copied to the Athena cluster and used there.  For an example of creating a container to use on Athena, an old Fortran program that prints out the prime numbers from 0 to 30 will be used here.  It was built and ran fine on the Teal cluster:

[user@teal ~/fortran_programming]$ ./primes Prime numbers, 1-30. 2 is prime. 3 is prime. 5 is prime. 7 is prime. 11 is prime. 13 is prime. 17 is prime. 19 is prime. 23 is prime. 29 is prime.

On Athena, however, there is a problem if trying to run a copy of the executable program, that was brought over from Teal:

[user@athena101: ~/fortran_programming]$ ./primes ./primes: error while loading shared libraries: libgfortran.so.3: cannot open shared object file: No such file or directory

A search of Athena will reveal that (as of this writing) the missing dynamic library file, libgfortran.so.3, isn't on the machine.  This is a trivial example, it would be easy to simply rebuild the code since it's available (see bottom of page) or go through a little effort to provide the missing library file.  In other cases though, the source code may be missing, or onerous to build, or there may be problems with finding or installing dynamic libraries, or it may even be that system admins just do not have time at the moment to help solve such problems.  In any case, a normal user of Athena can get around such problems on their own, using Apptainer.

First, it's good to know where the software did run successfully, and what missing ingredients are needed to make it run.  Of course, here we have the plain message about what's missing, but you can use the ldd command also to see how a program was built.  So, we need to know what operating system worked with the software, as well as components used to build it:

[user@teal:~/fortran_programming]$ ldd primes linux-vdso.so.1 => (0x00007ffff9df1000) libgfortran.so.3 => /lib64/libgfortran.so.3 (0x00002ac4d2dc1000) libm.so.6 => /lib64/libm.so.6 (0x00002ac4d30e3000) libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00002ac4d33e5000) libquadmath.so.0 => /lib64/libquadmath.so.0 (0x00002ac4d35fb000) libc.so.6 => /lib64/libc.so.6 (0x00002ac4d3837000) /lib64/ld-linux-x86-64.so.2 (0x00002ac4d2b9d000) [user@teal:~/fortran_programming]$ cat /etc/os-release NAME="CentOS Linux" VERSION="7 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="7" PRETTY_NAME="CentOS Linux 7 (Core)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:7" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-7" CENTOS_MANTISBT_PROJECT_VERSION="7" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="7"

So now we can see what library files are needed by this software (that may be missing on Athena), as well as what Operating System it was built and ran under (CentOS-7).  Further, we can do a search and find that the missing libgfortran.so.3 is indeed in /lib64/ on teal, and it seems to be a typical installation for CentOS-7.  It seems safe to assume then, that a container based on CentOS-7, should be able to run the program. 

To build such a container, start with a "definition file".  These are a listing of "ingredients" to build the container.  Your aim is to only include needed ingredients, as the resulting container could become bloated with unneeded things if not carefully defined.  Here is such a definition file used to build a container to run the primes program:

# To build this container: # apptainer build myPrimes.sif myPrimes.def # Once built, you can run it with: # ./myPrimes.sif # apptainer run myPrimes.sif # etc. # container is based on a Docker image # Apptainer will use the Docker protocol to get the base OS # for the image contained Bootstrap: docker From: centos:7 %help This container was created to study using Apptainer. A user of this container should be able to run /opt/primes inside of it, to print out the prime numbers, 0-30. %files # this section lets you copy files to where they'll be needed, inside of the container ./primes /opt/ # put the executable in a generic location in the container; the container will be able to run the local copy though! ./primes_lib/* /lib64/ # copy the needed library files to a generic location inside of the container %post NOW=`date` echo "export NOW=\"${NOW}\"" >> $APPTAINER_ENVIRONMENT # set up the dynamic library environment LD_LIBRARY_PATH=/usr/lib/:$LD_LIBRARY_PATH echo "export LD_LIBRARY_PATH=\"${LD_LIBRARY_PATH}\"" >> $APPTAINER_ENVIRONMENT # make sure the software has permissions to be executed chmod -R 755 /opt chmod 755 /lib64/libgfortran.so.3 chmod 755 /lib64/libquadmath.so.0 %environment NOW=$NOW # anything here is written to a file to be executed when the # image gets loaded. Be sure that this is what you want the container to do. %runscript echo "Container MyCentOS7 was created $NOW." /opt/primes %labels Author HPRC wiki Version v0.1

If you have Apptainer installed on your local machine, then you can do the same process with code that you have there, to create a containerized version of old code you may have running on your machine, but the install process is troublesome on the cluster.  Build your .sif file on your machine and scp it to your account on Athena to use it there.

If not, then you can try building the container on Athena.  This will have to be done either as an interactive or batch job, as account permissions aren't set up on the head node to allow building containers.  To use the definition file above to build the container, start an interactive Slurm job, and then use "apptainer build" as follows:

[user@athena1: ~/]$ cd my_containers [user@athena1: ~/my_containers]$ srun --cpus-per-task 1 --mem 4G --ntasks=1 --pty bash ## start on the head node [user@athena114: ~/my_containers]$ apptainer build myPrimes.sif myPrimes.def ## now on the compute node Build target 'myPrimes.sif' already exists and will be deleted during the build process. Do you want to continue? [N/y] y INFO: Starting build... Getting image source signatures Copying blob 2d473b07cdd5 skipped: already exists Copying config 6d42e44a9f done Writing manifest to image destination Storing signatures 2024/02/28 11:56:14 info unpack layer: sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc INFO: Copying ./primes to /opt/ INFO: Adding help info INFO: Adding labels INFO: Adding environment to container INFO: Adding runscript INFO: Creating SIF file... INFO: Build complete: myPrimes.sif

Once the .sif (the container) file is finished building, you can run the containerized program in one of several ways:

[user@athena114: ~/]$ ./myPrimes.sif ## run the container on the commmand line; uses the %runscript section of the definition file & is useful for scripting containers Container MyCentOS7 was created Wed Feb 28 17:29:44 UTC 2024. Prime numbers, 1-30. 1 is prime. 2 is prime. 3 is prime. 5 is prime. 7 is prime. 11 is prime. 13 is prime. 17 is prime. 19 is prime. 23 is prime. 29 is prime. [user@athena114: ~/]$ apptainer shell myPrimes.sif ## running commands from inside the container; do CTRL+SHIFT+D or enter "exit" to quit the shell INFO: gocryptfs not found, will not be able to use gocryptfs Apptainer> primes bash: primes: command not found Apptainer> /opt/primes Prime numbers, 1-30. 1 is prime. 2 is prime. 3 is prime. 5 is prime. 7 is prime. 11 is prime. 13 is prime. 17 is prime. 19 is prime. 23 is prime. 29 is prime. Apptainer> exit exit

Creating a Container Using Someone Else's Software

In the previous example, a method to create a container for some local software was demonstrated.  Often though, it will be the case that a container is desired for modern software that a user desires to manage and run.  The ROOT software was used in the interactive example and will be used here to show creating a container to hold a useful, modern day software package to do research work. 

To build a container from scratch, a "definition" file (.def) must be created that contains instructions for apptainer to use in building the container.  Here is a definition file from the tutorial at https://hsf-training.github.io/hsf-training-singularity-webpage/05-definition-files/index.html, which has been modified to use python3:

BootStrap: docker From: ubuntu:20.04 %help This definition file is from https://hsf-training.github.io/hsf-training-singularity-webpage/05-definition-files/index.html It has been edited to update to Python3, and to be used with a more recent version of Root. Note that since the video was made, some changes in Root have occurred, for instance, the feature "--has-python" is now "--has-pyroot" as of this writing (03/06/2024), so you may have to juggle such things also to follow along with the tutorial video. %post apt-get update -y apt-get install wget -y export DEBIAN_FRONTEND=noninteractive apt-get install dpkg-dev cmake g++ gcc binutils libx11-dev libxpm-dev \ libxft-dev libxext-dev python3 libssl-dev libgsl0-dev libtiff-dev -y cd /opt wget https://root.cern/download/root_v6.30.04.Linux-ubuntu20.04-x86_64-gcc9.4.tar.gz tar -xzvf root_v6.30.04.Linux-ubuntu20.04-x86_64-gcc9.4.tar.gz %environment export PATH=/opt/root/bin:$PATH export LD_LIBRARY_PATH=/opt/root/lib:$LD_LIBRARY_PATH export PYTHONPATH=/opt/root/lib %runscript python3 /opt/root/tutorials/roofit/rf101_basics.py %labels Author HEPTraining

This time, the container will be based on a version of Ubuntu rather than CentOS, though Docker base layers (note the mandatory "BootStrap" line) are still used to form a rudimentary operating system that the Ubuntu layers will be added to in building the container.  This definition file is more complex than the previous one, so here is a breakdown of it, section by section.

The "%help" section is just text, whatever you'd like to put in that may be helpful to someone running the container.

The "%post" section contains instructions to download files, install things, update things, create files, etc.  This is all done in a clean environment, so nothing the user building the container has set up in their environment will be used here.  Remember, when you see something like "cd /opt" in a definition file, it refers to the "/opt" in the container, not on the host machine where the container is being built.  DEBIAN_FRONTEND is set to "noninteractive" so no user interaction is needed during installs; this needs to be set here since otherwise it's not in the environment when things in the %post section are run, and the container build will hang or error out due to lack of user intervention.

The "%environment" section contains variables to effect the containerized environment during runtime.  So anything set up here will also not be available to things declared in the %post section.  So for instance, if DEBIAN_FRONTEND is set up in the "%environment" section, it might also be needed to set it up in the "%post" section as well.

The "%runscript" section contains items that will get written to a file in the container and executed when the container runs.  Any arguments on the command line after the container name when it runs, can be handled in this section, if any are needed as input to software that is installed in the container.  The shell used by the container here defaults to bash or sh but can be changed by adding a line just like you would at the beginning of a shell script to specify the shell to use:  "#!/bin/csh",  for example.

The "%labels" section lets metadata for the container be defined.  Items in this section are in name- or key-value pairs.

There are more sections that can be defined than this, and sections can be repeated, and in any order.  Just be sure to keep the Header section (the Boostrap line) at the top of the file.

Now that the definition file has been created, the container can be built using the apptainer "build" argument.  It can take quite a bit of time to build a container, as things may take a while to download, and building code can also be time consuming.  Then too, there may be contention on the node where the container build is occurring, it's always a good thing to bear that in mind, as other users' processes may be running.  Here is an example of how to build the above definition file into an Apptainer container; there will be 100s of lines of output during the build so they won't be included here:

[user@athena102: ~/my_containers]$ apptainer build rootInUbuntuPython3.sif rootInUbuntuPython3.def

At the time of this writing, running this command on athena102 with a fairly light load (load average: 19.02, 27.26, 22.90), this build took a little over 7 minutes.  Here is a comparison of the sizes of this container, and the one built in the previous example:

[user@athena102: ~/my_containers]$ ls -lah myPrimes.sif -rwxr-x--- 1 user user 72M Feb 28 12:30 myPrimes.sif [user@athena102: ~/my_containers]$ ls -lah rootInUbuntuPython3.sif -rwxr-x--- 1 user user 770M Mar 28 10:03 rootInUbuntuPython3.sif

The second .sif file is over 10 times as large as the first, due to the ROOT package, installing Python3 into it, and so on.  The potential size of a resulting container may be a consideration also, and it will be wise to add elements to the definition files only as needed, to keep containers as lean as possible.

Now that the ROOT container is built, here is an example of running it:

[user@athena102: ~/my_containers]$ apptainer exec ./rootInUbuntuPython3.sif root /opt/root/tutorials/roofit/rf101_basics.C -q INFO: gocryptfs not found, will not be able to use gocryptfs ------------------------------------------------------------------ | Welcome to ROOT 6.30/04 https://root.cern | | (c) 1995-2024, The ROOT Team; conception: R. Brun, F. Rademakers | | Built for linuxx8664gcc on Jan 31 2024, 11:27:19 | | From heads/master@tags/v6-30-04 | | With c++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 | | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q' | ------------------------------------------------------------------ Processing /opt/root/tutorials/roofit/rf101_basics.C... [#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization [#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization RooRealVar::mean = 1.01746 +/- 0.0300144 L(-10 - 10) RooRealVar::sigma = 2.9787 +/- 0.0219217 L(0.1 - 10)

Using the "exec" argument to apptainer is another way to use an Apptainer container, this lets any executable file known to be stored in the container be used.  The Python3 that is stored in this container, for example, could be called from the command line using this method.  Since the %runscript section in the definitions file was defined, this container could also simply be run as an executable, itself, from the command line, which can be very handy for batch jobs as will be shown in the next section:

[user@athena102: ~/my_containers]$ ./rootInUbuntuPython3.sif root /opt/root/tutorials/roofit/rf101_basics.C -q [#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization [#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization RooRealVar::mean = 1.01746 +/- 0.0300144 L(-10 - 10) RooRealVar::sigma = 2.9787 +/- 0.0219217 L(0.1 - 10) Info in <TCanvas::Print>: png file rf101_basics.png has been created

Before moving on to the next section, note that both of these runs of this software produced a .png file as output.  Some outputs, like this, will need to be scp-ed to the user's local machine or the head node, to be viewed in an image viewer or other data handler.

Batch job

Remember:  batch job to build my primes.f90, then one to run it.

Show a batch job to containerize & run the ROOT software mentioned above.

More Information:

Docker Hub is probably the biggest location for images to be used with Singularity/Apptainer; you may wish to search their for software that interests you. 

Sylabs Singularity Container Library is another major source of images.

Extra

This is the Fortran code to print some prime numbers, mentioned above, in case it is interesting or useful to anyone.  Feel free to copy, edit it, and use it to experiment with making Apptainer/Singularity projects as you wish.

 

program primes implicit none integer :: i i = 1 print *, "Prime numbers, 1-30." do i=1, 30 if (prime(i)) then write (*, '(I2, A10)') i, " is prime." end if end do contains function prime(n) implicit none logical :: prime integer :: i, j integer, intent(IN) :: n i = 0 j = 0 if (2 > n) then prime = .false. return else if (2 == n) then prime = .true. return else if (0 == mod(n, 2)) then prime = .false. return else i = 3 do while ((i * i) <= n) j = i do while ((j <= (i*i))) if (0 == mod(n, j)) then prime = .false. return end if j = j + 1 end do i = i + 2 end do end if prime = .true. end function prime end program primes