2022-04-14 15:10:59 +02:00
# This ARG has to be at the top, otherwise the docker daemon does not known what to do with FROM ${RUNTIME_IMAGE}
2024-01-20 19:48:04 +01:00
ARG RUNTIME_IMAGE = gcr.io/distroless/static:nonroot
2022-04-14 15:10:59 +02:00
2021-10-05 16:58:54 +01:00
# All builds should be done using the platform native to the build node to allow
# cache sharing of the go mod download step.
# Go cross compilation is also faster than emulation the go compilation across
# multiple platforms.
2024-02-13 11:19:38 +00:00
FROM --platform=${BUILDPLATFORM} docker.io/library/golang:1.22-bookworm AS builder
2019-01-22 02:50:50 +09:00
# Copy sources
2020-03-29 14:54:36 +01:00
WORKDIR $GOPATH/src/github.com/oauth2-proxy/oauth2-proxy
2018-12-20 11:06:26 +00:00
# Fetch dependencies
2019-07-15 21:38:55 +01:00
COPY go.mod go.sum ./
2021-10-05 16:58:54 +01:00
RUN go mod download
2018-12-20 11:06:26 +00:00
2019-07-11 17:18:07 -05:00
# Now pull in our code
COPY . .
2021-10-05 16:58:54 +01:00
# Arguments go here so that the previous steps can be cached if no external
# sources have changed.
2021-03-29 19:08:40 +01:00
ARG VERSION
2021-10-05 16:58:54 +01:00
ARG TARGETPLATFORM
ARG BUILDPLATFORM
2021-03-29 19:08:40 +01:00
2019-03-20 15:15:47 -07:00
# Build binary and make sure there is at least an empty key file.
# This is useful for GCP App Engine custom runtime builds, because
# you cannot use multiline variables in their app.yaml, so you have to
# build the key into the container and then tell it where it is
# by setting OAUTH2_PROXY_JWT_KEY_FILE=/etc/ssl/private/jwt_signing_key.pem
# in app.yaml instead.
2021-10-05 16:58:54 +01:00
# Set the cross compilation arguments based on the TARGETPLATFORM which is
# automatically set by the docker engine.
RUN case ${ TARGETPLATFORM } in \
"linux/amd64" ) GOARCH = amd64 ; ; \
2022-04-14 10:52:43 -04:00
# arm64 and arm64v8 are equivilant in go and do not require a goarm
# https://github.com/golang/go/wiki/GoArm
2022-11-03 21:24:52 -04:00
"linux/arm64" | "linux/arm/v8" ) GOARCH = arm64 ; ; \
2022-02-17 22:55:57 +01:00
"linux/ppc64le" ) GOARCH = ppc64le ; ; \
2021-10-05 16:58:54 +01:00
"linux/arm/v6" ) GOARCH = arm GOARM = 6 ; ; \
2023-09-08 18:18:20 +02:00
"linux/arm/v7" ) GOARCH = arm GOARM = 7 ; ; \
2021-10-05 16:58:54 +01:00
esac && \
printf " Building OAuth2 Proxy for arch ${ GOARCH } \n " && \
2021-11-13 03:12:46 +05:30
GOARCH = ${ GOARCH } VERSION = ${ VERSION } make build && touch jwt_signing_key.pem
2018-12-20 11:06:26 +00:00
2024-01-20 19:48:04 +01:00
# Copy binary to runtime image
2022-04-14 15:10:59 +02:00
FROM ${RUNTIME_IMAGE}
2020-03-29 14:54:36 +01:00
COPY --from= builder /go/src/github.com/oauth2-proxy/oauth2-proxy/oauth2-proxy /bin/oauth2-proxy
COPY --from= builder /go/src/github.com/oauth2-proxy/oauth2-proxy/jwt_signing_key.pem /etc/ssl/private/jwt_signing_key.pem
2018-12-20 11:06:26 +00:00
2020-03-29 14:54:36 +01:00
ENTRYPOINT [ "/bin/oauth2-proxy" ]