Sling Academy
Home/Golang/Creating Minimal Docker Images for Go Apps

Creating Minimal Docker Images for Go Apps

Last updated: November 27, 2024

Introduction to Docker and Go

Docker is an open platform for developing, shipping, and running applications. It allows developers to package applications into containers—standardized executable components combining application source code with the operating system libraries and dependencies required to run that code in any environment. Go, often referred to as Golang, is a highly-efficient, compiled language primarily used for building efficient and scalable web applications and server-side components.

Why Minimal Docker Images?

Minimal Docker images are beneficial because they:

  • Reduce the surface for security vulnerabilities due to the smaller attack vector.
  • Decrease image build times and speed up deployments.
  • Require less storage and network bandwidth.

Building a Minimal Docker Image

To create a minimal Docker image for a Go application, follow these steps:

Create a Sample Go Application

Start by creating a basic Go application. For the sake of this tutorial, we will create an HTTP server.


package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World!")
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

Building the Go Application

Use Go’s build feature to create a binary for the application. This can be done using the following command:


go build -o app main.go

Write the Dockerfile

A Dockerfile contains instructions on how the Docker image for an application should be built. Here’s a Dockerfile for our Go application:


# Start with a base image containing only the needed functionality
FROM golang:alpine as builder

# Set work directory
WORKDIR /app

# Copy Go code into the container image
COPY . .

# Build the Go app with CGO disabled and static linking
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

# Now create the final smaller image
FROM scratch

# Copy the built binary from the previous stage
COPY --from=builder /app/app .

# Command to run the app
CMD ["./app"]

Build and Run Your Docker Image

Build the Docker image using the following command:


docker build -t my-go-app .

Run the Docker container using the submitted image:


docker run -p 8080:8080 my-go-app

Conclusion

By following the steps above, you can create minimal Docker images for your Go applications. The use of multi-stage builds enables creating smaller and more secure images suited for any production environment.

Next Article: Environment Variables in Go: Managing Configurations

Previous Article: Cross-Compiling Go Programs for Any OS

Series: Development and Debugging in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant