The stock Caddy package from the Cloudsmith repo ships every module that lives in the main Caddy repo and nothing else. The moment a deployment needs a DNS provider for ACME DNS-01, a shared storage backend for a cluster, or a third-party handler, you stop installing Caddy from a package and start building it. The tool that does the building is xcaddy. This article is the build pipeline we run in production — how to compile a binary with the exact module set a workload needs, make the build reproducible across machines, and stand the binary up as a clean drop-in for the package.
How to verify
What you start from on the build machine — Go toolchain present, xcaddy installed, network reachable for module fetches:
go version
xcaddy version
ls -la "$(go env GOPATH)/bin/xcaddy" 2>/dev/null
curl -sI https://proxy.golang.org/ | head -1
go env GOPROXY GOSUMDB
On the target host, what is in the existing binary you might replace:
caddy version
caddy list-modules | wc -l
sha256sum "$(which caddy)"
xcaddy version should match a known good release pinned in the build script. The GOSUMDB value matters — sum.golang.org is the default and is what makes a build reproducible across machines.
What’s happening
Caddy has no runtime plugin loading. Every module — handlers, matchers, ACME issuers, DNS providers, storage backends — is a Go package that is statically linked into the binary at compile time. To add a module to your Caddy install, you produce a new binary.
xcaddy is a small Go program that writes a main.go importing every module you list, runs go build, and drops a binary in the current directory. The standard pattern looks like:
xcaddy build \
--with github.com/caddy-dns/cloudflare \
--with github.com/caddyserver/caddy-storage-redis \
--output /usr/local/bin/caddy-custom
Under the hood, xcaddy generates a one-file Go program that does import _ "github.com/caddy-dns/cloudflare" for each --with, then invokes go build with the standard Caddy cmd/caddy/main.go entry point. The Go module system fetches the modules, the checksum database verifies them, and the linker produces a static binary. There is no glibc dependency, no dynamic loader trickery — a 40-60 MB binary you can copy to any matching CPU architecture.
Reproducibility is the property that matters most for an MSP. Given the same Caddy version, the same plugin versions, the same Go toolchain, and the same GOSUMDB, you get the same binary every time. We treat that as a contract: when the binary hash on a host changes, somebody changed the build inputs and that change should appear in version control. See caddy modules plugins for a survey of which modules exist.
The procedure
-
Pin every input. A working
xcaddy buildcommand for a real deployment looks like this — note that every version is explicit:xcaddy build v2.8.4 \ --with github.com/caddy-dns/cloudflare@v0.0.0-20240703190432-eb4f2eaccd0d \ --with github.com/caddyserver/caddy-storage-redis@v1.1.3 \ --with github.com/ueffel/caddy-brotli@v0.4.0 \ --output ./caddy-customCaddy version, every plugin version. No
@latestanywhere —@latestmeans “the build is non-reproducible by definition.” If a plugin doesn’t tag releases, pin a commit hash. -
Build in CI, not on the target. The production hosts should not have a Go toolchain. Build in a GitHub Actions, GitLab CI, or Drone job; publish the artifact to your release bucket; let configuration management pull the signed binary down:
# .github/workflows/caddy-build.yml (excerpt) - uses: actions/setup-go@v5 with: { go-version: '1.22' } - run: | go install github.com/caddyserver/xcaddy/cmd/xcaddy@v0.4.2 xcaddy build v2.8.4 \ --with github.com/caddy-dns/cloudflare@v0.0.0-20240703190432-eb4f2eaccd0d \ --output caddy sha256sum caddy > caddy.sha256 - uses: actions/upload-artifact@v4 with: { name: caddy-linux-amd64, path: caddy* }The
caddy.sha256file is what configuration management compares against before installing. -
Verify the module set. After the build, list modules to confirm everything you asked for is present:
./caddy-custom list-modules --versions | grep -E 'cloudflare|redis|brotli'If a
--withsilently fell out of the build (a typo in the import path, a deleted plugin),list-modulesis where you catch it. -
Install on the target. The systemd unit references
caddyby name in/usr/bin/. Stop the service, swap the binary, validate the config under the new binary, then start:sudo systemctl stop caddy sudo install -m 0755 caddy-custom /usr/bin/caddy sudo -u caddy caddy validate --config /etc/caddy/Caddyfile sudo systemctl start caddy sudo systemctl status caddyThe
installstep preserves the right ownership and mode in one command — better thanmvandchmod. -
Record the build manifest. A small file lives next to the config — checked into the same repo:
# /etc/caddy/BUILD.txt caddy v2.8.4 xcaddy v0.4.2 go 1.22.5 built 2026-06-02 from ci-run #4218 sha256: 7c4d9e... (see release artifact) plugins: github.com/caddy-dns/cloudflare v0.0.0-20240703190432-eb4f2eaccd0d github.com/caddyserver/caddy-storage-redis v1.1.3 github.com/ueffel/caddy-brotli v0.4.0When something is misbehaving at 02:00, this file answers “what is actually running here” without a build-system spelunk.
-
Containerize where appropriate. For Docker-based deployments, the same idea fits a multi-stage Dockerfile — build stage runs
xcaddy, runtime stage copies the binary into ascratchorgcr.io/distroless/staticimage. The result is a 50 MB container with no shell and no package manager, which makes the security posture much easier to defend.
Common pitfalls
xcaddy buildwithout an explicit version argument builds againstlatestCaddy — a moving target. Always passv2.x.yas the first positional argument so the build is reproducible.- A
replacedirective inxcaddy(--with github.com/foo/bar=../local-fork) is great for testing a fork, but it must never end up in a CI build script. The replacement path is machine-local; CI will fail or, worse, pick up an unrelated module. - The Go module proxy can serve stale data for a few minutes after a tag is pushed; if
xcaddycomplains about a version it cannot fetch, wait or setGOPROXY=directfor that build only. xcaddywrites a temporarygo.modin a working directory; if the build host’s$TMPDIRis small, the build fails with a confusing “no space left on device” — pointTMPDIRat a larger volume or use the--build-dirflag.- A custom binary is not updated by
apt upgrade. If your fleet mixes stock and custom Caddy, the package’scaddy.serviceunit will still update around you — review the unit on each minor bump and re-apply any local edits.
Stack Harbor operates Caddy installs where the module set matters — DNS providers wired to client registrars, Redis storage for multi-node cert sharing, third-party auth modules integrated with their IdP — and we treat the build pipeline as code under review. The binary that lands on production is built in CI, hash-pinned, signed, and the manifest is checked in next to the Caddyfile. If you run a Caddy stack that has outgrown the stock binary and you want the build/test/deploy operated for you, that work sits inside our Managed Operations practice.