Introducing a new way to review your PRs.

Read the announcement

basecamp / kamal

Deploy web apps anywhere.

View on GitHub

17 documents · Updated Nov 16, 2026

docs / application-deployment

Application deployment lifecycle

The application deployment system turns a selected image version into a running application on the target hosts. It prepares shared web resources, starts each application role in a controlled order, connects the role to the proxy, and provides commands for inspection, recovery, and routine operations.

Deployment flow

The deployment flow is coordinated by the top-level deploy, redeploy, rollback, and setup commands. It combines image delivery with host-side rollout work so that every role receives the same selected version.

flowchart TD
    A[setup or deploy] --> B{Push image?}
    B -->|yes| C[Build and deliver image]
    B -->|no| D[Pull image]
    C --> E[Run pre-deploy hook]
    D --> E
    E --> F[Start proxy]
    F --> G{Setup mode?}
    G -->|yes| H[Start accessories]
    G -->|no| I[Find stale containers]
    H --> I
    I --> J[Prepare app hosts]
    J --> K[Boot application roles]
    K --> L[Prune old resources]
    L --> M[Run post-deploy hook]

The setup command also bootstraps the servers and starts all accessories. redeploy skips server, proxy, accessory, and prune work. rollback selects an existing container version and uses the normal application boot path when that version exists on every required host. These differences let operators choose between a first install, a complete rollout, a lighter repeat rollout, and a known-good recovery path. See Command execution and orchestration for the wider command model and Image building strategies for image delivery.

Deployment orchestration

The deployment command first makes the image available, then takes the deploy lock before it changes remote application state. It runs the pre-deploy hook before proxy and application changes. The application boot follows stale-container cleanup, and the post-deploy hook runs after the measured deployment work completes. This order keeps image selection, remote changes, and operator hooks tied to one deployment event. lib/…/main.rb• L1-109

References: lib/…/main.rb• L1-109 lib/…/base.rb• L87-123

Application rollout

The application boot prepares each application host before it starts containers. It then processes roles in host groups that respect the configured rollout limit. The primary role acts as a health barrier: other roles wait for it to become healthy, and a failure prevents the remaining roles from starting. A successful role boot connects the new container to the proxy or waits for its health state when no proxy serves that role. The old version is stopped only after the new version succeeds. lib/…/app.rb• L1-41, lib/…/boot.rb• L14-101

References: lib/…/app.rb• L1-41 lib/…/boot.rb• L14-101 lib/…/specifics.rb• L21-31

Version selection

An application version identifies the image and the related container instance for a rollout. Normal deployment uses the configured version or the latest image tag. Rollback uses a requested version only when each role can find a matching container on the target hosts. After a successful boot, each host marks the selected image as the latest local image for later operational commands. lib/…/app.rb• L332-357, lib/…/main.rb• L86-109

References: lib/…/app.rb• L332-357 lib/…/main.rb• L86-109 lib/…/app.rb• L41-73

Application resources

The application rollout includes resources that must be ready before traffic reaches the new container. Asset files, proxy error pages, and optional TLS certificates are prepared on each relevant host before role boot.

Asset handling

Asset handling makes files from the selected image available through a host volume used by proxy-backed roles. The system extracts the asset path before boot, copies it into the new versioned volume, and can copy content between the old and new volumes during a transition. After the old container stops, it removes older extracted and volume directories. This preserves static files while traffic moves between versions. lib/…/assets.rb• L12-18, lib/…/assets.rb• L1-50, lib/…/role.rb• L133-158

References: lib/…/assets.rb• L12-18 lib/…/assets.rb• L1-50 lib/…/role.rb• L133-158

Error page handling

Error page handling uploads versioned 4xx and 5xx HTML files to the proxy error-page directory before application boot. Proxy routing points to the directory for the active version. Cleanup removes old version directories after the old application stops. If no error-page path is configured, this part of the lifecycle is skipped. lib/…/error_pages.rb• L1-33, lib/…/error_pages.rb• L1-9, lib/…/proxy.rb• L121-127

References: lib/…/error_pages.rb• L1-33 lib/…/error_pages.rb• L1-9 lib/…/proxy.rb• L121-127

Proxy and certificate handoff

A role that uses the application proxy receives a proxy target after its container starts. The proxy uses the container endpoint and the role's routing settings to send traffic to the new version. When a role has a custom TLS certificate, the boot flow writes its certificate and key before the proxy uses that role. See Proxy management for the proxy service lifecycle. lib/…/proxy.rb• L1-31, lib/…/ssl_certificates.rb• L11-28, lib/…/proxy.rb• L20-64

References: lib/…/proxy.rb• L1-31 lib/…/ssl_certificates.rb• L11-28 lib/…/proxy.rb• L20-64

Container operations

The application command object provides the common actions that the CLI runs on each role and host. It describes container creation, image and container inspection, controlled stop and removal, proxy changes, and log access without deciding which hosts should run them.

