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 / container-lifecycle

Container lifecycle behavior

Container lifecycle behavior is the set of actions that turns a built image into a running application or accessory service. Deployment commands create versioned application containers, check that they are ready, connect them to the proxy when needed, and remove or retain older containers so rollouts and recovery stay controlled.

Deployment lifecycle

The deployment flow moves from image availability to safe replacement of the running application. The main deploy path also starts supporting services and ends by removing resources that are no longer needed.

flowchart LR
  A[Build or pull image] --> B[Ensure proxy]
  B --> C[Boot accessories when requested]
  C --> D[Stop stale containers]
  D --> E[Create and check new app containers]
  E --> F[Stop old app containers]
  F --> G[Prune old containers and images]

The deploy command follows this order. redeploy starts at stale-container detection because it does not prepare the proxy or accessories. rollback selects an existing version and sends it through the normal application boot flow instead of building a new image. (See application deployment for the wider release process.)

References: lib/…/main.rb• L18-109

Application container creation

An application rollout creates a detached container for each selected role and host. Each container has a version in its identity, so the system can distinguish the new instance from the instance that currently receives traffic.

Before creation, the rollout prepares the role environment and uploads its secret file. If a container with the target version already exists on a host, the system renames it before it creates the replacement. This prevents a name collision from stopping a retry or recovery action.

The container joins the shared runtime network and receives the role command, environment, volumes, labels, and restart policy. These inputs define the application instance that the later inspection and proxy steps find.

References: lib/…/boot.rb• L14-53 lib/…/app.rb• L16-35 lib/…/role.rb• L42-60 lib/…/role.rb• L124-130

Application readiness

Readiness is the gate between container creation and traffic registration. A container without a running proxy is checked until its Docker state is running or healthy, with a configurable delay and timeout.

For a role served by the proxy, the rollout first obtains the new container endpoint and asks the proxy to deploy that endpoint. The proxy then controls traffic readiness and can drain requests when the old container stops.

References: lib/…/boot.rb• L46-60 lib/…/poller.rb• L4-36 lib/…/proxy.rb• L1-21 lib/…/role.rb• L74-86

Application replacement

The old application container stays in place until the new container starts successfully. After readiness passes, the system stops the old version and performs related asset and error-page cleanup when those features are enabled.

The current version is selected from active containers that match the service, destination, and role. Stopped or extra versions are treated as stale rather than as the live application. A separate stale-container command can report these versions or stop them before a rollout.

References: lib/…/boot.rb• L20-31 lib/…/boot.rb• L66-74 lib/…/app.rb• L45-74 lib/…/app.rb• L169-190

Rollout coordination

Rollouts can affect many roles and hosts at once, so the lifecycle uses a health barrier to control when dependent roles start. The primary role opens the barrier only after its first container is ready; other roles wait for that result.

sequenceDiagram
  participant P as Primary role
  participant B as Health barrier
  participant R as Other roles
  P->>P: Create and check container
  P->>B: Open after readiness
  B-->>R: Allow role boot
  R->>R: Create and check containers
  P-->>B: Close on failure
  B-->>R: Halt remaining role boots

Hosts can also be handled in groups. A boot limit controls how many hosts enter the application boot stage together, and a wait value can pause between groups. These controls reduce the number of containers changed at one time without changing the per-container readiness rules.

References: lib/…/app.rb• L19-39 lib/…/app.rb• L367-369 lib/…/boot.rb• L17-32 lib/…/barrier.rb• L3-30

Failed application boot

If the new container does not start or become ready, the boot flow stops that new container and does not stop the old one. If the failed container is the primary role, the barrier closes and later roles do not boot. The system then attempts to show the failed container logs and health state.

This preserves the last known working application during a failed replacement. A rollback uses a version that is still available as a container and repeats the same guarded boot sequence.

References: lib/…/boot.rb• L20-25 lib/…/boot.rb• L61-100 lib/…/main.rb• L86-109

Accessory lifecycle

