Skip to content

Swarm

Source: src/Docker/Swarm.ts

Swarm mode on a Docker engine — an idempotent docker swarm init.

Turns the engine behind the given context (or the local engine) into a single-node swarm: the node becomes a manager that can run Docker.Service workloads. Regular (non-swarm) docker usage of the engine is unaffected.

Pass the swarm as the context of a Docker.Service or overlay Docker.Network: the workload then deploys after the swarm exists and inherits its Docker context.

An engine that is already in swarm mode is treated as foreign — adopt it with adopt(true) (or --adopt) to manage it. Destroying the resource dissolves the node’s swarm membership (docker swarm leave --force), which stops every service running on it — services managed by the same stack are destroyed first via their dependency edges.

Growing the cluster beyond one node is host-level setup: run docker swarm join --token <token> <manager-ip>:2377 on each additional machine (docker swarm join-token worker on the manager prints the command). Alchemy manages the swarm’s workloads through the manager.

Local single-node swarm

const swarm = yield* Docker.Swarm("swarm");

Remote engine over SSH

const vps = yield* Docker.Context("vps", {
docker: "host=ssh://deploy@example.com",
});
const swarm = yield* Docker.Swarm("swarm", {
context: vps,
advertiseAddr: "10.0.0.1",
});

Reference without owning

// Services don't require a Swarm resource — point them at an engine that
// is already a manager and the swarm's lifecycle stays external: destroy
// removes the services, never the swarm.
const web = yield* Docker.Service("web", {
context: vps,
image: "nginx:alpine",
});

Adopt an already-initialized engine

// Adoption makes the swarm part of the stack — destroy then dissolves
// the node's membership.
const swarm = yield* Docker.Swarm("swarm").pipe(adopt(true));
const swarm = yield* Docker.Swarm("swarm");
const web = yield* Docker.Service("web", {
context: swarm,
image: "nginx:alpine",
replicas: 2,
ports: [{ external: 8080, internal: 80 }],
});