Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:ARCANGEL0 EVA Container Lifecycle Management

From Leeroopedia
Knowledge Sources
Domains Containers, Infrastructure, Self_Hosting
Last Updated 2026-10-29 14:00 GMT

Overview

Pattern for deploying and managing loopback-only containerized services — checking runtime availability, creating containers with private configuration, probing readiness, and managing start/stop lifecycle across host reboots.

Description

When a framework needs a self-hosted service (e.g., a private SearXNG search instance), container lifecycle management automates:

  1. Runtime availability check: Detects Docker on PATH, verifies the daemon responds to `docker info`, and handles docker group membership (using `sg docker -c` to access a newly-granted group without process restart).
  2. Container creation: Pulls the official image, writes private settings into a config volume (with a random secret key, JSON API enabled, limiter disabled), and runs a detached container published only on 127.0.0.1.
  3. Readiness probing: Polls the service's JSON API endpoint for up to 75 seconds (1.5s intervals) to confirm the service is actually serving results, not just started.
  4. Lifecycle management: `ensure_running` starts a stopped container after a host reboot and probes for 45s; `stop` halts the container with a 3s grace period while keeping the image and config volume.

Key design decisions:

  • Loopback-only binding: The container is published on 127.0.0.1, never on 0.0.0.0 — preventing external access.
  • Private config volume: Container settings are written to a named volume, not a bind mount, for portability.
  • Random secret key: Each container gets a cryptographically random `secret_key` for session security.
  • Docker group handling: Uses `sg docker -c` to access a newly-granted docker group without requiring a process restart.

Usage

Apply this principle when:

  • Deploying a self-hosted service as a Docker container from within an application.
  • Needing to manage container lifecycle across host reboots.
  • Ensuring a containerized service is actually ready before using it.
  • Handling Docker group membership changes without restarting the parent process.

Theoretical Basis

The readiness probe follows a deadline-bounded polling loop:

# Abstract algorithm (NOT real implementation)
def wait_for_ready(base_url, deadline_s=75, interval_s=1.5):
    deadline = monotonic() + deadline_s
    while monotonic() < deadline:
        if probe_json_api(base_url):
            return True
        sleep(interval_s)
    return False

Container creation with loopback-only binding:

# Abstract algorithm (NOT real implementation)
def create_container(port, image):
    volume = "mitsu-searxng-config"
    write_settings(volume, settings_with_random_secret())
    docker_run(
        "--detach", "--name", container_name,
        "--publish", f"127.0.0.1:{port}:8080",
        "--volume", f"{volume}:/etc/searxng",
        image,
    )

Practical Guide

  • Bind to 127.0.0.1 only — never expose self-hosted services to the network.
  • Use a random secret key generated with `secrets.token_urlsafe()`.
  • Probe the JSON API endpoint, not just the TCP port — a started container may not be ready.
  • Use `sg docker -c` to access a newly-granted docker group without restarting the process.
  • Keep config in a named volume, not a bind mount, for portability.
  • Stop with a grace period (3s) and keep the image/volume so restart is fast.
  • Handle multiple image sources (GHCR, Docker Hub) for resilience.

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment