container

package
v1.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

container

Container setup pipeline providing overlay resolution, bind path deduplication, environment collection, and GPU detection for Apptainer execution.

Architecture

setup.go     Main setup pipeline, SetupConfig/SetupResult
resolve.go   Overlay path resolution (name → absolute path)
bind.go      Bind path deduplication and validation
env.go       Environment variable collection from .env files
gpu.go       GPU detection (NVIDIA, AMD) and flag generation
path.go      Container path utilities

Key Types

SetupConfig - Input configuration:

  • Overlays - Overlay paths or names (resolved automatically)
  • WritableImg - Whether .img overlays are writable
  • EnvSettings - User environment variables (KEY=VALUE)
  • BindPaths - User bind paths
  • Fakeroot - Use fakeroot
  • ApptainerFlags - Additional flags

SetupResult - Processed output ready for execution:

  • Overlays - Resolved ordered overlay paths
  • OverlayArgs - Paths with :ro/:rw suffixes
  • EnvList - Complete environment variable list
  • EnvNotes - Environment descriptions for display
  • BindPaths - Deduplicated bind paths
  • Fakeroot - Final fakeroot setting
  • ApptainerFlags - Flags including GPU detection
  • LastImg - Path to .img overlay (if present)

Important Diff from Apptainer Flags

Writable in CondaTainer means making the ext3 .img overlay writable, not adding --writable to Apptainer. The .img overlay is writable by default when used as an overlay.

By default, CondaTainer will add :ro suffix to all overlays for safety. If WritableImg is true, the .img overlay will have :rw to allow writing, but the .sqf overlays will still be :ro.

Usage

Setup Pipeline
cfg := container.SetupConfig{
    Overlays:    []string{"cellranger/9.0.1", "custom.sqf"},
    WritableImg: true,
    EnvSettings: []string{"DEBUG=1"},
    BindPaths:   []string{"/data"},
    Fakeroot:    false,
}

result, err := container.Setup(cfg)
if err != nil {
    // Handle errors
}

// Use result for execution
opts := &apptainer.ExecOptions{
    Overlay:    result.OverlayArgs,
    Env:        result.EnvList,
    Bind:       result.BindPaths,
    Fakeroot:   result.Fakeroot,
    Additional: result.ApptainerFlags,
}
Overlay Resolution
// Resolve overlay names to absolute paths
// Searches: absolute path → images dirs
overlays, err := container.ResolveOverlayPaths([]string{
    "cellranger/9.0.1",        // → /path/cellranger--9.0.1.sqf
    "/abs/path/custom.sqf",    // → /abs/path/custom.sqf
    "myenv.img",               // → /path/myenv.img
})
GPU Detection
// Auto-detect GPUs and generate flags
gpuFlags := container.DetectGPUFlags()
// Returns: ["--nv"] for NVIDIA, ["--rocm"] for AMD, [] otherwise
Fakeroot Management
// Auto-enable fakeroot for writable .img
fakeroot := container.AutoEnableFakeroot(
    lastImg="/path/env.img",
    writableImg=true,
    currentFakeroot=false,
)
// Returns: true (auto-enabled)

Setup Workflow

  1. Resolve overlays - Name/path → absolute paths
  2. Validate - Ensure at most one .img overlay
  3. Order - Place .img last (required for writable overlay layer)
  4. Lock check - Verify .img availability (exclusive lock if writable)
  5. Build overlay args - Add :ro/:rw suffixes
  6. Collect environment - Resolve #ENV: for each overlay (embedded build script, sidecar .env overrides)
  7. Deduplicate binds - Remove conflicting bind paths
  8. Detect GPU - Add --nv or --rocm if available
  9. Return result - Ready-to-use configuration

Overlay Ordering

  • SquashFS (.sqf) overlays are read-only, order matters for file precedence
  • ext3 (.img) overlay must be last (writable layer)
  • Automatic reordering ensures correct layering

Environment Variables

Each overlay's environment comes from the #ENV:/#ENVNOTE: directives in the build script embedded inside it (/cnt/<name>/<version>/.cnt-build-script), read directly from the .sqf via unsquashfs -cat — no mount, no sidecar required. $app_root is resolved to the overlay's mount root (/cnt/<name>/<version>) at load time.

A sidecar <overlay>.env (e.g. cellranger--9.0.1.sqf.env) is optional and, when present, shadows the embedded values for local overrides:

CELLRANGER_ROOT=/cnt/cellranger/9.0.1
PATH=/cnt/cellranger/9.0.1/bin:$PATH

CollectOverlayEnv merges every overlay's resolved env into the final list; ResolveOverlayEnv returns a single overlay's whatis/env/notes for info.

Common Environment:

  • LC_ALL=C.UTF-8, LANG=C.UTF-8
  • CURL_CA_BUNDLE=, SSL_CERT_FILE= (unset to avoid host interference)

Automatic Bind Paths

BindPaths() in bind.go collects bind mounts automatically before the user-specified paths are appended:

Source Condition
Current working directory Always
$SCRATCH When env var is set
$TMPDIR When env var is set and the path exists
Scheduler node-local tmp When inside a job exposing one (e.g. SLURM_TMPDIR)
Base data directories From config; added :ro if not writable
condatainer executable For nested calls (always; bound to /usr/bin/condatainer)

