Go's standard library does not talk to Docker or Kubernetes for you, but it provides everything a containerised Go service needs: net/http for health endpoints, os/signal and context for graceful shutdown, and log/slog for logs that container platforms collect. To manage containers or clusters from Go you add the official client modules, the Docker Engine SDK and Kubernetes client-go.
Go also suits this work because it compiles to one static binary, which produces very small images that start quickly.
Compile with CGO_ENABLED=0 so the binary has no dependency on a C library, then copy it into a minimal runtime image. Run as a non-root user.
Copying go.mod and go.sum first lets Docker cache the dependency layer, so code changes rebuild quickly. Use a Go version that matches your go.mod and pin the base images by version or digest rather than latest. The older answer put this Dockerfile in a go code block; it is not Go code.
Kubernetes stops a pod by sending SIGTERM, then SIGKILL after the grace period. Handle the signal, stop accepting connections and finish in-flight requests.
The method-and-path patterns such as GET /healthz need Go 1.22 or later. Log to standard output in JSON so the platform's log agent can collect it. Read settings such as database hosts from environment variables and fail fast when a required one is missing.
Since Go 1.25 the runtime sets GOMAXPROCS from the container's CPU limit. On older versions, set it yourself or use the automaxprocs module so a pod limited to a fraction of a CPU does not run with the node's full core count.
Do not call the Docker API over plain HTTP on port 2375. That exposes root-equivalent control of the host without authentication. Use the Docker Engine SDK, which connects to the local Unix socket by default, or run the docker CLI with os/exec.
The SDK packages have been reorganised between releases, so check the import paths against the version you install. For one-off automation, exec.CommandContext(ctx, "docker", "run", "--rm", "alpine", "echo", "hello") is simpler and gives you a timeout through the context.
k8s.io/client-go is the official client. Outside a cluster build the configuration from your kubeconfig. Inside a pod use rest.InClusterConfig(), which picks up the pod's service account.
Grant the service account only the RBAC permissions it needs, for example list and watch on pods in one namespace.
To react to changes, do not poll with List. Use a shared informer, which keeps a local cache and delivers add, update and delete events.
The old example used a metav1.Pod type that does not exist. The pod type is corev1.Pod from k8s.io/api/core/v1. For production operators, build on controller-runtime or Kubebuilder, which add work queues, retries, leader election and reconcile loops on top of informers.
Use a versioned image tag instead of latest, keep liveness checks cheap and independent of dependencies, and let readiness reflect whether the pod can serve traffic. Set a memory limit, and consider GOMEMLIMIT slightly below it so the garbage collector works harder before the kernel kills the container.
No. Use the Docker Engine SDK and k8s.io/client-go. The standard library supplies HTTP, JSON, signals and logging for the services themselves.
A static binary runs on scratch or a distroless static image. Use distroless or Alpine if you need CA certificates and a non-root user already set up.
Listen for SIGTERM with signal.NotifyContext, then call http.Server.Shutdown with a timeout shorter than the pod's termination grace period.
Use controller-runtime or Kubebuilder for operators. Use plain client-go for scripts and tools that list, patch or watch a few resources.
A good containerised Go service is a static binary in a minimal non-root image, with health endpoints, structured logs and graceful shutdown. Manage Docker through its SDK over the local socket, never an open TCP port, and manage Kubernetes with client-go informers or controller-runtime. Set probes, resource limits and versioned image tags in the manifest.