tugtainer Documentation
repository·main·Indexed 23 days ago
https://github.com/quenary/tugtainerA 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.
What's inside tugtainer
- 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.
How container hooks are executed
mainTugtainer uses a
hooks_executorto manage the lifecycle of shell commands during updates.Key Components:
get_hooks_map(host_id, names): Retrieves a mapping of container names to their configuredContainerHooksfrom the database.run_hooks(client, container_name, hooks, hook_name): Executes the commands associated with a specificEHookName(e.g.,PRE_UPDATE) sequentially usingclient.container.exec.
Execution Logic:
- Commands are run one after another.
run_hooksnever raises an exception itself. Instead, it collects any exceptions raised by individual commands and returns them as alist[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_UPDATEhook fails) or simply report the error (forPOST_UPDATEhooks).
Configure Container Update Lifecycle Hooks
mainContainer 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.
Configure container update hooks via ALLOW_HOOKS and ALLOW_EXEC
mainContainer update hooks (arbitrary shell commands run inside containers at specific lifecycle points) require two separate feature gates to be enabled:
- Backend Gate (
ALLOW_HOOKS): A configuration flag on the Tugtainer backend. Whenfalse, the backend will not attempt to trigger hooks. - Agent Gate (
ALLOW_EXEC): A configuration flag on the host agent. Whenfalse, the agent will refuse to execute shell commands inside containers via the/api/container/exec/endpoint.
Both must be
truefor hooks to actually run on a given host.- Backend Gate (
Configure Container Hooks UI visibility
mainThe hooks management panel in the container card is conditionally rendered based on the following criteria:
containersStore.hooksEnabled()must betrue.- The selected container must not be
protected(!containersStore.selected()?.protected).
If these conditions are met, a new accordion panel labeled
CONTAINERS.HOOKS.LABELwill appear.How container update hooks work
mainContainer 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_STOPorPRE_UPDATEhook 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_UPDATEorPRE_ROLLBACKhook 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.
Configure update lifecycle hooks
mainTugtainer 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:
ALLOW_HOOKS=trueon the Tugtainer backend.ALLOW_EXEC=trueon the Tugtainer-Agent for the specific host.
Use ContainerHooks to automate lifecycle tasks
mainThe
ContainerHooksschema 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 usingsh -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)Set up the Tugtainer Backend development environment
mainThe Tugtainer Backend uses the
uvpackage manager for dependency management. Follow these steps to set up your local environment:- Install uv: Follow the official installation guide.
- Create a virtual environment: Run
uv venv. - Activate the environment:
- On Unix/macOS:
source .venv/bin/activate - On Windows:
.venv\scripts\activate
- On Unix/macOS:
- Install dependencies: Run
uv sync --lockedto install all locked dependencies. - Install pre-commit hooks: Run
pre-commit install.
uv venv source .venv/bin/activate uv sync --locked pre-commit installRun the Tugtainer Backend and Agent
mainYou 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- Run the Backend:
Enable Container Update Hooks via Configuration
mainTo 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
truefor hooks to execute on a host.- Backend Gate: Set the
ALLOW_HOOKSenvironment variable totrue. This enables the public API endpoints for managing hooks. - Agent Gate: Set the
ALLOW_EXECconfiguration on the agent side. This allows the agent to execute arbitrary shell commands inside containers.
By default, both are set to
Falsefor security.- Backend Gate: Set the
Configure Tugtainer via environment variables
mainEnvironment 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.examplefile in the repository root.