Skip to content

Swarm on one node: services, stacks, secrets and configs, so Dokploy runs - #731

Open
masihmoloodian wants to merge 1 commit into
libracoder:mainfrom
masihmoloodian:feat/swarm
Open

masihmoloodian wants to merge 1 commit into
libracoder:mainfrom
masihmoloodian:feat/swarm

Conversation

@masihmoloodian

@masihmoloodian masihmoloodian commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

What this changes and why

Every Swarm request answered 404, so docker service, docker stack deploy and platforms that deploy through Swarm failed on Kiri. Dokploy, from #603, is one:

debug (HTTP code 404) unexpected - Kiri does not support POST /services/create yet

Kiri is now a swarm of one node, and that node is the leader. Everything lives in a new module, crates/kiri/src/daemon/swarm.rs. The rest of the daemon changes only in small hooks: a field on Engine, the route in api.rs, the Swarm block of /info, and overlay networks reported as overlay/swarm.

Area What works
Swarm and node swarm init, leave, update; node ls, inspect, update; info .Swarm. Before init, every Swarm endpoint answers Docker's "This node is not a swarm manager" 503.
Services service create, ls (status=true), ps, inspect, scale, update (version is checked), rollback (rollback=previous), logs, rm. Filters are the ones the CLI, dockerode and Traefik send, including _up-to-date.
Tasks Ordinary containers, named <service>.<slot>.<task id> and carrying Docker's com.docker.swarm.* labels. They are made through the same create as docker run, so mounts, env, healthchecks and limits are the existing code's.
Keeping services as specced One thread handles replicas or global mode, restart policy (condition, delay, max attempts), placement constraints (node.role, node.id, node.hostname, node.platform.*, node.labels.*), rolling updates (parallelism, delay, stop-first/start-first, monitor, failure action) and rollbacks, with UpdateStatus as the CLI's progress reads it. A healthcheck counts: a task is running only once healthy.
Networks --driver overlay (refused with 403 outside a swarm) is a bridge network that reports overlay and swarm. A service's name and tasks.<name> resolve on it to its tasks.
Ports --publish in host mode, and in ingress mode, where one running task takes each port. A port with no number given gets one from 30000–32767.
Secrets and configs Create, list, inspect, update (labels only) and remove; a secret still in use can't be removed. They are mounted read-only at /run/secrets/<name> or the config's target, with File.Mode.
Stacks docker stack deploy/rm, stack ps/services.

The swarm is kept in swarm.json next to networks.json, written atomically and readable only by its owner. A daemon restart keeps the swarm and its services.

Not here yet, and listed in docs/features.md:

  • joining other nodes, and autolock
  • job services, which are refused with a clear message
  • a virtual IP per service: a name resolves to the tasks' own addresses, as with --endpoint-mode dnsrr
  • refusing docker run --network on an overlay network that isn't attachable

Secrets are files readable only by their owner, not encrypted as in Docker's Raft log.

This is a large change. CONTRIBUTING asks for agreement on an issue before one this size, and #603 hasn't had a maintainer's answer yet. Most of it is one new file, though, and I'm happy to split it into smaller PRs (swarm and nodes, then services, then networks and ports, then secrets and stacks) or to change the approach.

Refs #603

How you tested it

On this branch, with docker CLI 29.8.2 against the Kiri it builds:

$ docker service ls                          # before init
Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.
$ docker swarm init --advertise-addr 127.0.0.1
Swarm initialized: current node (pwk2759dv49wa07ytc62whcq9) is now a manager.
$ docker service create --name web --replicas 2 nginx:alpine
verify: Service web converged
$ docker service ps web
ID             NAME      IMAGE          NODE      DESIRED STATE   CURRENT STATE
hbbxlp1kcw33   web.1     nginx:alpine   …         Running         Running 21 seconds ago
y5f4dbqqdbt2   web.2     nginx:alpine   …         Running         Running 21 seconds ago
$ docker service update --image nginx:1.27-alpine web     # one task at a time, stop-first
verify: Service web converged
$ docker service rollback web
verify: Service web converged
$ docker run --rm --network appnet alpine wget -qO- http://api | grep -o '<title>.*</title>'
<title>Welcome to nginx!</title>
$ docker stack deploy -c compose.yml demo    # a network, a secret, a config, two services, a port
$ docker stack services demo
NAME         MODE         REPLICAS   IMAGE           PORTS
demo_probe   replicated   1/1        alpine:latest
demo_web     replicated   2/2        nginx:alpine    *:18090->80/tcp

