This lesson is in the early stages of development (Alpha version)

Building Containers

Overview

Teaching: 40 min
Exercises: 30 min
Questions
  • How to build containers with my requirements?

Objectives
  • Download and assemble containers from available images in the repositories.

Running containers from the available public images is not the only option. In many cases, it is required to modify an image or even to create a new one from scratch. For such purposes, Apptainer provides the command build, defined in the documentation as the Swiss army knife of container creation.

The usual workflow is to prepare and test a container in a build environment (like an interactive SLURM job), either with an interactive sandbox or from a definition file, and then to deploy the container into a production environment for execution. Interactive sandboxes are great to experiment and test your new container, or to troubleshoot issues that require changing the container composition. If you want to distribute the container or use it in production, then we recommend to build it from a definition file, as described in the next episode. This ensures the greatest possibility of reproducibility and transparency.

Build a container interactively

Notes on shared file systems

Avoid using network storage as a sandbox directory, as these systems can lead to permissions issues and very slow performance. At MSI, this applies to your home directory, group storage, and global scratch. Instead, use the local storage available inside a SLURM job via the --tmp flag. This storage corresponds to SSDs installed in the node you are running your job on. You can then work out of this directory for the duration of the job, but make sure to copy any results you want to save out of /tmp before the end of the job or it will be deleted.

Request dedicated computing resources

Let’s request some resources from SLURM, so that we have local storage and can run longer build processes than would be possible on our login nodes.

salloc --time=6:00:00 --nodes=1 --ntasks-per-node=8 --partition=agsmall,aglarge --tmp=48gb --mem=16g
salloc: Setting account: dunn0404
salloc: Pending job allocation 160383731
salloc: job 160383731 queued and waiting for resources
salloc: job 160383731 has been allocated resources
salloc: Granted job allocation 160383731
salloc: Waiting for resource configuration
salloc: Nodes acn40 are ready for job
ssh acn40
cd /tmp
module load apptainer

Create an editable ‘sandbox’ container

While images contained in the .sif files are more compact and immutable objects, ideal for reproducibility, when we want to build and test images it can be more convenient to use a sandbox, which can be easily modified.

The command build provides a flag --sandbox that will create a writable directory, myUbuntu, in your work directory:

apptainer build --sandbox myUbuntu docker://ubuntu:24.04

The container name is myUbuntu, and it has been initialized from the official Docker image of Ubuntu 24.04 We had to do a little extra setup for the sandbox because of some permissions peculiarities, by creating the users directory for the sandbox and changing the permissions for the whole sandbox.

To initialize an interactive session use the shell command. And to write files within the sandbox directory use the --writable option. Finally, the installation of new components will require superuser access inside the container, so use also the --fakeroot option, unless you are already root also outside.

apptainer shell --writable --fakeroot myUbuntu
Apptainer> whoami
root

Depending on the Apptainer installation (privileged or unprivileged) and the version, you may have some additional requirements, like the fakeroot utility or newuidmap and newgidmap. If you get an error when using --fakeroot have a look at the fakeroot documentation. MSI is currently using fakeroot for Apptainer on our clusters.

--fakeroot is not root

ATTENTION! --fakeroot allows you to be root inside a container that you own but is not changing who you are outside. All the outside actions and the writing on bound files and directories will happen as your outside user, even if in the container is done by root.

Installing software in the sandbox

As an example, let’s create a container with hmmer 3.4 available using the myUbuntu sandbox. First, we need to install the development tools (remember that in this interactive session we are superuser):

Apptainer> apt update
Apptainer> apt install build-essential wget

Where apt is the package manager used in Debian distributions (like Ubuntu).

We will follow the a modified version of the installation steps described in the hmmer website. Here is a summary of the commands you will need (you may need to adjust link and commands if there is a new Hmmer version):

Apptainer> mkdir /opt/hmmer && cd /opt/hmmer
Apptainer> wget http://eddylab.org/software/hmmer/hmmer.tar.gz
Apptainer> tar zxf hmmer.tar.gz
Apptainer> ln -s hmmer*/ src
Apptainer> cd src
Apptainer> ./configure --prefix=/opt/hmmer/install
Apptainer> make
Apptainer> make install

After the software has finished installing, let’s exit the shell and return to the host:

Apptainer> exit

Note that we installed this software into the /opt directory. We could have installed the software anywhere we liked, as we are able to freely modify the contents of the sandbox image. However, /opt is a good choice installing software into most images because in the Linux filesystem, the /opt directory is intended as a place to install optional or add-on software packages. By using /opt we avoid the chance of accidentally overwriting critical files in other directories, and other users who might use this image later will know to look in /opt for user-installed software.

Running the newly installed software

You can use now the container to run hmmer in a few steps. Let’s try one method, by printing the help documentation for nhmmer using apptainer exec:

 apptainer exec myUbuntu /opt/hmmer/install/bin/nhmmer -h
# nhmmer :: search a DNA model, alignment, or sequence against a DNA database
# HMMER 3.4 (Aug 2023); http://hmmer.org/
# Copyright (C) 2023 Howard Hughes Medical Institute.
# Freely distributed under the BSD open source license.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Usage: nhmmer [options] <query hmmfile|alignfile|seqfile> <target seqfile>

Basic options:
  -h : show brief help on version and usage
...

This works, but it isn’t the most convenient. We have to type the full path to the executable each time we run the command, because we installed to /opt and this directory isn’t searched for executables by default.

We can get around this if we want to use nhmmer via apptainer shell instead, by modifying the PATH environment variable inside the container. The PATH variable is a list of directories where the shell will search for executables whose name matches commands that you run.

apptainer shell myUbuntu

Note that this time we don’t need to pass the --writeable or --fakeroot options, since those are only required when we want to edit the sandbox. Right now we only want to run it.

Apptainer> export PATH=$PATH:/opt/hmmer/install/bin
Apptainer> nhmmer -h
# nhmmer :: search a DNA model, alignment, or sequence against a DNA database
...

This still isn’t ideal, because we still need to update the environment variable PATH each time we start a new shell in order to use nhhmmer in this way. We will automate this in the next section.

Execute Python with PyROOT available

Build a container to use uproot in Python 3.9.

Solution

Start from the Python 3.9 Docker image and create the myPython sandbox:

apptainer build --sandbox myPython docker://python:3.9
apptainer shell myPython

Once inside the container, you can install Uproot.

Apptainer> python3 -m pip install --upgrade pip
Apptainer> python3 -m pip install uproot awkward

Exit the container and use it as you like:

apptainer exec myPython python -c "import uproot; print(uproot.__doc__)"
Uproot: ROOT I/O in pure Python and NumPy.
...

Notice how we did not need neither --writable nor --fakeroot for the installation, but everything worked fine since pip installs user packages in the user $HOME directory. In addition, Apptainer by default mounts the user home directory as read+write, even if the container is read-only. This is why a sandbox is great to test and experiment locally, but should not be used for containers that will be shared or deployed. Manual changes and local directories are difficult to reproduce and control. Once you are happy with the content, you should use definition files, described in the next episode.

Key Points

  • The command build is the basic tool for the creation of containers.

  • A sandbox is a writable directory where containers can be built interactively.

  • Superuser permissions are required to build containers if you need to install packages or manipulate the operating system.

  • Use interactive builds only for development and tests, use definition files for production or publicly distributed containers.