Go Docker deployment packs your compiled program into a small container image that runs the same way on every server. In this post, you will build a static binary and write a multi-stage Dockerfile. Then you will run the result in an image of only about 8 MB.
This lesson builds directly on Go cross compilation for any OS, because a container needs a Linux binary. Our example app also reads its port from the environment, just like in Go environment variables for configuration.
What Is Go Docker Deployment?
First, a container is a lightweight box that holds your program and everything it needs to run. Then Docker builds that box from a recipe file called a Dockerfile. As a result, the app runs the same on your laptop, a test server and the cloud.
For example, think of a lunch box. You pack the meal once at home, and then it travels anywhere unchanged. Similarly, Go Docker deployment packs one binary and ships it to any machine with Docker.
Above all, Go is a great fit for containers. In fact, a Go program compiles into a single file with no runtime to install. So the image can stay tiny.
The Example App for Go Docker Deployment
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.HandleFunc("GET /hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello from a container!")
})
http.HandleFunc("GET /health", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "ok")
})
log.Printf("listening on :%s", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
In fact, this small server is enough to show the whole process. Also, the /health route gives Docker and cloud platforms an easy way to check that the app is alive. Earlier, you built a bigger version in the Go HTTP server tutorial.
A Multi-Stage Dockerfile for Go Docker Deployment
# Stage 1: build the binary
FROM golang:1.24-alpine AS build
WORKDIR /src
COPY go.mod go.sum* ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /app .
# Stage 2: run it in an empty image
FROM scratch
COPY --from=build /app /app
EXPOSE 8080
USER 65534:65534
ENTRYPOINT ["/app"]
In short, a multi-stage build uses two images. First, the large Go image compiles your code. Then only the finished binary moves into scratch, which is a completely empty image. For instance, the diagram below shows this hand-over.

The build stage holds the Go toolchain and source code, but only the static binary is copied into the empty scratch image that you ship.
Why Each Line Matters
| Line | What it does |
|---|---|
COPY go.mod go.sum* ./ then go mod download | Caches modules, so rebuilds are faster. |
CGO_ENABLED=0 | Makes a static binary that needs no C libraries. |
-ldflags="-s -w" | Drops debug data, so the file is smaller. |
USER 65534:65534 | Runs as a non-root user for safety. |
ENTRYPOINT ["/app"] | Starts your program when the container starts. |
Building and Running the Image
$ docker build -t hello-go .
$ docker images hello-go
hello-go latest 7.89MB
$ docker run -d -p 8080:8080 hello-go
$ curl localhost:8080/hello
Hello from a container!
$ docker run -d -e PORT=9090 -p 9090:9090 hello-go
$ curl localhost:9090/hello
Hello from a container!
Here, the -p 8080:8080 flag connects a port on your machine to the port inside the container. Meanwhile, -e PORT=9090 sets an environment variable, so the same image runs on any port without a rebuild.
Keep the Build Context Small
# .dockerignore
.git
*.md
app
For Go Docker deployment, a .dockerignore file stops Docker from sending useless files to the build. Therefore, builds start faster, and secrets in your folder stay out of the image.
Scratch vs Alpine for Go Docker Images
| Base image | Size | Good for |
|---|---|---|
| scratch | about 8 MB here | Pure Go apps with no outside files |
| alpine | a few MB more | Apps that need a shell or CA certificates |
| distroless static | a little more than scratch | Certificates included, still no shell |
scratch is truly empty, so it has no shell and no CA certificates. However, if your app calls HTTPS services, it needs those certificates. In that case, copy them from the build stage or use a distroless or Alpine base instead.
Common Mistakes with Go Docker Deployment
| Problem you see | Why it happens | How to fix it |
|---|---|---|
| exec /app: no such file or directory | Because the binary was built with cgo and needs C libraries. | So build with CGO_ENABLED=0. |
| x509: certificate signed by unknown authority | Because scratch has no CA certificates. | So copy certificates or use distroless. |
| curl cannot connect to the app | Because the port was not published. | So add -p 8080:8080 to docker run. |
| Image is over 800 MB | Because the final stage still uses the Go image. | So use a multi-stage build. |
In fact, container questions are common in backend and DevOps interviews. So practise with my Go interview questions and answers.
Official Resources
These pages come from the Go and Docker teams, so they stay accurate and up to date.
- Go docs: Compile and install the application
- Docker docs: Dockerfile reference
- Docker docs: Multi-stage builds
Quick Recap
- Go Docker deployment ships one static binary inside a small image.
- A multi-stage build compiles in a big image and runs in a tiny one.
- Use
CGO_ENABLED=0so the binary works inscratch. - Publish ports with
-pand pass settings with-e. - Add CA certificates or use distroless when your app calls HTTPS.
What’s Next?
At last, your app runs in a container. Next, we will make it stop safely when the container shuts down, with graceful shutdown.
Next lesson: Go Graceful Shutdown for HTTP Servers Explained


