Skip to content

5. Running a container, both interactively ( only the shell or inspect ) and via Slurm

Working with symlinked directories

If you are to access the apptainer image directory via a Symlink and execute the upcoming apptainer commands, it will trigger a warning similar to below

WARNING: Error changing the container working directory. Using '/users/....' instead: chdir /users/...: no such file or directory

The issue occurs because:

  1. Your shell resolves the symlink: When you cd symlinkeddir/, your shell follows the symlink and your $PWD becomes /users/... (the symlink path)
  2. Apptainer tries to bind the literal path: Apptainer attempts to set the working directory inside the container to match your current $PWD, but it's working with the symlink path /users/...
  3. The symlink doesn't exist inside the container: Even though you've bound the entire filesystem with APPTAINER_BIND, the symlink itself may not be properly resolved or may not exist in the container's filesystem view

Solutions

  • Option 1: Always use the absolute paths

  • Option 2 : Use cd -P to resolve symlinks

  • To have a look at the contents of your container, you can "shell" into it using apptainer shell containername .

    apptainer shell trimmomatic_0.40.aif
    
    Note - Above shell command will enter the shell within the container image. Note the prompt is now prefixed with "Apptainer",
    Apptainer>
    
    Exit the container by running the command exit which will bring you back to the host system
    Apptainer> exit
    

  • To view metadata and configuration details about an Apptainer container image, use apptainer inspect containername

    apptainer inspect trimmomatic_0.40.aif
    

  • apptainer exec command

    • The apptainer exec command allows you to run a specific command inside an Apptainer container, making it ideal for executing scripts, tools, or workflows within the container’s environment. For example, you can run a Python script or query the container’s filesystem without entering an interactive shell. Let't say you have a python script ( let's call it my_tensorflow.py) using Tensorflow libraries with a help menu and assuming you have Tensorflow container with all required libraries
    apptainer exec trimmomatic_0.40.aimg trimmomatic --help
    
  • The apptainer run command, on the other hand, is designed to execute the default process defined in the container (such as its runscript), providing a convenient way to start the container’s main application or service as intended by its creator. This is commonly used when you want to use the container as a standalone executable.

Slurm template

#!/bin/bash -e

#SBATCH --job-name=trimmomatic-via-apptainer
#SBATCH --time=00:10:00
#SBATCH --mem=4G
#SBATCH --cpus-per-task=4

# Run container %runscript
apptainer exec trimmomatic_0.40.aimg trimmomatic --help some_argument

Accessing GPUs via Apptainer

Accessing GPUs via apptainer requires --nv flag

  • If your Slurm job has requested access to an NVIDIA GPU, an Apptainer container can transparently access it using the --nv flag:

    apptainer exec --nv my_container.sif
    
  • For an example,

    apptainer exec --nv tensorflow-latest-gpu.aif my_tensorflow.py some_argument