Dokploy end to end. I ran its install.sh steps against Kiri: swarm init, the overlay dokploy-network, two secrets, the dokploy-postgres and dokploy services, and the Traefik container on 80/443. Then, through Dokploy's API, I deployed:

  • an app from an image (nginx:alpine)
  • an app built from a Git repository's Dockerfile (docker/welcome-to-docker)

Both run as Swarm services and answer through Dokploy's Traefik at hello.localhost and built.localhost. Redeploy (Dokploy sends ForceUpdate + 1), stop and start (scale 0/1) work. So do the commands Dokploy's UI runs inside its container: docker service ps/ls --format, docker node inspect, docker info --format '{{.Swarm.LocalNodeState}}' and docker service logs --timestamps. A daemon restart keeps it all running.

  • cargo test -p kiri: 673 passed. 14 are new in swarm.rs. Most test the planner as a pure function, without a VM: restart conditions, scale down, an update one task at a time, start-first, a failed update rolling back, an update that isn't done while a slot waits, constraints, ports, version checks, and state that survives a restart.
  • tests/e2e.sh: a new section, "swarm", with 26 checks. They run only when no swarm of the user's own is active, and leave the engine as they found it. The suite, on this branch rebased onto main (f9cd271): 799 passed, 4 failed, 1 skipped. Three of them (default-bridge-no-names, dns-failed-start-gone, disconnect-dns-gone) expect a name that doesn't exist not to resolve. This Mac's DNS answers every name with a 198.18.0.0/15 address, so they fail with or without this change. The fourth, finder-big (50 MB read through the Finder share, which this change does not touch), passed when the Finder checks were run again on their own (16 passed).
  • cargo clippy -p kiri: no new warnings (49 on main, 49 here).

Tested on: macOS 27.2.1, Apple M4 Pro, docker CLI 29.8.2. The daemon code is the same on every host, but I haven't run it on Windows or Linux.

Found while testing Dokploy, not in this PR: docker build from a docker CLI inside a container (Dokploy's CLI 28.5 with buildx 0.39) doesn't put the image in Kiri's store. buildx sees no built-in BuildKit and makes its own docker-container "default" builder without default-load, so the result stays in the build cache. docker build uses that default builder whatever buildx use says. DOCKER_BUILDKIT=0 (Kiri's classic /build) works, so the Dokploy test set it on the dokploy service. This affects any containerized docker CLI, CI runners too: #734.

How you found it

  • I use Kiri and ran into this myself
  • I found it by reading the code or comparing with Docker
  • Someone else reported it (link the issue above)

AI tools used to write this change:

Checklist

  • I have at most 3 pull requests open on Kiri, this one included
  • cargo fmt, cargo clippy and the tests pass (no rustfmt config in the repository; the new lines follow the files' style)
  • It works on macOS, Windows and Linux, or the change only touches one of them
    (daemon code shared by every host; tested on macOS only)

…runs

Every Swarm request answered 404 ("Kiri does not support POST
/services/create yet"), so `docker service`, `docker stack deploy` and
platforms that deploy through Swarm, such as Dokploy, did not work.

Kiri is now a swarm of one node, its leader:

- `swarm init`/`leave`/`update`, `node ls`/`inspect`/`update`, and the
  Swarm block of `/info`; before `init`, Docker's "not a swarm manager"
  503 on every Swarm endpoint.
- Services and tasks, with the filters the CLI and Traefik send. A task
  is an ordinary container, named and labelled as Docker names it, made
  through the same create as `docker run`. A thread keeps each service
  as its spec says: replicas or global, restart policy, placement
  constraints, rolling updates (parallelism, delay, stop-first or
  start-first, monitor, failure action) and rollbacks.
- `--driver overlay` networks, which are bridge networks that report
  overlay and swarm. A service's name and `tasks.<name>` resolve on
  them to its tasks.
- `--publish` in host mode, and in ingress mode, where one running task
  takes each port.
- Secrets and configs, mounted read-only at their targets.
- `service logs` and task logs, merged from the tasks' containers.

The state is kept in `swarm.json` beside `networks.json`, readable only
by its owner. Not here: joining other nodes, autolock, job services, and
a virtual IP per service (names resolve to the tasks, as with
`--endpoint-mode dnsrr`).

Refs libracoder#603
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant