Build a Singularity* Container for an MPI Application

There are several ways to build Singularity* containers described in the Singularity official documentation.

This section demonstrates how to build a container for an MPI application from scratch using recipes. Singularity recipes are files that include software requirements, environment variables, metadata, and other useful details for designing a custom container.

Recipe File Structure

A recipe file consists of the header and sections. The header part defines the core operating system and core packages to be installed. In particular:

  • Bootstrap - specifies the bootstrap module.
  • OSVersion - specifies the OS version. Required if only you have specified the %{OSVERSION} variable in MirrorURL.
  • MirrorURL - specifies the URL to use as a mirror to download the OS.
  • Include - specifies additional packages to be installed into the core OS (optional).

The content of a recipe file is divided into sections that execute commands at different times during the build process. The build process stops if a command fails. The main sections of a recipe are:

  • %help - provides help information.
  • %setup - executes commands on the host system outside of the container after the base OS is installed.
  • %post - executes commands within the container after the base OS has been installed at build time.
  • %environment - adds environment variables sourced at runtime. If you need environment variables sourced during build time, define them in the %post section.

Sample Recipe File

This recipe file illustrates how to install Intel MPI Benchmarks in a container. To run your own application, customize the recipe with your application configuration and workload.  

BootStrap: yum
OSVersion: 7
MirrorURL: <URL>
Include: yum
###############################################################################
%setup
###############################################################################
### installing runtime for host machine ###
yum-config-manager --add-repo https://yum.repos.intel.com/mpi/setup/intel-mpi.repo
yum install intel-mpi-2019.1-053 -y
source /opt/intel/compilers_and_libraries_2019.1.144/linux/mpi/intel64/bin/mpivars.sh -ofi_internal
base=`pwd`
echo "Get IMB code"
rm -rf mpi-benchmarks
git clone https://github.com/intel/mpi-benchmarks.git
cd mpi-benchmarks
echo "Build the code "
CC=mpicc CXX=mpicxx make IMB-MPI1
### Build benchmarks ###
mkdir -p $SINGULARITY_ROOTFS/benchmarks
if [ ! -x "$SINGULARITY_ROOTFS/benchmarks" ]; then
echo "failed to create benchmarks directory..."
exit 1
fi
echo "Copy all the binaries and anything else needed to run your binaries"
cp $base/mpi-benchmarks/IMB-MPI1 $SINGULARITY_ROOTFS/benchmarks
chmod -R 777 $SINGULARITY_ROOTFS/benchmarks
### install OPA ###
mkdir -p $SINGULARITY_ROOTFS/tempdir
if [ ! -x "$SINGULARITY_ROOTFS/tempdir" ]; then
echo "failed to create tempdir directory..."
exit 1
fi
cp -r $base/IntelOPA-IFS.RHEL74-x86_64.10.8.0.2.1/. $SINGULARITY_ROOTFS/tempdir
if [ ! -x "$SINGULARITY_ROOTFS/tempdir/INSTALL" ]; then
echo "No INSTALL in directory..."
exit 1
fi
exit 0
###############################################################################
%post
###############################################################################
### installing runtime libs for virtual machines ###
yum -y install perl atlas libpsm2 infinipath-psm libibverbs qperf pciutils tcl tcsh expect sysfsutils
yum -y install librdmacm libibcm perftest rdma bc elfutils-libelf-developenssh-clients openssh-server
yum -y install libstdc++-devel gcc-gfortran rpm-buildx compat-rdma-devel libibmad libibumad
yum -y install ibacm-devel libibumad-devel libibumad-static libuuid-devel pci-utils which
yum -y install iproute net-tools libhfi1 opensm-libs numactl-libs numactl
yum install -y yum-utils
yum-config-manager --add-repo https://yum.repos.intel.com/mpi/setup/intel-mpi.repo
yum install -y intel-mpi-2019.1-053
### install OPA driver ###
cd tempdir ; ./INSTALL --user-space -n ; cd ..; rm -rf tempdir
source /opt/intel/compilers_and_libraries_2019.1.144/linux/mpi/intel64/bin/mpivars.sh
################################################################################

Build a Container

After the recipe file is created, use it to create a Singularity container. The example below shows how to build a container with default parameters:

$ singularity build mpi.img ./Singularity_recipe_mpi

You can now launch your MPI application from a Singularity container.

Optimization Notice: 
For more complete information about compiler optimizations, see our Optimization Notice.
Select sticky button color: 
Orange (only for download buttons)