Containers

From ACCRE Wiki

Revision as of 20:25, 14 April 2025 by Dhakas1 (talk | contribs)

Containers and Apptainer/Singularity

Containers provide lightweight, virtualized operating environments that allow you to run an application and its dependencies in resource-isolated processes. They enable developers to package an application with all its dependencies into a standardized unit for software development.

Unlike traditional virtual machines (VMs) that virtualize the entire hardware stack, containers virtualize at the OS level, with multiple containers running directly on top of the OS kernel. This makes them more portable, efficient, and scalable than VMs.

Apptainer/Singularity

Apptainer (previously known as Singularity) is a tool that allows containers to run within a shared high-performance computing environment. This empowers users to control the OS environment (including software libraries) in which their jobs execute.

For instance, a user can run within an Ubuntu 16.04 environment even if the underlying OS on the ACCRE cluster is a different Linux distribution (e.g., Rocky Linux). Docker containers, for security reasons, cannot be directly run in shared environments like the ACCRE cluster. However, Docker container images can be easily used as Apptainer container images, which are safe for cluster execution.

Note: The terms Singularity and Apptainer are used interchangeably in this document.

When running within an Apptainer container, a user retains the same permissions and privileges they would have outside the container. An Apptainer image typically needs to be developed and built on a Linux machine where you have administrative access (e.g., a personal machine). If you lack administrative access to a Linux machine, you can create a virtual Linux machine using free tools like Vagrant or VirtualBox. Singularity offers helpful documentation on this process here.

A user's cluster storage is accessible from within the Apptainer container, but operations requiring root/sudo privileges (such as installing system software) are not permitted within the container's context when run on the cluster.

Building an Apptainer Image

Once you have installed Apptainer, create a spec file named Apptainer with the following content:

BootStrap: docker
From: ubuntu:16.04

%runscript
    echo "This is what happens when you run the container..."

%post
    apt-get update
    apt-get -y install python3 python3-numpy python3-scipy
    mkdir /nobackup /data /panfs

To build the image:

sudo apptainer build ubuntu16-accre.sif Apptainer

Building Containers from Docker Hub or Singularity Hub

You can use:

apptainer pull ubuntu16-accre.sif docker://library/ubuntu:16.04

Running a Singularity Image on the Cluster

To enter an interactive shell:

apptainer shell ubuntu16-accre.sif

To run a script:

apptainer exec ubuntu16-accre.sif /path/to/my/script.sh

To see the Linux distro change:

cat /etc/*release
apptainer shell ubuntu16-accre.sif
cat /etc/*release
exit

Running GPU-Enabled Containers

Use the --nv option:

BootStrap: docker
From: tensorflow/tensorflow:latest-gpu-py3

%help
    Apptainer image with TensorFlow 1.7 and Keras running on python 3.5 with GPU support.

%post
    export PATH=/bin:/sbin:/usr/bin:/usr/sbin:$PATH
    pip3 install -U keras
    mkdir /nobackup /data /panfs

Run it:

apptainer exec --nv my-singularity-image.sif python -c "import tensorflow"

Example SLURM script:

#!/bin/bash
#SBATCH --account=MY_GPU_ACCOUNT
#SBATCH --partition=pascal
#SBATCH --gres=gpu:1
#SBATCH --mem=10G
#SBATCH --time=12:00:00

git clone https://github.com/tensorflow/models.git
apptainer exec --nv docker://tensorflow/tensorflow:latest-gpu \
    python ./models/tutorials/image/mnist/convolutional.py

Docker

Apptainer supports Docker images. Users can build Docker images locally and use them on the cluster.

Writing a Dockerfile

FROM python:3.7-slim
WORKDIR /usr/src/app
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 80
ENV NAME World
CMD ["python", "app.py"]

Creating and Running the Container

docker build -t my-python-app .
docker run -p 4000:80 my-python-app

Uploading to Docker Hub

1. Login:

docker login

2. Tag the image:

docker tag my-python-app username/my-python-app

3. Push it:

docker push username/my-python-app

Full instructions: Docker Hub Docs

Saving Docker Image and Using with Apptainer

1. Save image:

docker save -o my-python-app.tar my-python-app

2. Transfer it:

scp my-python-app.tar user@cluster:/path/to/destination

3. Convert it:

apptainer build my-python-app.sif docker-archive://my-python-app.tar

4. Run it:

apptainer run my-python-app.sif

Commonly Encountered Errors

Missing Libraries

  • Ensure libraries are listed in requirements.txt
  • Use -B flag to bind paths:
apptainer run -B /accre,/cvmfs,/data/your_datapath:/workdir/data/ container.sif

Broken Links/Symlinks

  • Ensure target files/directories exist inside container
  • Use docker run -v to map volumes

Permission Issues

  • Use chmod and chown appropriately
  • Ensure correct user permissions in container

Environment Variables Not Set

  • Set them in Dockerfile using ENV or in Singularity file using %environment
  • Or at runtime:
env_var1=val1 singularity run -e "VARIABLE_NAME=value" my-image

Conflicting Environment Variables

Use --cleanenv to isolate container environment:

apptainer run --cleanenv ./lolcow.sif

ACCRE Singularity GitHub Repository

ACCRE maintains a GitHub repo: https://github.com/accre/singularity

We encourage users to contribute their examples!