This is an old revision of the document!


How to create a simple apptainer job.

Consider the case where you need to create a working python3 distrib with pytorch installed for running a script using GPU resources on ChaCha or DISCO. On those machines, we use Apptainer for the containers and this documentation describes how to create, customize and run images using Apptainer. Launching Apptainer tasks using Slurm is defined elsewhere.

Define the content of the container

Define a container definition, such as in rules.def (this correponds to a Dockerfile file):

Bootstrap: docker
From: nvidia/cuda:12.6.2-base-ubuntu22.04

%help
  This container provides a CUDA environment, with Python and Jupyternotebook.

%labels
  AUTHOR_NAME Pierre-André Mudry
  VERSION 1.0

%environment

%post -c /bin/bash
  export DEBIAN_FRONTEND=noninteractive
  apt-get -y update
  apt-get -y install git python3-pip python3-dev python3-opencv libglib2.0-0

%files
  requirements.txt requirements.txt

%post -c /bin/bash
  python3 -m pip install -r requirements.txt
  python3 -m pip install --upgrade pip
  pip3 install torch torchvision torchaudio -f https://download.pytorch.org/whl/cu111/torch_stable.html

This definition files is of a Debian distrib with CUDA, with python3 and pytorch installed and has other requirements as well. Those requirements are copied from you current directory requirements.txt file using the %files directive. In this example, we use for requirements.txt:

warp-lang
usd-core
matplotlib
pyglet
jupyter
python-language-server

Of course, you can tailor this to fit your needs.

Building the image

Once the definition is made, you can build the test.sif image with apptainer build test.sif rules.def. The image is created in your own directory. By default, when you run the image, it has access to the host filesystem. To run the image interactively (or not), you then simply run

Running the image

apptainer run --nv ./test.sif

The --nv flag must be used to use NVidia resources. If your run this script interactively, you then have access to the virtual environment. You can launch for instance nvidia-smi or jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser --allow-root to create a Juypter notebook system.

Edit this page
Back to top