tugtainer Documentation

repository·main·Indexed 23 days ago

https://github.com/quenary/tugtainer

A self-hosted application for automating the monitoring and updating of Docker containers with a web UI. Version 1.31.0 supports multiple hosts via agents, complex container dependency graphs, customizable update hooks, and notifications via Apprise and Jinja2. It includes a FastAPI backend with a public API and a dedicated agent for secure remote Docker access.

Tokens
10.2K
Snippets
27
Records
73
Agent score
81%

What's inside tugtainer

  1. Deploy the Tugtainer Agent for remote Docker access

    main
    The Tugtainer Agent is a component of the Tugtainer application that can be deployed in a separate container. Its primary purpose is to provide secure remote access to Docker, allowing the Tugtainer backend to manage containers on a different host or environment.
  2. How container hooks are executed

    main

    Tugtainer uses a hooks_executor to manage the lifecycle of shell commands during updates.

    Key Components:

    • get_hooks_map(host_id, names): Retrieves a mapping of container names to their configured ContainerHooks from the database.
    • run_hooks(client, container_name, hooks, hook_name): Executes the commands associated with a specific EHookName (e.g., PRE_UPDATE) sequentially using client.container.exec.

    Execution Logic:

    • Commands are run one after another.
    • run_hooks never raises an exception itself. Instead, it collects any exceptions raised by individual commands and returns them as a list[Exception].
    • An empty list indicates all commands succeeded or no hooks were configured. This allows callers to decide whether to abort an update (if a PRE_UPDATE hook fails) or simply report the error (for POST_UPDATE hooks).
  3. Configure Container Update Lifecycle Hooks

    main

    Container hooks allow you to run shell commands at specific points in a container's update or rollback lifecycle. The available hook types are:

    • pre_update: Before the update process begins.
    • post_update: After the update process completes.
    • pre_stop: Before the container is stopped.
    • pre_rollback: Before the old container is restored during a rollback.
    • post_rollback: After the old container is confirmed back up and running.
  4. Configure container update hooks via ALLOW_HOOKS and ALLOW_EXEC

    main

    Container update hooks (arbitrary shell commands run inside containers at specific lifecycle points) require two separate feature gates to be enabled:

    1. Backend Gate (ALLOW_HOOKS): A configuration flag on the Tugtainer backend. When false, the backend will not attempt to trigger hooks.
    2. Agent Gate (ALLOW_EXEC): A configuration flag on the host agent. When false, the agent will refuse to execute shell commands inside containers via the /api/container/exec/ endpoint.

    Both must be true for hooks to actually run on a given host.

  5. How container update hooks work

    main

    Container update hooks allow you to run shell commands inside a container at specific points in its update lifecycle.

    Lifecycle Hooks

    Hooks are categorized by the stage of the update process they target:

    • PRE_STOP: Runs before the container is stopped.
    • PRE_UPDATE: Runs before the container is updated (if the container is part of the update plan).
    • POST_UPDATE: Runs after a successful update and health check. Errors here are reported but do not change the update result.
    • PRE_ROLLBACK: Runs when a container is unhealthy after an update, while the container is still running, before the rollback (stop/remove) begins. Errors here are reported but do not block the rollback.
    • POST_ROLLBACK: Runs after a rollback is completed.

    Execution Logic and Error Handling

    • Abort Behavior: If a PRE_STOP or PRE_UPDATE hook fails, the update process for that container is aborted. The container is not stopped or updated, and the error is added to the update plan's error list.
    • Report-Only Behavior: If a POST_UPDATE or PRE_ROLLBACK hook fails, the error is logged and added to the error list, but the update/rollback process continues. This ensures that safety mechanisms (like rollbacks) are not blocked by hook failures.
  6. Configure update lifecycle hooks

    main

    Tugtainer allows running shell commands inside a container at specific points in the update lifecycle. Commands are executed as sh -c "<command>" via the agent.

    Available Hooks:

    • pre_update (Failure aborts the update)
    • pre_stop (Failure aborts the update)
    • post_update (Failure is report-only)
    • pre_rollback (Failure is report-only)
    • post_rollback (Failure is report-only)

    Security Requirements: This feature is disabled by default and requires two environment variables to be set:

    1. ALLOW_HOOKS=true on the Tugtainer backend.
    2. ALLOW_EXEC=true on the Tugtainer-Agent for the specific host.
  7. Use ContainerHooks to automate lifecycle tasks

    main

    The ContainerHooks schema allows you to define a list of shell commands to be executed inside a container at various stages of its update lifecycle. Each command is a raw shell string and is executed sequentially using sh -c "<command>".

    Available Hook Points:

    • pre_update: Before the container is updated.
    • post_update: After the container is updated.
    • pre_stop: Before the container is stopped.
    • pre_rollback: Before a rollback is performed.
    • post_rollback: After a rollback is performed.

    Data Structure: In the database and API, hooks are represented as a dictionary where keys are the hook names and values are lists of command strings.

    class ContainerHooks(BaseModel):
        pre_update: list[str] = Field(default_factory=list)
        post_update: list[str] = Field(default_factory=list)
        pre_stop: list[str] = Field(default_factory=list)
        pre_rollback: list[str] = Field(default_factory=list)
        post_rollback: list[str] = Field(default_factory=list)
  8. Set up the Tugtainer Backend development environment

    main

    The Tugtainer Backend uses the uv package manager for dependency management. Follow these steps to set up your local environment:

    1. Install uv: Follow the official installation guide.
    2. Create a virtual environment: Run uv venv.
    3. Activate the environment:
      • On Unix/macOS: source .venv/bin/activate
      • On Windows: .venv\scripts\activate
    4. Install dependencies: Run uv sync --locked to install all locked dependencies.
    5. Install pre-commit hooks: Run pre-commit install.
    uv venv
    source .venv/bin/activate
    uv sync --locked
    pre-commit install
  9. Run the Tugtainer Backend and Agent

    main

    You can run the different components of the system using the following Python module commands:

    • Run the Backend: python -m backend.dev
    • Run the Agent: python -m agent.dev
    • Run the Whole App: Use the VS Code default task Ctrl + Shift + B.
    python -m backend.dev
    python -m agent.dev
  10. Enable Container Update Hooks via Configuration

    main

    To use container update hooks (arbitrary shell commands run inside containers during the update lifecycle), you must enable two specific feature gates. Both must be set to true for hooks to execute on a host.

    1. Backend Gate: Set the ALLOW_HOOKS environment variable to true. This enables the public API endpoints for managing hooks.
    2. Agent Gate: Set the ALLOW_EXEC configuration on the agent side. This allows the agent to execute arbitrary shell commands inside containers.

    By default, both are set to False for security.

  11. Configure Tugtainer via environment variables

    main
    Environment variables are optional for Tugtainer. However, you can define specific variables to customize behavior (such as enabling the public API). A complete list of available variables and their descriptions can be found in the .env.example file in the repository root.