Skip to content

recipes/container ​

Experimental

This recipe is experimental: its options, defaults and behavior may change in a minor release. Report issues and feedback on GitHub.

View source on GitHub

typescript
import '@catapultjs/deploy/recipes/container'

This recipe runs one or more containers from your application image with Docker or Podman, without Compose. The image is built on your machine, streamed to each server over SSH (no registry needed), and each release records how its containers were started, so a rollback restarts the exact previous containers.

Use it for an application made of a few processes sharing one image: a web server, workers, a scheduler. For databases or several images, prefer compose. The recipe owns its whole pipeline: do not combine it with git, rsync, or an application recipe.

Quick start ​

typescript
// deploy.ts
import { defineConfig, set } from '@catapultjs/deploy'
import '@catapultjs/deploy/recipes/container'

set('container_engine', 'podman')
set('shared_files', ['.env'])
set('shared_dirs', ['storage'])
set('container_services', {
  web: {
    command: ['node', 'bin/server.js'],
    init: true,
    env: { PORT: '3333' },
    env_file: ['.env'],
    ports: ['127.0.0.1:3333:3333'],
    volumes: ['./storage:/app/storage'],
  },
  worker: {
    command: ['node', 'ace', 'queue:work'],
    init: true,
    healthcheck: false,
    env_file: ['.env'],
  },
})
set('container_migrate', { service: 'web', command: ['node', 'ace', 'migration:run', '--force'] })

export default defineConfig({
  hosts: [
    {
      name: 'production',
      ssh: 'deploy@example.com',
      deployPath: '/srv/myapp',
      healthcheck: { url: 'http://127.0.0.1:3333/health', retries: 10 },
    },
  ],
})

Run npx cata deploy:setup once, put the production .env in shared/.env on the server, then run npx cata deploy.

Add a .containerignore (or .dockerignore) that excludes at least .env and node_modules: the build context is your local directory, uncommitted changes included.

How it works ​

  1. The image is built locally once per deployment as localhost/<container_image>:<release>, and streamed to each server over SSH. When it did not change since the current release, the server only adds the new tag.
  2. shared_dirs / shared_files are linked into the release. The env values of each service are written to container/<service>.env in the release, readable by the deploy user only.
  3. The optional migration runs in a one-off container of the new image, on the project network, with the service's env files, user and volumes. A failure stops the deployment before publication.
  4. After publication, each service container (<container_project>-<service>) is stopped, removed and started again from the new image, except services deployed without downtime. Containers of services removed from container_services are stopped and removed.
  5. Catapult waits until every container runs, and is healthy when the image declares a healthcheck, then checks again 3 seconds later. A container that exits, becomes unhealthy, restarts in between, or restarts repeatedly while waiting (a crash loop hidden by the restart policy) fails the deployment and triggers the automatic rollback.

All services share a network named after container_project, so they reach each other by container name. Ports are released between stop and start: expect a short interruption on each deployment, unless the service uses blue_green.

The release manifest records how each service is started: command, ports, volumes, labels and env file paths, but never the values of environment variables.

Pipeline

deploy:lock → oci:check → container:check → deploy:release → deploy:shared → container:prepare → oci:build → oci:transfer → container:migrate → deploy:publish → container:up → deploy:log_revision → deploy:healthcheck → container:retire → deploy:cleanup → oci:cleanup → deploy:unlock

Tasks

TaskInsertedDescription
oci:checkyesDetects the engine, checks access, architecture, disk and linger
container:checkyesValidates container_services, checks podman-restart.service
container:prepareyesWrites env files and records the services of the release
oci:buildyesBuilds the image locally, once for all hosts
oci:transferyesStreams the image to the server and writes release.json
container:migrateyesRuns container_migrate when set
container:upyesReplaces the containers and waits until they are ready
container:retireyesStops the blue_green containers no longer receiving traffic
oci:cleanupyesRemoves images of deleted releases, keeps a few local builds
container:startmanualStarts the existing containers
container:stopmanualStops the containers
container:restartmanualRestarts the containers and waits until they are ready
container:removemanualStops and removes the containers; volumes and network are kept
container:logsmanualShows recent logs of each container
container:statusmanualLists the containers of the project

Manual tasks run against the current release: npx cata task container:logs. For an interactive shell, use npx cata ssh, then podman exec -it <project>-web sh.

cata status reports the engine version and the state of each container.

Configuration ​

Services

container_services maps service names to their options. Every option is optional.

OptionTypeDefaultDescription
commandstring[]image CMDCommand and arguments, passed without shell
initbooleanfalseRuns an init process as PID 1 (signal forwarding, zombie reaping)
healthcheckboolean | objectimagefalse disables the image HEALTHCHECK, an object replaces it
entrypointstringimageExecutable replacing the image entrypoint
userstringimageuser or uid:gid
envRecord<string, string>{}Non-secret variables, written to an env file of the release
env_filestring[][]Env files, relative to the release (e.g. a shared .env)
portsstring[][]Published ports, e.g. 127.0.0.1:3333:3333
volumesstring[][]./dir:/path, /abs:/path or named:/path, with :ro if needed
restartstring'always'no, always, unless-stopped or on-failure[:N]
labelsRecord<string, string>{}Extra labels (the com.catapultjs. prefix is reserved)
memorystringnoneMemory limit, e.g. 512m or 2g
cpusnumber | stringnoneCPU limit, e.g. 1.5
networksstring[][]Existing networks joined in addition to the project network
optionsstring[][]Raw engine options, e.g. --cap-drop=ALL
blue_greenobjectnoneDeploys without downtime behind Caddy, see below

