Containers: Difference between revisions

From ACCRE Wiki

No edit summary
Line 1: Line 1:
= 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.
 
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/Singularity ==


[https://apptainer.org/ 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.  
[https://apptainer.org/ 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.
 
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.


<blockquote style="background-color: #f0f8ff; padding: 10px;">
<blockquote style="border-left: 4px solid #ccc; padding-left: 10px;">
'''Note:''' The terms ''Singularity'' and ''Apptainer'' are used interchangeably in this document.
'''Note:''' The terms "Singularity" and "Apptainer" are used interchangeably in this document.
</blockquote>
</blockquote>


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 [https://www.vagrantup.com/ Vagrant] or [https://www.virtualbox.org/ VirtualBox]. Singularity offers helpful documentation on this process [https://docs.sylabs.io/guides/latest/user-guide/ here].
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 [https://www.vagrantup.com/ Vagrant] or [https://www.virtualbox.org/ VirtualBox]. Singularity offers helpful documentation on this process [https://docs.sylabs.io/guides/latest/user-guide/ 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.
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. Below are basic instructions for running Apptainer on the cluster. It is highly recommended to explore the comprehensive Apptainer documentation. Please also refer to the [https://www.youtube.com/watch?v=Hj5eTZGUsDM apptainer deep dive video] provided by the CIQ.


=== Building an Apptainer Image ===
=== Building an Apptainer Image ===


Once you have [https://apptainer.org/user-docs/master/ installed Apptainer], create a spec file named <code>Apptainer</code> with the following content:
Once you have [https://apptainer.org/user-docs/master/ installed Apptainer on your own Linux machine or virtual machine], you are ready to create your image. First, create a spec file named Apptainer with the following content:


<pre>
<pre>
Line 28: Line 22:


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


%post
%post
    apt-get update
apt-get update
    apt-get -y install python3 python3-numpy python3-scipy
apt-get -y install python3 python3-numpy python3-scipy
    mkdir /nobackup /data /panfs
# install any other software you need here...
mkdir /nobackup /data /panfs
</pre>
</pre>


To build the image:
In this file:
 
* <code>BootStrap: docker</code> and <code>From: ubuntu:16.04</code> instruct Singularity to build an image based on the latest version of Ubuntu 16.04, pulling the base image from Docker Hub.
* The <code>%runscript</code> section defines commands or tasks that will execute each time a container of this image is run.
* The <code>%post</code> section contains one-time setup steps to be performed inside the image. Here, <code>apt-get</code> is used to install Python 3 with NumPy and SciPy. You can add commands to install any other necessary software.
* Creating the directories <code>/nobackup</code>, <code>/data</code>, and <code>/panfs</code> within the <code>%post</code> section is crucial for accessing your cluster storage from within the container. Failure to do so will result in a warning message when running the container on the cluster. Specifically, creating <code>/panfs</code> ensures access to your entire <code>/home</code> space from within the container.
 
To build the image, execute the following command on your Linux machine with administrative privileges:


<pre>
<pre>
# on your personal Linux machine
sudo apptainer build ubuntu16-accre.sif Apptainer
sudo apptainer build ubuntu16-accre.sif Apptainer
</pre>
</pre>


=== Building Containers from Docker Hub or Singularity Hub ===
=== Building Containers Directly from Docker Hub or Singularity Hub ===
 
Often, creating your own image is unnecessary as others may have already created suitable ones. [https://hub.docker.com/ Docker Hub] and [https://cloud.sylabs.io/library Sylabs Cloud] host registries of images contributed by various users. The example below, in fact, pulls its base image from Docker Hub. If you find an image you wish to use directly, you can either build it locally or run it directly.


You can use:
Building the image locally is advisable if you plan to run it multiple times, as it avoids repeated downloads from the web, which can be slow. You can use the <code>singularity pull</code> subcommand to pull a container from the cloud and create a runnable container image. Since pull command does not rely on having a sudo access, you can run pull command directly on ACCRE.


<pre>
<pre>
# From the cluster
apptainer pull ubuntu16-accre.sif docker://library/ubuntu:16.04
apptainer pull ubuntu16-accre.sif docker://library/ubuntu:16.04
</pre>
</pre>
If you need to modify the container (e.g., adding access to ACCRE storage), it's recommended to create a spec file as described earlier.


=== Running a Singularity Image on the Cluster ===
=== Running a Singularity Image on the Cluster ===


To enter an interactive shell:
Once your image is successfully created, you can run the container using Apptainer commands. Apptainer is one of our core software and doesn’t have to be loaded through module load command. You can access the container’s shell using the following command on the cluster:


<pre>
<pre>
# From the cluster
apptainer shell ubuntu16-accre.sif
apptainer shell ubuntu16-accre.sif
</pre>
</pre>


To run a script:
The <code>shell</code> subcommand is useful for interactive work. Note that any new data created inside the container will persist outside its context. For batch processing (e.g., within a SLURM job), use the <code>exec</code> subcommand instead. For example:


<pre>
<pre>
Line 64: Line 73:
</pre>
</pre>


To see the Linux distro change:
Here, <code>script.sh</code> contains the processing steps you want to execute within the batch job. You can also pass a generic Linux command to the <code>exec</code> subcommand. Pipes and redirected output are supported with <code>exec</code>. The following demonstrates the change in Linux distribution when entering a Apptainer container:


<pre>
<pre>
cat /etc/*release
[jill@vmps10 ~]$ cat /etc/*release
apptainer shell ubuntu16-accre.sif
CentOS release 6.9 (Final)
cat /etc/*release
...
[jill@vmps10 ~]$ apptainer shell ubuntu16-accre.sif
Apptainer> cat /etc/*release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
...
Apptainer ubuntu-16.04.simg:~> exit
exit
exit
</pre>
</pre>
Line 75: Line 90:
=== Running GPU-Enabled Containers ===
=== Running GPU-Enabled Containers ===


Use the <code>--nv</code> option:
Apptainer supports running on GPUs from within containers. The <code>--nv</code> option must be passed to the <code>exec</code> or <code>shell</code> subcommand. Here's an example of a spec file for creating a GPU-enabled Tensorflow/Keras image for use on ACCRE:


<pre>
<pre>
Line 82: Line 97:


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


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


Run it:
Instantiating a container from such an image would look like this:
 
<pre>
<pre>
apptainer exec --nv my-singularity-image.sif python -c "import tensorflow"
$ apptainer exec --nv my-singularity-image.sif python -c "import tensorflow"
</pre>
</pre>


Example SLURM script:
Alternatively, you can run a GPU-enabled image directly from Docker Hub. Here's an example SLURM script for running a GPU-enabled Tensorflow container on ACCRE GPUs:


<pre>
<pre>
Line 106: Line 120:
#SBATCH --time=12:00:00
#SBATCH --time=12:00:00


# Clone tensorflow repo to run a tutorial
git clone https://github.com/tensorflow/models.git
git clone https://github.com/tensorflow/models.git
apptainer exec --nv docker://tensorflow/tensorflow:latest-gpu \
apptainer exec --nv docker://tensorflow/tensorflow:latest-gpu \
    python ./models/tutorials/image/mnist/convolutional.py
python ./models/tutorials/image/mnist/convolutional.py
</pre>
</pre>
Again, we recommend you to use <code>apptainer pull</code> to build an image locally from docker hub for repeated usage.


== Docker ==
== Docker ==


Apptainer supports Docker images. Users can build Docker images locally and use them on the cluster.
Since Apptainer supports running Docker images, many users prefer to build docker image locally on their computer, then either create an image archive to transfer to the cluster using tools like scp, or upload the image to Docker Hub so it can be pulled directly from the cluster.


=== Writing a Dockerfile ===
=== Writing a Dockerfile ===
A Dockerfile is a text document containing all the commands a user would execute on the command line to assemble an image. Here's an example:


<pre>
<pre>
# 1. Use an official Python runtime as a parent image
FROM python:3.7-slim
FROM python:3.7-slim
# 2. Set the working directory in the container
WORKDIR /usr/src/app
WORKDIR /usr/src/app
# 3. Copy the current directory contents into the container at /usr/src/app
COPY . .
COPY . .
# 4. Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# 5. Make port 80 available to the world outside this container
EXPOSE 80
EXPOSE 80
# 6. Define environment variable
ENV NAME World
ENV NAME World
# 7. Run app.py when the container launches
CMD ["python", "app.py"]
CMD ["python", "app.py"]
</pre>
</pre>


=== Creating and Running the Container ===
=== Creating a Container Using Docker ===
 
To create a Docker container, first build an image from your Dockerfile, then run a container from this image:


<pre>
<pre>
Line 134: Line 168:
</pre>
</pre>


=== Uploading to Docker Hub ===
=== Creating a Docker Hub Account and Uploading the Container to Docker Hub ===


1. Login:
To share your Docker container images, you need to upload them to Docker Hub. First, [https://hub.docker.com/signup create a Docker Hub account]. Then follow these steps:
 
# Authenticate your Docker client to Docker Hub:
<pre>
<pre>
docker login
docker login
</pre>
</pre>


2. Tag the image:
# Tag your image to match your Docker Hub username:
<pre>
<pre>
docker tag my-python-app username/my-python-app
docker tag my-python-app username/my-python-app
</pre>
</pre>


3. Push it:
# Upload your image to Docker Hub:
<pre>
<pre>
docker push username/my-python-app
docker push username/my-python-app
</pre>
</pre>


Full instructions: [https://docs.docker.com/docker-hub/ Docker Hub Docs]
Complete instructions can be found [https://docs.docker.com/docker-hub/ here].


=== Saving Docker Image and Using with Apptainer ===
=== Saving a Docker Image and Running It with Apptainer ===


1. Save image:
If you prefer not to use Docker Hub, you can save your Docker image as an archive, transfer it to your cluster, and run it using Apptainer. Follow these steps:
 
# Save your Docker image as a tar archive
<pre>
<pre>
docker save -o my-python-app.tar my-python-app
docker save -o my-python-app.tar my-python-app
</pre>
</pre>


2. Transfer it:
# Transfer the image archive to your cluster using scp
<pre>
<pre>
scp my-python-app.tar user@cluster:/path/to/destination
scp my-python-app.tar user@cluster-address:/path/to/destination
</pre>
</pre>


3. Convert it:
# On the cluster, load and convert the Docker image using Apptainer
<pre>
<pre>
apptainer build my-python-app.sif docker-archive://my-python-app.tar
apptainer build my-python-app.sif docker-archive://my-python-app.tar
</pre>
</pre>


4. Run it:
# Run the container using Apptainer
<pre>
<pre>
apptainer run my-python-app.sif
apptainer run my-python-app.sif
Line 179: Line 217:
=== Missing Libraries ===
=== Missing Libraries ===


* Ensure libraries are listed in <code>requirements.txt</code>
'''Symptoms:''' Errors indicating that a library or dependency is not found when trying to run your application inside a container.
* Use <code>-B</code> flag to bind paths:


'''Possible Solutions:'''
* Ensure all necessary libraries are listed in your <code>requirements.txt</code> (for Python) or the equivalent dependency file.
* Double-check your Dockerfile to confirm the <code>COPY</code> command correctly transfers the dependency file and that the <code>RUN</code> command installs dependencies.
* Verify that you are correctly binding paths using the <code>-B</code> flag:
<pre>
<pre>
apptainer run -B /accre,/cvmfs,/data/your_datapath:/workdir/data/ container.sif
apptainer run -B /accre,/cvmfs,/data/your_datapath:/workdir/data/ container.sif
Line 188: Line 229:
=== Broken Links/Symlinks ===
=== Broken Links/Symlinks ===


* Ensure target files/directories exist inside container
'''Symptoms:''' The application fails because links point to files or directories that do not exist within the container's filesystem.
* Use <code>docker run -v</code> to map volumes
 
'''Possible Solutions:'''
* Confirm that the file or directory the link points to exists within the container.
* Ensure shared volumes are correctly mapped and accessible when running with <code>docker run -v</code>.


=== Permission Issues ===
=== Permission Issues ===


* Use <code>chmod</code> and <code>chown</code> appropriately
'''Symptoms:''' Errors related to file or directory permissions.
* Ensure correct user permissions in container
 
'''Possible Solutions:'''
* Set appropriate permissions using <code>chmod</code> and <code>chown</code> in the Dockerfile.
* Ensure the non-root user has necessary permissions.


=== Environment Variables Not Set ===
=== Environment Variables Not Set ===


* Set them in Dockerfile using <code>ENV</code> or in Singularity file using <code>%environment</code>
'''Symptoms:''' Application behavior suggests environment variables are missing or incorrect.
* Or at runtime:


'''Possible Solutions:'''
* Use the <code>ENV</code> instruction (Docker) or <code>%environment</code> section (Singularity) to set variables.
* Set environment variables at runtime:
<pre>
<pre>
env_var1=val1 singularity run -e "VARIABLE_NAME=value" my-image
env_var1=val1 env_var2=val2 singularity run -e "VARIABLE_NAME=value" my-image
</pre>
</pre>


=== Conflicting Environment Variables ===
=== Multiple Environment Variables Overlapping ===


Use <code>--cleanenv</code> to isolate container environment:
If you have Conda or pip installations in your profile folder, they might interfere with the container’s setup.


'''Solution:'''
<pre>
<pre>
apptainer run --cleanenv ./lolcow.sif
apptainer run --cleanenv ./lolcow.sif
Line 215: Line 265:
== ACCRE Singularity GitHub Repository ==
== ACCRE Singularity GitHub Repository ==


ACCRE maintains a GitHub repo: [https://github.com/accre/singularity https://github.com/accre/singularity]
ACCRE maintains a [https://github.com/accre/singularity Singularity GitHub repository] with more examples.
 
We encourage users to contribute their examples!
While we continuously work to update the content, due to the rapidly evolving nature of technology, some content in our GitHub Repository may be outdated. Therefore, we also encourage users to contribute their own examples to the repository!
While we continuously work to update the content, due to the rapidly evolving nature of technology, some content in our GitHub Repository may be outdated. Therefore, we also encourage users to contribute their own examples to the repository!

Revision as of 20:33, 14 April 2025

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. Below are basic instructions for running Apptainer on the cluster. It is highly recommended to explore the comprehensive Apptainer documentation. Please also refer to the apptainer deep dive video provided by the CIQ.

Building an Apptainer Image

Once you have installed Apptainer on your own Linux machine or virtual machine, you are ready to create your image. First, 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
	# install any other software you need here...
	mkdir /nobackup /data /panfs

In this file:

  • BootStrap: docker and From: ubuntu:16.04 instruct Singularity to build an image based on the latest version of Ubuntu 16.04, pulling the base image from Docker Hub.
  • The %runscript section defines commands or tasks that will execute each time a container of this image is run.
  • The %post section contains one-time setup steps to be performed inside the image. Here, apt-get is used to install Python 3 with NumPy and SciPy. You can add commands to install any other necessary software.
  • Creating the directories /nobackup, /data, and /panfs within the %post section is crucial for accessing your cluster storage from within the container. Failure to do so will result in a warning message when running the container on the cluster. Specifically, creating /panfs ensures access to your entire /home space from within the container.

To build the image, execute the following command on your Linux machine with administrative privileges:

# on your personal Linux machine
sudo apptainer build ubuntu16-accre.sif Apptainer

Building Containers Directly from Docker Hub or Singularity Hub

Often, creating your own image is unnecessary as others may have already created suitable ones. Docker Hub and Sylabs Cloud host registries of images contributed by various users. The example below, in fact, pulls its base image from Docker Hub. If you find an image you wish to use directly, you can either build it locally or run it directly.

Building the image locally is advisable if you plan to run it multiple times, as it avoids repeated downloads from the web, which can be slow. You can use the singularity pull subcommand to pull a container from the cloud and create a runnable container image. Since pull command does not rely on having a sudo access, you can run pull command directly on ACCRE.

# From the cluster
apptainer pull ubuntu16-accre.sif docker://library/ubuntu:16.04

If you need to modify the container (e.g., adding access to ACCRE storage), it's recommended to create a spec file as described earlier.

Running a Singularity Image on the Cluster

Once your image is successfully created, you can run the container using Apptainer commands. Apptainer is one of our core software and doesn’t have to be loaded through module load command. You can access the container’s shell using the following command on the cluster:

# From the cluster
apptainer shell ubuntu16-accre.sif

The shell subcommand is useful for interactive work. Note that any new data created inside the container will persist outside its context. For batch processing (e.g., within a SLURM job), use the exec subcommand instead. For example:

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

Here, script.sh contains the processing steps you want to execute within the batch job. You can also pass a generic Linux command to the exec subcommand. Pipes and redirected output are supported with exec. The following demonstrates the change in Linux distribution when entering a Apptainer container:

[jill@vmps10 ~]$ cat /etc/*release
CentOS release 6.9 (Final)
...
[jill@vmps10 ~]$ apptainer shell ubuntu16-accre.sif
Apptainer> cat /etc/*release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
...
Apptainer ubuntu-16.04.simg:~> exit
exit

Running GPU-Enabled Containers

Apptainer supports running on GPUs from within containers. The --nv option must be passed to the exec or shell subcommand. Here's an example of a spec file for creating a GPU-enabled Tensorflow/Keras image for use on ACCRE:

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

Instantiating a container from such an image would look like this:

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

Alternatively, you can run a GPU-enabled image directly from Docker Hub. Here's an example SLURM script for running a GPU-enabled Tensorflow container on ACCRE GPUs:

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

# Clone tensorflow repo to run a tutorial
git clone https://github.com/tensorflow/models.git
apptainer exec --nv docker://tensorflow/tensorflow:latest-gpu \
	python ./models/tutorials/image/mnist/convolutional.py

Again, we recommend you to use apptainer pull to build an image locally from docker hub for repeated usage.

Docker

Since Apptainer supports running Docker images, many users prefer to build docker image locally on their computer, then either create an image archive to transfer to the cluster using tools like scp, or upload the image to Docker Hub so it can be pulled directly from the cluster.

Writing a Dockerfile

A Dockerfile is a text document containing all the commands a user would execute on the command line to assemble an image. Here's an example:

# 1. Use an official Python runtime as a parent image
FROM python:3.7-slim

# 2. Set the working directory in the container
WORKDIR /usr/src/app

# 3. Copy the current directory contents into the container at /usr/src/app
COPY . .

# 4. Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# 5. Make port 80 available to the world outside this container
EXPOSE 80

# 6. Define environment variable
ENV NAME World

# 7. Run app.py when the container launches
CMD ["python", "app.py"]

Creating a Container Using Docker

To create a Docker container, first build an image from your Dockerfile, then run a container from this image:

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

Creating a Docker Hub Account and Uploading the Container to Docker Hub

To share your Docker container images, you need to upload them to Docker Hub. First, create a Docker Hub account. Then follow these steps:

  1. Authenticate your Docker client to Docker Hub:
docker login
  1. Tag your image to match your Docker Hub username:
docker tag my-python-app username/my-python-app
  1. Upload your image to Docker Hub:
docker push username/my-python-app

Complete instructions can be found here.

Saving a Docker Image and Running It with Apptainer

If you prefer not to use Docker Hub, you can save your Docker image as an archive, transfer it to your cluster, and run it using Apptainer. Follow these steps:

  1. Save your Docker image as a tar archive
docker save -o my-python-app.tar my-python-app
  1. Transfer the image archive to your cluster using scp
scp my-python-app.tar user@cluster-address:/path/to/destination
  1. On the cluster, load and convert the Docker image using Apptainer
apptainer build my-python-app.sif docker-archive://my-python-app.tar
  1. Run the container using Apptainer
apptainer run my-python-app.sif

Commonly Encountered Errors

Missing Libraries

Symptoms: Errors indicating that a library or dependency is not found when trying to run your application inside a container.

Possible Solutions:

  • Ensure all necessary libraries are listed in your requirements.txt (for Python) or the equivalent dependency file.
  • Double-check your Dockerfile to confirm the COPY command correctly transfers the dependency file and that the RUN command installs dependencies.
  • Verify that you are correctly binding paths using the -B flag:
apptainer run -B /accre,/cvmfs,/data/your_datapath:/workdir/data/ container.sif

Broken Links/Symlinks

Symptoms: The application fails because links point to files or directories that do not exist within the container's filesystem.

Possible Solutions:

  • Confirm that the file or directory the link points to exists within the container.
  • Ensure shared volumes are correctly mapped and accessible when running with docker run -v.

Permission Issues

Symptoms: Errors related to file or directory permissions.

Possible Solutions:

  • Set appropriate permissions using chmod and chown in the Dockerfile.
  • Ensure the non-root user has necessary permissions.

Environment Variables Not Set

Symptoms: Application behavior suggests environment variables are missing or incorrect.

Possible Solutions:

  • Use the ENV instruction (Docker) or %environment section (Singularity) to set variables.
  • Set environment variables at runtime:
env_var1=val1 env_var2=val2 singularity run -e "VARIABLE_NAME=value" my-image

Multiple Environment Variables Overlapping

If you have Conda or pip installations in your profile folder, they might interfere with the container’s setup.

Solution:

apptainer run --cleanenv ./lolcow.sif

ACCRE Singularity GitHub Repository

ACCRE maintains a Singularity GitHub repository with more examples. While we continuously work to update the content, due to the rapidly evolving nature of technology, some content in our GitHub Repository may be outdated. Therefore, we also encourage users to contribute their own examples to the repository!