Deploying a Static Go Binary with systemd Instead of Docker

Why I deploy some Go services as a single static binary with systemd instead of a container, what it looks like, and when Docker is still the better fit.

· 2 min read · Erik Kubica

In 2018, I containerized almost every application I touched. Docker was the standard answer: bundle the operating system, the runtime, the dynamic libraries, and your code into a self-contained image, ship it to a registry, and run it the same way anywhere.

It worked, and I liked it.

Five years later, I woke up to a security alert from our cloud vulnerability scanner. A critical OpenSSL CVE had been found in Debian Buster. Our application did not use OpenSSL, since it was a pure Go API using native cryptographic packages. But because our Dockerfile started with FROM golang:1.20, twenty-eight container images in our registry were flagged as vulnerable.

I was spending several hours each month rebuilding base images, upgrading Alpine packages, managing multi-stage Docker builds, and dealing with Docker daemon memory use on 2 GB virtual servers.

That was the week I stopped using Docker for those Go services in production.

The 14 MB Static Executable

A Go binary can be built with no external dependencies, which is what makes this setup possible.

When you compile with CGO disabled:

$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o dist/api ./cmd/server
$ ls -lh dist/api
-rwxr-xr-x 1 erik erik 14M Nov  8 08:30 dist/api
$ file dist/api
dist/api: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped

The part to look for in the output of file is statically linked, stripped.

That single 14 megabyte file contains:

  • The compiled application machine code
  • The Go runtime and garbage collector
  • The HTTP/1.1 and HTTP/2 web servers
  • Embedded HTML templates and static assets (via //go:embed)
  • TLS certificate authorities and timezone databases

It does not link against libc. It does not need Node, Python, or PHP. You can drop it onto a clean Ubuntu 24.04 server, an Alpine minimal install, or an older CentOS box, and it should run.

Deployment: A Couple of Seconds Over scp

Here is how the operational workflow compared for me, shipping a container versus shipping a static executable.

The Docker workflow:

Step 1: Build image (45 seconds)
Step 2: Scan vulnerabilities (30 seconds)
Step 3: Push 420 MB layer to remote registry (60 seconds)
Step 4: SSH to server and pull 420 MB layer (45 seconds)
Step 5: Stop old container and run new container (10 seconds)

The static binary workflow:

$ scp dist/api deploy@prod-app-01:/usr/local/bin/api.new
dist/api                     100%   14MB  12.4MB/s   00:01
$ ssh deploy@prod-app-01 "mv /usr/local/bin/api.new /usr/local/bin/api && sudo systemctl restart api"

Total elapsed time on my setup: about two seconds.

There are no registry credentials to rotate and no image pruning cron jobs to clear out dangling layers in /var/lib/docker/overlay2. It also removed the Docker daemon's roughly 200 MB of RAM on small servers.

A 15-Line systemd Service

"What about process supervision, environment variables, and auto-restart?"

Linux already ships with a solid process supervisor: systemd.

; /etc/systemd/system/api.service
[Unit]
Description=Production API Service
After=network.target

[Service]
Type=simple
User=www-data
Group=www-data
ExecStart=/usr/local/bin/api
Restart=always
RestartSec=3s
LimitNOFILE=65535
ProtectSystem=strict
ProtectHome=yes
NoNewPrivileges=yes

[Install]
WantedBy=multi-user.target

You get automatic restarts on failure, journald log rotation, cgroup resource limits, and sandboxing without running an extra daemon.

Containers make a lot of sense when your runtime has a big dependency graph, or when you run many services on shared infrastructure. For a single static binary, I found they added more than they gave.

If your service is a single static Go binary on a server you control, this is worth trying: compile it statically, copy it over, and let systemd supervise it. If you need orchestration, or your stack has many moving parts, Docker is still the right call.

Tell me what you need built

Usually a reply within a day, by email or WhatsApp.