RSS DEV Community

Simple Golang Dockerfile with multi staged builds (reduces ~96.67% of the image size)

This Dockerfile reduces the size of a Golang Docker image from approximately 1GB to 40MB and speeds up the rebuild process. The file uses a two-stage build process, with the first stage building the Go application using the golang:1.23-alpine base image, and the second stage creating a minimal image for running the app using the alpine:latest base image. The builder stage downloads dependencies, builds the application, and creates a binary, which is then copied to the runner stage. The runner stage sets environment variables, exposes a port, and defines the command to run the application.
dev.to
dev.to