2024-04-29 12:46:00 +00:00
|
|
|
# Set the default Go version as a build argument
|
|
|
|
ARG XGO="go-1.21.x"
|
2022-07-15 22:59:55 +00:00
|
|
|
|
2024-04-29 12:46:00 +00:00
|
|
|
# Use xgo (a Go cross-compiler tool) as build image
|
|
|
|
FROM --platform=$BUILDPLATFORM techknowlogick/xgo:${XGO} as build
|
2022-07-15 22:59:55 +00:00
|
|
|
|
2024-04-29 12:46:00 +00:00
|
|
|
# Set the working directory and copy the source code
|
|
|
|
WORKDIR /go/src/codeberg.org/codeberg/pages
|
|
|
|
COPY . /go/src/codeberg.org/codeberg/pages
|
2022-07-15 22:59:55 +00:00
|
|
|
|
2024-04-29 12:46:00 +00:00
|
|
|
# Set the target architecture (can be set using --build-arg), buildx set it automatically
|
|
|
|
ARG TARGETOS TARGETARCH
|
|
|
|
|
|
|
|
# Build the binary using xgo
|
|
|
|
RUN --mount=type=cache,target=/root/.cache/go-build \
|
|
|
|
--mount=type=cache,target=/go/pkg \
|
|
|
|
GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=1 \
|
|
|
|
xgo -x -v --targets=${TARGETOS}/${TARGETARCH} -tags='sqlite sqlite_unlock_notify netgo' -ldflags='-s -w -extldflags "-static" -linkmode external' -out pages .
|
|
|
|
|
|
|
|
# Use a scratch image as the base image for the final container,
|
|
|
|
# which will contain only the built binary and the CA certificates
|
2022-07-15 22:59:55 +00:00
|
|
|
FROM scratch
|
2024-04-29 12:46:00 +00:00
|
|
|
|
|
|
|
# Copy the built binary and the CA certificates from the build container to the final container
|
|
|
|
COPY --from=build /go/src/codeberg.org/codeberg/pages/ /pages
|
2022-07-15 22:59:55 +00:00
|
|
|
COPY --from=build \
|
|
|
|
/etc/ssl/certs/ca-certificates.crt \
|
|
|
|
/etc/ssl/certs/ca-certificates.crt
|
2022-11-11 22:51:45 +00:00
|
|
|
|
2024-04-29 12:46:00 +00:00
|
|
|
# Expose ports 80 and 443 for the built binary to listen on
|
|
|
|
EXPOSE 80/tcp
|
|
|
|
EXPOSE 443/tcp
|
|
|
|
|
|
|
|
# Set the entrypoint for the container to the built binary
|
2022-11-11 22:51:45 +00:00
|
|
|
ENTRYPOINT ["/pages"]
|