Container state

Application containers are tied to a service, destination, role, and version. This identity lets the system find the current running version, distinguish stale versions, and stop or remove one version without changing other roles. Health status comes from the container runtime when the role has no proxy-managed readiness path. lib/…/app.rb• L16-73, lib/…/role.rb• L124-130

References: lib/…/app.rb• L16-73 lib/…/containers.rb• L1-31 lib/…/role.rb• L124-130

Command execution

The app exec command runs a requested command either in the current application container or in a temporary container from the selected image. It supports remote interactive sessions over SSH, normal output, environment values, and detached work. Reuse targets the selected existing version; a new-container execution isolates the command from the running application. lib/…/app.rb• L91-161, lib/…/execution.rb• L1-38

References: lib/…/app.rb• L91-161 lib/…/execution.rb• L1-38

Logs and inspection

Operational inspection covers container lists, image lists, versions, health data, and current or followed logs. The CLI runs these views across application hosts unless the operator narrows the host or role set. Log follow runs from the primary host and can filter by time, line count, or text. lib/…/app.rb• L163-241, lib/…/logging.rb• L1-28

References: lib/…/app.rb• L163-241 lib/…/logging.rb• L1-28 lib/…/containers.rb• L4-30

Operational subcommands

The app command group exposes both lifecycle controls and read-only views. These commands share the same role and host selection rules, so an operator can inspect or change the same deployment scope used by a rollout.

Command groupPurpose
kamal app boot, start, stopCreate or restart application containers and update proxy state when needed.
kamal app execRun a command in an existing container or a new image-based container.
kamal app containers, stale_containers, images, version, detailsInspect runtime state and identify old versions.
kamal app logsRead or follow application logs with optional filters.
kamal app live, maintenanceResume traffic or stop traffic while allowing configured draining.
kamal app removeStop and remove application containers, images, and application directories.
Hidden removal commandsRemove one version, all containers, all images, or application directories as part of larger workflows.

The top-level details command combines proxy, application, and accessory views. The top-level remove command composes application removal with proxy, accessory, and registry cleanup. These cross-service commands are useful when the desired result is a clean host rather than an application-only change. lib/…/app.rb• L43-325, lib/…/main.rb• L112-117, lib/…/main.rb• L187-198

References: lib/…/app.rb• L43-325 lib/…/main.rb• L112-117 lib/…/main.rb• L187-198

Application deployment depends on several command areas that own different parts of the same rollout. Read Command execution and orchestration for dispatch and host selection, Accessory service operations for auxiliary services, Proxy management for traffic routing, Image building strategies for image production, and Host and runtime operations for server and Docker preparation.

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the image version used by image delivery, container names, proxy error-page paths, asset directories, and rollback lookup aligned.
  • Keep the proxy target contract aligned with role port, health settings, TLS paths, and the endpoint returned after container boot.
  • Keep asset extraction and cleanup names aligned with the versioned volume paths passed to the application container.
  • Keep the primary-role barrier behavior aligned with rollout limits and the order of role boot hooks.
  • Keep deploy, redeploy, setup, and rollback subcommand order aligned when adding or removing a lifecycle stage.
  • Keep application subcommand options aligned with the execution mode selected by app exec.

Important failure modes

TriggerConsequenceGuard
The selected image is not available on a host.Container boot cannot start for that role.Deliver or pull the image before app boot; use the configured version or a known available rollback version.
The primary role fails its health check.The barrier closes and queued roles do not boot.Inspect the failed version's logs and health data before retrying.
The new container has no endpoint for a proxy-backed role.Proxy deployment stops and the new container is stopped.Confirm that the container started and that its identity can be found on the host.
A stale container remains active.An old version can continue to consume resources or serve unexpected state.Run stale-container detection with stopping enabled before boot and prune after a successful deployment.
Asset or error-page preparation is skipped or incomplete.The new version can serve missing static files or lose its custom error response.Prepare these resources on every application host before role boot.
A custom TLS certificate is missing one required part.The proxy cannot use the role's custom certificate.Provide both certificate and private key content before boot.

Key files

FileWhy to read it
lib/…/main.rbShows the top-level setup, deploy, redeploy, and rollback flow.
lib/…/app.rbDefines application lifecycle and operational subcommands.
lib/…/boot.rbShows per-role rollout order, health barriers, proxy handoff, and old-version cleanup.
lib/…/assets.rbShows when image assets move into host volumes.
lib/…/error_pages.rbShows how versioned proxy error pages reach hosts.
lib/…/app.rbProvides the application container command surface and version lookup model.
lib/…/execution.rbDefines existing-container and new-container command execution modes.
lib/…/proxy.rbConnects an application role to proxy traffic control.
lib/…/role.rbDefines role identity, host placement, proxy use, and asset behavior.