Dockerfiles to build Docker images for your Go app. It makes use of Docker's multi-stage build feature to create the Go application binary, and then attach it to a minimal base image. The Go version used is 1.22
, though it should work for most Go versions (not tested). You'll have to tweak the Dockerfile to COPY
any static files required by the application, to be copied to the final image.
There are 4 Dockerfiles provided
It's important that you understand Go build flags to build a functioning binary for your application. Not all apps will work with build configuration provided in the Dockerfiles.
$ docker build -t webgo-helloworld-alpine -f Dockerfile-alpine ./example
$ docker build -t webgo-helloworld-debian -f Dockerfile-debian ./example
$ docker build -t webgo-helloworld-busybox -f Dockerfile-busybox ./example
$ docker build -t webgo-helloworld-scratch -f Dockerfile-scratch ./example
$ docker run -p 8080:8080 --rm -ti webgo-helloworld-alpine
$ docker run -p 8080:8080 --rm -ti webgo-helloworld-debian
$ docker run -p 8080:8080 --rm -ti webgo-helloworld-busybox
$ docker run -p 8080:8080 --rm -ti webgo-helloworld-scratch
The gopher used here was created using Gopherize.me. The expert Docker gopher tells us how to build production ready, secure, Docker images for Go applications!