Minimal Docker image for Go App

Minimal Docker image for Go App

September 22, 2023

It is possible to create minimal Docker image with single binary file.

Build Go App

Build Go App for linux as usually:

go build -ldflags="-s -w" -trimpath ./cmd/example

If you work on macOS append flags for cross compilation:

GOARCH=amd64 GOOS=linux go build ...

In current directory will be created binary file example.

Create Docker image

Docker uses tar archives to import images:

tar -c example | docker import - example:latest

Launch Docker container

Now you may start new container with created image:

docker run example:latest /example