This repository contains a simple calculator application written in Go. It showcases the advantages of using Go with Docker, particularly through multi-stage builds and distroless images.
- Statically-Typed Language: Go compiles directly to machine code and does not require a runtime. This results in smaller and faster executables compared to dynamically-typed languages.
- Efficient Execution: No need for additional runtime dependencies, leading to streamlined containerization.
- Reduced Image Size: Separates the build environment from the runtime environment, ensuring only the final binary is included in the final image.
- Cleaner Builds: Minimizes the size of the Docker image by excluding build tools and dependencies.
- Minimalist Approach: Contains only the essential runtime libraries needed to run the application, excluding unnecessary components like package managers or shells.
- Enhanced Security: Reduces attack surfaces by removing unnecessary utilities from the container.
# Use an official Go image to build the application
FROM golang:1.20 AS build
# Set the working directory inside the container
WORKDIR /app
# Copy the Go application code into the container
COPY . .
# Build the Go application
RUN go build -o calculator calculator.go
# Command to run the app
CMD ["./calculator"]
###########################################
# BASE IMAGE
###########################################
FROM ubuntu AS build
RUN apt-get update && apt-get install -y golang-go
ENV GO111MODULE=off
COPY . .
RUN CGO_ENABLED=0 go build -o /app .
############################################
# HERE STARTS THE MAGIC OF MULTI STAGE BUILD
############################################
FROM scratch
# Copy the compiled binary from the build stage
COPY --from=build /app /app
# Set the entrypoint for the container to run the binary
ENTRYPOINT ["/app"]
Below is a comparison of the Docker image sizes for the generic Dockerfile and the multi-stage Dockerfile:
By leveraging Go's statically-typed nature and Docker's multi-stage builds, we achieve significant benefits:
- Reduced Image Size: Multi-stage builds and distroless images help in creating smaller, more efficient Docker images.
- Enhanced Security: Distroless images minimize attack surfaces by excluding unnecessary utilities.