Containers for HPC
How to package local code in a container and run it in the cloud
Intro to Docker
Docker is a tool that packages applications and their dependencies into containers, ensuring they run the same way on any system. It's useful because it:
Ensures Consistency across different environments (e.g., development, testing, production).
Isolates Applications, preventing conflicts and improving security.
Makes Deployment Easy by packaging everything needed to run an app in one container.
Increases Efficiency since containers are lightweight and use less resources compared to traditional virtual machines.
In short, Docker simplifies running and deploying applications by making them portable and consistent.
Following requires docker downloaded / docker account. You can skip these steps if you dont have them.
Download sample code
git clone https://github.com/sanjeev-one/Intro-to-Supercomputing-24---Duke-IEEE.git
The dockerfile tells docker how to setup the container:
```dockerfile
# Use the official Python image with version 3.8
FROM python:3.8-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . .
# Install any dependencies specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Run ml_app.py when the container launches
CMD ["python", "ml_app.py"]
```
Build and Run the Docker Container
Build the Docker Image:
In a local terminal that has the directory with ml_app.py open: (This may need logging into docker with docker login and using a dockerhub account)
docker build -t ml-app .
Run the Container Locally:
docker run ml-app
This will print the model's accuracy and the prediction for the sample flower measurements in your terminal.
Push to Docker Hub and Deploy on a VM - skip if doing workshop
Log In to Docker Hub: needs https://hub.docker.com/account
docker login
Tag and Push the Image: (change username to your docker hub account username)
docker tag ml-app username/ml-app
docker push username/ml-app
On the VM, Pull and Run the Image:
You can follow Jetstream 2 tutorial to connect to a jetstream 2 vm
Pull the Docker Image:
docker pull dukeieee/ml-app
This command downloads the Docker image
dukeieee/ml-app
from Docker Hub to your local system or VM. The image contains the Python ML app and its required dependencies.Run the Docker Container:
docker run dukeieee/ml-app
This command starts a container using the downloaded image. The app will train a machine learning model on the iris dataset and print the model's accuracy and predictions directly to your terminal.
This approach ensures that the app runs with all necessary dependencies, regardless of the environment, providing a consistent and reproducible setup.
HPC Specific Variants
Apptainer
Both Apptainer and Docker are containerization tools, but they have different primary use cases and features:
Apptainer:
Formerly known as Singularity, it's designed for high-performance computing (HPC) environments.
Focuses on user-level container management, which does not require root privileges.
Highly compatible with HPC batch systems and allows seamless integration into shared file systems.
Emphasizes security, enabling users to securely run containers without additional system privileges.
Running container on TAMU Faster
Authorized ACCESS users can log in using the Web Portal:
On a login node:
srun --nodes=1 --ntasks-per-node=4 --mem=30G --time=01:00:00 --pty bash -i
#(wait for job to start)
On a compute node:
cd $SCRATCH
export SINGULARITY_CACHEDIR=$TMPDIR/.singularity
module load WebProxy
singularity pull hello-world.sif docker://hello-world
singularity pull ml-app.sif docker://dukeieee/ml-app
#(wait for download and convert)
exit
Example on Grace, batch job
Create a file named singularity_pull.sh
:
#!/bin/bash
## JOB SPECIFICATIONS
#SBATCH --job-name=singularity_pull #Set the job name to "singularity_pull"
#SBATCH --time=01:00:00 #Set the wall clock limit to 1hr
#SBATCH --nodes=1 #Request 1 node
#SBATCH --ntasks=4 #Request 4 task
#SBATCH --mem=30G #Request 30GB per node
#SBATCH --output=singularity_pull.%j #Send stdout/err to "singularity_pull.[jobID]"
# set up environment for download
cd $SCRATCH
export SINGULARITY_CACHEDIR=$TMPDIR/.singularity
module load WebProxy
# execute download
singularity pull hello-world.sif docker://hello-world
singularity pull ml-app.sif docker://dukeieee/ml-app
On a login node,
sbatch singularity_pull.sh
#wait to complete
cd $SCRATCH
#see files
Interact with container
When a container image file is in place at HPRC, it can be used to control your environment for doing computation tasks.
These examples use a container image almalinux.sif
from https://hub.docker.com/_/almalinux, which is a lightweight derivative of the Redhat OS.
Shell
The shell command allows you to spawn a new shell within your container and interact with it one command at a time. Don't forget to exit
when you're done.
singularity shell <image.sif>
Example:
singularity shell ml-app.sif
Executing commands
The exec command allows you to execute a custom command within a container by specifying the image file and the command.
singularity exec <image.sif> <command>
The command can refer to an executable installed inside the container, or to a script located on a mounted cluster filesystem (see Files in and outside a container).
Example program installed inside image:
singularity exec almalinux.sif bash --version
Example executable file myscript.sh
:
starts with
#!/usr/bin/env bash
has the executable permission set by
chmod u+x myscript.sh
is located in the current directory
singularity exec almalinux.sif ./myscript.sh
Running a container
Execute the default runscript defined in the container
singularity run hello-world.sif
singularity run --pwd /app ml-app.sif
Last updated