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:

  1. Ensures Consistency across different environments (e.g., development, testing, production).

  2. Isolates Applications, preventing conflicts and improving security.

  3. Makes Deployment Easy by packaging everything needed to run an app in one container.

  4. 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.


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)

Run the Container Locally:

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

Tag and Push the Image: (change username to your docker hub account username)

On the VM, Pull and Run the Image:

You can follow Jetstream 2 tutorial to connect to a jetstream 2 vm

  1. Pull the Docker Image:

    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.

  2. Run the Docker Container:

    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:

On a compute node:

Example on Grace, batch job

Create a file named singularity_pull.sh:

On a login node,


Interact with container

make sure you are on a compute node: srun --pty --time=00:30:00 --mem=10G --ntasks=1 bash -i

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.

Example:

Executing commands

The exec command allows you to execute a custom command within a container by specifying the image file and the 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:

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

Running a container

Execute the default runscript defined in the container

--pwd /app specifies the working directory in the container.

Last updated