In this exercise, we aim to build a lightweight Dockerized API that weighs less than 20 MB. To achieve this, we'll use a multi-stage build and Alpine Linux — a minimal base image designed small containers. Our API will be written in Go.
Project Structure
exec01/
├── Dockerfile
├── go
│ ├── go.mod
│ └── main.go
- Dockerfile
FROM golang:1.24.3-alpine AS builder
ARG TARGETARCH
WORKDIR /app
COPY ./go .
RUN go build -o app && chmod +x app
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/app /app/app
EXPOSE 8080
CMD ["/app/app"]
- Go Module File
module exec01/helloworld
go 1.24.3
- Main Application (Go)
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", Handler)
http.ListenAndServe(":8080", nil)
}
func Handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
}
Top comments (0)