Menu

A Go web service on a free container tier: a 15 MB image and tens of megabytes of RAM

DEV Community: go·SnapDeploy·about 15 hours ago
#Ba9TZ3Bd
#devtogo#devto#dev#container#service
Reading 0:00
15s threshold

TL;DR: A net/http service with a health endpoint, graceful shutdown and the port read from the environment, built by a two-stage Dockerfile into a 7 MB binary and an image around 15 MB. What it uses at runtime on 512 MB and a quarter of a CPU, how to add a database without adding bloat, how to embed static files in the binary, and what sleep looks like in the logs.

If a free container tier was designed for one language, it was Go. A Go web service compiles to a single static binary, starts in milliseconds and idles in a few tens of megabytes, so 512 MB and a quarter of a CPU is not a constraint, it is a surplus. This guide builds a small HTTP service the way it should be built for a container: a two-stage Dockerfile, a tiny image, a health endpoint, graceful shutdown, and the port read from the environment.

The service

Standard library only. net/http is enough for most services and it keeps the binary small.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.