Status | stable |
OCI Reference | cgr.dev/chainguard/go |
Variants/Tags |
Contact Chainguard for enterprise support, SLAs, and access to older tags.
Container image for building Go applications.
The image is available on cgr.dev
:
docker pull cgr.dev/chainguard/go:latest
NOTE: As of 12/30/2022, the default go image uses Wolfi, which is glibc based.
If you were using this image before and are now running into trouble, the musl/Alpine based image is
still available at cgr.dev/chainguard/go:latest-musl
.
To build the Go application in examples/hello/main.go using the host architecture:
docker run --rm -v "${PWD}:/work" -w /work/examples/hello \
-e GOOS="$(go env GOOS)" -e GOARCH="$(go env GOARCH)" \
cgr.dev/chainguard/go build -o /work/hello .
The example application will be built to ./hello
:
$ ./hello
Hello World!
In Go 1.20, we default to using the new GODEBUG
settings of tarinsecurepath=0
and zipinsecurepath=0
.
These can be disabled by clearing the GODEBUG
environment variable, or by setting them to 1
.
Learn more about these settings in the Go release notes.
The following example Dockerfile builds a hello-world program in Go and copies it on top of the cgr.dev/chainguard/static:latest
base image:
# syntax=docker/dockerfile:1.4
FROM cgr.dev/chainguard/go:latest as build
WORKDIR /work
COPY <<EOF go.mod
module hello
go 1.19
EOF
COPY <<EOF main.go
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
EOF
RUN go build -o hello .
FROM cgr.dev/chainguard/static:latest
COPY --from=build /work/hello /hello
CMD ["/hello"]
Run the following command to build the demo image and tag it as go-hello-world
:
docker build -t go-hello-world .
Now you can run the image with:
docker run go-hello-world
You should get output like this:
Hello World!
It’s worth noting how small the resulting image is:
docker images go-hello-world
REPOSITORY TAG IMAGE ID CREATED SIZE
go-hello-world latest 859fedabd532 5 hours ago 3.21MB