container

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: BSD-3-Clause Imports: 11 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 - Load .env files for each overlay
  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 can have an associated .env file (e.g., cellranger--9.0.1.sqf.env):

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

Loaded automatically and merged into final environment list.

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
Base data directories From config; added :ro if not writable
condatainer executable For nested calls (non-portable installs only)

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 AutoEnableFakeroot

func AutoEnableFakeroot(lastImg string, writable bool, currentFakeroot bool) bool

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

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 CollectOverlayEnv

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

CollectOverlayEnv reads .env files from overlay paths and returns environment configs and notes

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 ResolveOverlayPaths

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

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

Types

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