Keep secrets in env_file (typically a shared .env), never in env: values are passed as env files, never on the command line. Relative volume sources resolve inside the release directory: point them to shared_dirs entries to keep data across releases, or use named volumes.

Enable init for processes that do not handle SIGTERM, such as Node.js running as PID 1: without it, each stop waits container_stop_timeout seconds before the container is killed.

Set healthcheck: false on services the image HEALTHCHECK does not apply to, such as a worker sharing the image of an HTTP server: otherwise the worker never becomes healthy and the deployment fails.

An object defines the healthcheck in the configuration instead of the image. It also works where Podman drops the image HEALTHCHECK (see Limitations). cmd runs with /bin/sh -c inside the container; durations use the 30s or 1m30s format:

typescript
web: {
  command: ['node', 'bin/server.js'],
  healthcheck: {
    cmd: 'wget -qO- http://127.0.0.1:3333/health || exit 1',
    interval: '10s',
    timeout: '3s',
    retries: 3,
    start_period: '5s',
  },
  memory: '512m',
  cpus: 1.5,
  networks: ['proxy'],
}

networks lists networks created outside Catapult, such as the network of a reverse proxy: Catapult checks that they exist and never creates them. Joining several networks requires Docker 25 or later. options passes extra flags to run for what the recipe does not cover; the options it manages (--name, --network, --restart, --rm, --detach) are refused.

Extra networks, resource limits and options also apply to the one-off migration container, so it reaches the same services.

Labels apply to service containers only, not to the one-off migration container, so reverse-proxy routing labels never target it.

Other keys

KeyTypeDefaultDescription
container_migrate{ service: string, command: string[] }noneOne-off command run before publication
container_wait_timeoutnumber60Seconds to wait for containers to be ready
container_stop_timeoutnumber10Seconds given to a container to stop gracefully
container_logs_linesnumber100Lines shown per container by container:logs
container_caddy_upstreamsstring'/etc/caddy/catapult'Directory of the upstream files of blue_green services

The image options (container_engine, container_build_engine, container_project, container_image, container_build, container_source, container_compression, container_keep_local) are the same as for compose, as are the build on the servers and the transfer through a registry.

All keys are available in JSON configurations with "recipes": ["container"].

Zero downtime with Caddy ​

A web service with blue_green alternates between two host ports instead of being replaced in place. Caddy proxies the traffic, and Catapult switches it to the new release once checked:

typescript
set('container_services', {
  web: {
    command: ['node', 'bin/server.js'],
    init: true,
    env_file: ['.env'],
    blue_green: { ports: [3001, 3002], container_port: 3333, health_path: '/health' },
  },
  worker: { command: ['node', 'ace', 'queue:work'], init: true, env_file: ['.env'] },
})
# /etc/caddy/Caddyfile, written once
example.com {
	reverse_proxy {
		import /etc/caddy/catapult/myapp-web.caddy
	}
}
  1. container:up starts the new release in the idle slot (<project>-<service>-a or -b, published on 127.0.0.1 on its port), waits until it is ready and requests health_path on it directly.
  2. Catapult writes the upstream file (to 127.0.0.1:<port>), validates the Caddy configuration and reloads Caddy. Requests in flight finish on the previous release; new requests reach the new one.
  3. deploy:healthcheck runs, through Caddy when healthcheck.url points to it.
  4. container:retire waits drain seconds, then stops the previous container.
OptionTypeDefaultDescription
ports[number, number]requiredHost ports used alternately, published on 127.0.0.1
container_portnumberrequiredPort the application listens on in the container
health_pathstringnonePath requested on the new container before switching
drainnumber10Seconds the previous container keeps to finish requests

When a deployment fails before container:retire, the previous container is still running: the rollback points Caddy back to it and removes the new one, without restarting anything. A later rollback starts the restored release in the idle slot and switches, as a deployment does. An invalid Caddy configuration or a failed reload restores the previous upstream files.

Other services (workers) are still replaced in place, so two versions never consume the same queue. blue_green services cannot set ports, and their ports must differ across services. Enabling blue_green on a running service removes its single container first: that deployment has a short interruption.

Catapult writes the upstream files with sudo and reloads Caddy with sudo caddy reload --config /etc/caddy/Caddyfile, using the caddy keys caddy_config_path and caddy_use_sudo (set it to false when deploying as root). The deploy user needs passwordless sudo for tee, cp, mv, rm, mkdir and caddy. When caddy reload cannot reach the admin endpoint, for example under SELinux, declare an admin socket in the global options: admin unix//run/caddy/admin.sock.

Server requirements ​

Catapult checks the server but never installs or changes anything on it.

  • Docker: a deploy user allowed to use Docker. Restart policies are applied by the Docker daemon after a reboot.
  • Podman: restart policies are applied after a reboot by podman-restart.service. Enable it with systemctl --user enable podman-restart.service (without --user for rootful Podman).
  • Rootless Podman: enable lingering so containers survive the end of the SSH session: sudo loginctl enable-linger <user>.

Rollback ​

npx cata rollback switches current back and recreates the containers recorded by the restored release, with its image and env files. It is refused before any change when the target has no recorded configuration, misses an env file, or when its image is no longer on the server.

A rollback does not restore data or reverse migrations. Write migrations that the previous release can still run against, or restore a backup.

Limitations ​

  • Containers are replaced in place: a short interruption happens on fixed ports. Use blue_green for web services behind Caddy.
  • One image per project. Use compose for stacks combining several images.
  • Podman ignores the image HEALTHCHECK for OCI images, the format produced by Docker with the containerd image store: Catapult warns when it is lost. Declare healthcheck in the service, or build with Podman locally.