Host and runtime operations
Host and runtime operations prepare deployment hosts, connect them to an image registry, protect changes from overlap, and remove old runtime data. These commands support the larger deployment flow described in Command execution and work with Application deployment, Container runtime, and Remote execution.
Operation groups
The CLI groups host work by its effect on the host or on deployment safety. Each group acts locally, on remote hosts, or on both sides of the image path.
| Group | Purpose | Main commands |
|---|---|---|
| Server | Run a command or prepare Docker on deployment hosts | server exec, server bootstrap |
| Registry | Start a local registry or authenticate to a remote registry | registry setup, registry login, registry logout, registry remove |
| Runtime cleanup | Remove old application images and stopped containers | prune images, prune containers, prune all |
| Deployment lock | Prevent overlapping changes | lock status, lock acquire, lock release |
| Hooks | Run project-owned scripts at supported lifecycle points | Hook files in .kamal/hooks |
| Audit | Record and display host-side actions | audit |
Host preparation
Host preparation makes a deployment host able to run Kamal-managed containers. server bootstrap checks for Docker, installs it when the host has the required privilege and a download tool, adds the user to the Docker group when needed, and then runs the docker-setup hook. The top-level setup command uses this step before it deploys the application.
The command fails with a manual-install message when the host lacks the access or tools needed for automatic setup. This keeps an incomplete host from appearing ready for the later proxy, accessory, or application steps.
flowchart LR
A[server bootstrap] --> B{Docker available?}
B -- Yes --> D[docker-setup hook]
B -- No --> C{Install access available?}
C -- Yes --> E[Install Docker and refresh access]
C -- No --> F[Report manual setup needed]
E --> D
D --> G[Host ready for deployment]
References: lib/…/server.rb• L37-69 lib/…/docker.rb• L3-40 lib/…/main.rb• L1-15
Server commands
server exec is the general host command path. It runs a command on all selected hosts by default and can target selected hosts or roles through the shared CLI options. Interactive mode runs through the primary host and is intended for a console or shell; raw mode preserves command output for use in a pipe.
The command records each remote execution in the host audit log. It therefore provides both an operator tool and a trace of direct host changes.
References: lib/…/server.rb• L1-35 lib/…/base.rb• L20-33 lib/…/auditor.rb• L10-18
Image access
Registry operations make the image path available to both the local build side and the remote runtime side. A local registry is started as a local Docker container. A remote registry instead receives Docker login or logout commands on the local machine and on the selected hosts.
Registry sessions
registry setup starts or logs into the configured registry. registry login is the remote-registry form of setup, while registry logout is its removal counterpart. The commands reject login and logout when the configuration uses a local registry; local registries use setup and remove instead.
Local and remote work can be skipped independently. This supports recovery when only one side needs a new session, while still making the default path cover every side that will build or pull images.
References: lib/…/registry.rb• L1-49 lib/…/registry.rb• L1-37
Docker runtime
The Docker command layer gives host operations a common way to check Docker, install it, manage group access, and create the shared kamal network. Higher-level commands use this layer instead of defining separate host setup rules for each service.
The local side also checks for Docker and Docker Buildx when a registry or build action needs local Docker support. This separates local prerequisites from the remote host checks performed by server bootstrap.
References: lib/…/docker.rb• L1-49 lib/…/base.rb• L37-41 lib/…/base.rb• L315-327
Runtime cleanup
Pruning keeps host storage bounded without removing the images and containers that the configured service still needs. The cleanup is scoped by service and, where relevant, destination so one deployment does not remove another deployment's runtime data.
Image and container pruning
prune images removes unused dangling images and old tagged images. It preserves images used by the service's containers and keeps destination tags that may use another repository. prune containers removes stopped application containers while retaining a configured number; the default is five and the value must be at least one.
prune all performs both actions. Deploy flows call it after the application starts, so cleanup follows a successful runtime change rather than replacing the active image first.
References: lib/…/prune.rb• L1-33 lib/…/prune.rb• L4-40 lib/…/main.rb• L18-53
Deployment safety
The lock, hook, and audit systems add control around commands that can change a deployment. They share the configured service, destination, host set, and version so an operator can relate a command to the deployment state it affects.
Deploy lock
The deploy lock is a host-side marker held on the primary host. It identifies the person, version, and message for the active operation. Mutating commands acquire it for the operation and release it after success or failure; a manually acquired lock can stop an unsafe concurrent change.
lock status reports the current owner. lock acquire requires a message. lock release clears the marker. Automatic commands can fail at once when the lock is held, or wait for an automatic lock when the caller enables lock waiting. They do not wait on a manual lock.
sequenceDiagram
participant CLI
participant Primary as Primary host
CLI->>Primary: Check or create deploy lock
alt Lock is free
Primary-->>CLI: Lock acquired
CLI->>Primary: Change runtime
CLI->>Primary: Remove lock
else Lock is held
Primary-->>CLI: Owner and message
CLI-->>CLI: Fail or wait by policy
end
References: lib/…/lock.rb• L1-33 lib/…/base.rb• L87-123 lib/…/base.rb• L135-222 lib/…/lock.rb• L5-69
Lifecycle hooks
Hooks are local project scripts that extend supported CLI lifecycle points. The CLI runs a hook only when its file exists and hooks are not disabled with --skip-hooks. Hook environment values describe the command, selected hosts or roles, lock state, and other details supplied by the lifecycle step.
Hooks can receive secrets when the lifecycle requires them. Their output follows the configured hook output level unless a command-level quiet or verbose option overrides it. A failed hook stops the operation with a hook-specific error.
The connection path runs pre-connect before the first remote connection. Deployment and host preparation add hooks such as pre-deploy, post-deploy, and docker-setup around their own work.
References: lib/…/hook.rb• L1-20 lib/…/base.rb• L224-267 lib/…/configuration.rb• L268-302 lib/…/main.rb• L34-52 lib/…/server.rb• L67-68
Audit records
Audit records provide a short host-side history of actions that affect the deployment. A record includes deployment tags and an action message, and the audit command shows the last 50 lines from each selected host.
The record step creates the run directory when needed and appends to a service- and destination-specific log. Direct server commands, pruning, image pulls, and runtime changes use this path so operators can inspect what the CLI asked a host to do.
References: lib/…/auditor.rb• L1-39 lib/…/main.rb• L119-125 lib/…/server.rb• L27-32 lib/…/prune.rb• L10-31 lib/…/commander.rb• L92-110
Change checklist
When these files or sections are changed, remember to consider these:
- If host bootstrap changes, verify that
setupstill prepares Docker before deployment and that thedocker-setuphook still runs after preparation. - If registry behavior changes, verify the local-versus-remote command contract and both local and remote skip flags.
- If prune filters or retention rules change, verify that active images and the configured destination remain protected.
- If lock paths or lock messages change, verify automatic lock waiting, manual-lock detection, and status output together.
- If hook context or output rules change, verify
--skip-hooks, secret passing, and the configuration-level output setting. - If audit tags or log paths change, verify both record writers and the
auditreader use the same service and destination identity.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A host lacks Docker and cannot use root access with curl or wget | Bootstrap stops and deployment does not proceed | Install Docker manually, then run bootstrap again |
| A manual deploy lock exists | A mutating command fails instead of changing a host | Inspect lock status and release the lock only when the work is complete |
| A hook exits with an error | The owning lifecycle operation stops with a hook error | Fix the hook or use --skip-hooks only when bypassing it is safe |
| Prune retention is below one | Container cleanup is rejected | Use a retention value of at least one |
Local registry is used with registry login or registry logout | The command is rejected because local registries need container setup or removal | Use registry setup or registry remove |
| Registry login runs on only one side | Builds or pulls can fail on the side without credentials | Run setup for both local and remote sides unless a skip flag is intentional |
Key files
| File | Why to read it |
|---|---|
| lib/…/server.rb | Defines host command execution and Docker bootstrap behavior. |
| lib/…/docker.rb | Defines the shared Docker checks and host setup actions. |
| lib/…/registry.rb | Defines the local and remote registry command contract. |
| lib/…/registry.rb | Shows the runtime effect of registry setup, login, logout, and removal. |
| lib/…/prune.rb | Defines cleanup entry points and retention policy. |
| lib/…/prune.rb | Defines the safety filters for images and stopped containers. |
| lib/…/base.rb | Coordinates locks, hooks, host selection, and shared command behavior. |
| lib/…/lock.rb | Defines the operator-facing lock commands. |
| lib/…/lock.rb | Defines the host-side lock record and status marker. |
| lib/…/hook.rb | Defines hook lookup and hook command construction. |
| lib/…/auditor.rb | Defines audit record format and log access. |
| lib/…/main.rb | Shows how setup, deploy, prune, and audit fit into the top-level flow. |