Accessories are long-lived services such as databases or queues. They use a stable service name rather than an application version, and their hosts come from the accessory configuration. An accessory boot skips a host that already has its container.

Boot prepares registry access and the shared network, creates required directories, uploads files and secrets, and then starts the container. If the accessory uses the proxy, the system registers its running endpoint after creation. (See accessory operations for accessory-specific commands.)

References: lib/…/accessory.rb• L4-41 lib/…/accessory.rb• L43-75 lib/…/accessory.rb• L362-371 lib/…/accessory.rb• L29-39

Accessory restart and replacement

An accessory restart stops and starts the existing container. A reboot is a replacement operation: it pulls the image, records the image used by the old container, stops and removes that container, boots a new one, and removes the old image when Docker no longer needs it.

This order allows an image change or a changed port binding to take effect. The remove step applies only to the named accessory service, so it does not remove application containers.

References: lib/…/accessory.rb• L77-132 lib/…/accessory.rb• L309-331 lib/…/accessory.rb• L15-38 lib/…/accessory.rb• L112-120

Accessory removal

Accessory removal stops the service before it removes the container, image, and service data directory. The command can apply this sequence to one accessory or to all configured accessories.

When an accessory is connected to the proxy, stopping also removes its proxy entry. Application stop follows the same traffic rule: remove the live proxy target before stopping the container.

References: lib/…/accessory.rb• L241-265 lib/…/accessory.rb• L355-360 lib/…/accessory.rb• L110-125 lib/…/app.rb• L62-78

Container inspection and cleanup

Inspection identifies containers by service labels, role, destination, and version. The commands use this identity to find the live version, check health, list stale instances, and target a single version for removal. (See container runtime for the broader runtime model and image identity and registry for image selection.)

The lifecycle keeps stopped application containers for a limited time so operators can inspect or roll back to them. Pruning removes stopped containers beyond the configured retention count and removes unused service images while preserving images still referenced by containers.

References: lib/…/base.rb• L5-19 lib/…/app.rb• L60-74 lib/…/containers.rb• L4-29 lib/…/prune.rb• L5-32 lib/…/prune.rb• L1-31

Change checklist

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

  • Keep application container identity consistent across version lookup, stale detection, health checks, and targeted removal.
  • Preserve the readiness contract between application boot and proxy registration. A proxy-backed role must expose a usable endpoint before the old container is stopped.
  • Keep barrier failure behavior aligned with role boot order. A primary-role failure must not allow dependent roles to start.
  • Keep accessory service filters separate from application labels so accessory removal cannot affect application containers.
  • Keep prune rules compatible with rollback retention. Image cleanup must not remove an image still referenced by a retained container.

Important failure modes

TriggerConsequenceGuard
The new application container never reaches running or healthy before the timeoutThe new version is stopped and the old version remains; later roles may be blockedHealth polling and the primary-role barrier
A target application version already has a container name on the hostThe new container cannot be created under its expected identityRename the clashing container before boot
A proxy-backed container has no usable endpointTraffic cannot be registered for the new instanceRead the container endpoint before proxy deployment
A rollout stops stale containers without preserving the live versionThe active application can be stopped before replacementExclude the current running version from stale results
An accessory is rebooted without retaining its old image identityThe old image may be removed too early or not removed after replacementRecord the image ID before stop and remove it only after the new boot

Key files

FileWhy to read it
lib/…/main.rbShows how deploy, redeploy, rollback, and cleanup commands coordinate the lifecycle.
lib/…/boot.rbShows the guarded application replacement sequence and failure handling.
lib/…/app.rbShows application start, stop, stale detection, inspection, and removal commands.
lib/…/app.rbShows the Docker actions and identity rules for versioned application containers.
lib/…/barrier.rbShows how role boot is held behind primary application readiness.
lib/…/accessory.rbShows accessory boot, restart, reboot, stop, and removal sequences.
lib/…/accessory.rbShows the stable accessory container actions and service-scoped inspection.
lib/…/prune.rbShows which stopped containers and images the cleanup stage can remove.