After collection, DeduplicateBindPaths() removes conflicting bind paths:

  • Keeps longest/most specific paths
  • Removes parent paths when child is bound
  • Example: /home/user/data removes /home/user

GPU Detection

Auto add flags based on available devices:

  • NVIDIA: Checks for /dev/nvidia*, adds --nv
  • AMD: Checks for /dev/kfd, adds --rocm

Error Handling

  • Overlay not found → search all images directories
  • Multiple .img overlays → error (only one writable layer allowed)
  • Locked .img → error with suggestion to check running instances
  • Invalid bind path → validation error

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindPaths

func BindPaths(paths ...string) []string

BindPaths collects all directories suitable for --bind flags. Deduplication and child path filtering is handled by DeduplicateBindPaths().

func BuildPathEnv

func BuildPathEnv(overlays []string) string

BuildPathEnv constructs the PATH environment variable based on overlays. When MPI_DIR is set (exported by condatainer when ntasks > 1), its bin/ is prepended so MPI tools are accessible inside the container.

func DeduplicateBindPaths

func DeduplicateBindPaths(paths []string) []string

DeduplicateBindPaths resolves, deduplicates, and filters child paths from bind directories. It handles formats: "/path", "/path:/container", "/path:/container:ro"

func DetectGPUFlags

func DetectGPUFlags() []string

DetectGPUFlags inspects the host for GPU support and returns the necessary apptainer flags.

func FormatOverlayMount

func FormatOverlayMount(path string, writable bool) string

FormatOverlayMount formats an overlay path with the appropriate :ro or :rw suffix

func InstalledOverlays

func InstalledOverlays() (map[string]string, error)

InstalledOverlays scans all image search paths and returns a mapping from normalized overlay names to their absolute file path. Searches user → scratch → legacy → system directories. First match wins (user overlays shadow system ones). Result is cached for the lifetime of the process; call InvalidateInstalledOverlaysCache after installing a new overlay.

func InvalidateInstalledOverlaysCache added in v1.3.0

func InvalidateInstalledOverlaysCache()

InvalidateInstalledOverlaysCache clears the cached overlay map so the next call to InstalledOverlays rescans the image directories.

func ResolveOverlayEnv added in v1.5.1

func ResolveOverlayEnv(overlayPath string) (whatis string, configs, notes map[string]string)

ResolveOverlayEnv resolves a single overlay's description (#WHATIS:), env vars, and notes for display. The embedded build script is the source of truth (.sqf); a sidecar <overlay>.env shadows it. $app_root is substituted with the overlay's mount root. A :ro/:rw suffix on the path is ignored.

func ResolveOverlayPaths

func ResolveOverlayPaths(inputs []string) ([]string, error)

ResolveOverlayPaths coerces user input into absolute overlay paths that exist on disk.

Types

type Diagnostic added in v1.5.0

type Diagnostic struct {
	Level   string
	Message string
}

Diagnostic is a non-fatal setup message returned to presentation layers.

func AutoEnableFakeroot

func AutoEnableFakeroot(lastImg string, writable bool, currentFakeroot bool) (bool, []Diagnostic)

AutoEnableFakeroot checks if fakeroot should be auto-enabled for writable .img overlays Returns the updated fakeroot setting

func CollectOverlayEnv

func CollectOverlayEnv(paths []string) (map[string]string, map[string]string, []Diagnostic)

CollectOverlayEnv resolves environment variables for the given overlay paths and returns environment configs, notes, and non-fatal diagnostics for callers to present or log.

For each overlay the embedded build script (/cnt/<name>/<version>/.cnt-build-script) is the source of truth, keeping the .sqf self-contained; a sidecar <overlay>.env, if present, shadows it for local overrides. $app_root is substituted with the overlay's mount root at load time. When the same variable is set by more than one overlay, the later overlay wins and a diagnostic is recorded.

type SetupConfig

type SetupConfig struct {
	Overlays       []string // Overlay paths (will be resolved)
	WritableImg    bool     // Whether .img overlays should be writable
	EnvSettings    []string // User-specified environment variables (KEY=VALUE format)
	BindPaths      []string // User-specified bind paths
	Fakeroot       bool     // Whether to use fakeroot
	ApptainerFlags []string // Additional apptainer flags to pass through
}

SetupConfig holds all configuration needed to set up a container

type SetupResult

type SetupResult struct {
	Overlays       []string          // Resolved and ordered overlay paths
	OverlayArgs    []string          // Overlay paths with :ro/:rw suffixes
	EnvList        []string          // Complete environment variable list
	EnvNotes       map[string]string // Environment variable notes for display
	Diagnostics    []Diagnostic      // Non-fatal messages for callers to present or log
	BindPaths      []string          // Deduplicated bind paths
	Fakeroot       bool              // Final fakeroot setting (may be auto-enabled)
	ApptainerFlags []string          // Apptainer flags including GPU flags
	LastImg        string            // Path to .img overlay if present
}

SetupResult contains all the processed configuration ready for container execution

func Setup

func Setup(cfg SetupConfig) (*SetupResult, error)

Setup processes all container configuration and returns a ready-to-use result

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL