/tiny-go-container

How to minify the size of a Golang container

Primary LanguageGo

Going from a Big Golang Docker Container to a Tiny One

Creating the Golang program

package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello World!")
}

File size:

  • 78 bytes or 0.000078 megabytes

Compiled file size:

  • 2 megabytes

Just that!

1st Dockerfile (Complete Golang in an uknown OS)

The fastest way to deploy a Golang container is by creating an image like this:

FROM golang:1.10.0
WORKDIR /app
ADD . /app
RUN cd /app && go build -o goapp
# EXPOSE 8080 
ENTRYPOINT ./goapp

I commented the EXPOSE beacuse my program doesn't need it, if you're creating a web app uncomment it!

This created a Container with a size of 782MB!!! This is to much for just a tiny program. We can improve it!

2nd Dockerfile (Using Alpine)

By using a smaller Linux Distribution like Alpine it looks like this:

FROM golang:alpine
WORKDIR /app
ADD . /app
RUN cd /app && go build -o goapp
# EXPOSE 8080
ENTRYPOINT ./goapp

The size went from 782MB to 378MB. That's better but it still needs some improvements

3rd Dockerfile (Compiling it in a Dev Container and Run it in a fresh Prod Container)

To optimize the Dockerfile we are going to create a container with Golang, compile it and then copy just the compiled program to a new container:

FROM golang:alpine AS build-env
WORKDIR /app
ADD . /app
RUN cd /app && go build -o goapp

FROM alpine
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
WORKDIR /app
COPY --from=build-env /app/goapp /app

# EXPOSE 8080
ENTRYPOINT ./goapp

Final thoughts

The final size of the container went from 782MB to 378MB and finally to 6.71MB

The program used 2MB and the final container uses 6.71MB so the container is using 4.71MB

We minified the container and decreased the size a 99.14%