Replies: 3 comments
-
here is my current # syntax=docker/dockerfile:1.4
# Create .netrc file for private go module
FROM --platform=${BUILDPLATFORM} bufbuild/buf:latest as buf
ARG BUF_USERNAME=my_user_name
SHELL ["/bin/ash", "-o", "pipefail", "-c"]
RUN --mount=type=secret,id=BUF_TOKEN \
buf registry login --username=$BUF_USERNAME --token-stdin < /run/secrets/BUF_TOKEN
# Add tini to act as PID1 for proper signal handling
FROM --platform=${BUILDPLATFORM} alpine as tini
ENV TINI_VERSION v0.19.0
# Use BuildKit to help translate architecture names
ARG TARGETPLATFORM
# translating Docker's TARGETPLATFORM into tini download names
RUN case ${TARGETPLATFORM} in \
"linux/amd64") TINI_ARCH=amd64 ;; \
"linux/arm64") TINI_ARCH=arm64 ;; \
"linux/arm/v7") TINI_ARCH=armhf ;; \
"linux/arm/v6") TINI_ARCH=armel ;; \
"linux/386") TINI_ARCH=i386 ;; \
esac \
&& wget -q https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-static-${TINI_ARCH} -O /tini \
&& chmod +x /tini
FROM --platform=${BUILDPLATFORM} golang:1.18-bullseye AS base
WORKDIR /src
COPY --from=buf /root/.netrc /root/.netrc
ENV CGO_ENABLED=0
ENV GOPRIVATE=github.com/threat-zero/*,go.buf.build
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download && go mod verify
COPY . .
FROM base AS build
ARG TARGETOS
ARG TARGETARCH
RUN --mount=target=. \
--mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build \
-a -trimpath -o /app ./service/account/main.go
FROM gcr.io/distroless/static:nonroot AS final
WORKDIR /
COPY --from=tini --chown=nonroot:nonroot /tini /tini
ENTRYPOINT ["/tini", "--"]
COPY --from=build --chown=nonroot:nonroot src/config /config
COPY --from=build --chown=nonroot:nonroot /app /app
EXPOSE 8080
USER nonroot:nonroot
# Metadata params
ARG TARGET=account
ARG DOCKER_REGISTRY=ghcr.io
ARG DOCKER_CONTEXT_PATH=myorg
ARG BUILD_DATE
ARG VERSION
ARG VCS_URL=myrepo
ARG VCS_REF=1
ARG VENDOR=myorg
# Metadata
LABEL org.opencontainers.image.created=$BUILD_DATE \
org.opencontainers.image.name="${TARGET}" \
org.opencontainers.image.title="${TARGET}" \
org.opencontainers.image.description="Example multi-stage docker build" \
org.opencontainers.image.url=https://github.com/myorg/$VCS_URL \
org.opencontainers.image.source=https://github.com/myorg/$VCS_URL \
org.opencontainers.image.revision=$VCS_REF \
org.opencontainers.image.version=$VERSION \
org.opencontainers.image.vendor=$VENDOR \
org.opencontainers.image.ref.name=$VCS_REF \
org.opencontainers.image.licenses=MIT
CMD ["/app"] Build steps: VERSION=$(git describe --tags || echo "HEAD")
BUILD_DATE=$(date +%FT%T%Z)
DOCKER_IMAGE=ghcr.io/myorg/myrepo/account
# build
docker buildx create --use
docker buildx build --platform linux/arm64,linux/amd64 \
-t $DOCKER_IMAGE:$VERSION \
-t $DOCKER_IMAGE:latest \
--build-arg BUILD_DATE=$BUILD_DATE --build-arg VERSION=$VERSION \
--secret id=BUF_TOKEN,src=buf_token.txt \
-f Dockerfile.local --push .
# inspect
docker buildx imagetools inspect $DOCKER_IMAGE:$VERSION
docker buildx imagetools inspect --raw $DOCKER_IMAGE:$VERSION
# run
docker run -it --rm --platform linux/arm64 $DOCKER_IMAGE:$VERSION
docker run -it --rm --platform linux/amd64 $DOCKER_IMAGE:$VERSION
# Notice `platform` in the build_info logs
# build_info={"branch":"main","build_time":"","commit":"","compiler":"gc","go_version":"go1.18.5","platform":"linux/arm64","state":"dirty","tag":""} |
Beta Was this translation helpful? Give feedback.
-
On your dockers config in goreleaser.yml, you can pass any flags you want, so you should be able to use the same switch case you already have. |
Beta Was this translation helpful? Give feedback.
-
The about Dockerfile-based build approach was a temporary workaround for me to add GoReleaser The benefit I see with What I was looking for is native support to add platform-specific |
Beta Was this translation helpful? Give feedback.
-
I am trying to make the Docker container respond to
SIGTERM
andSIGINT
and enable graceful shutdown.For that, I wanted to add
tini
to goreleaser multi-platform build process documented by @caarlos0Are there any examples to pick TARGETPLATFORM specific
tini
binary and add it to multi-platform Dockerfile in.goreleaser.yaml
?Inspiration from multi-platform docker build with tini for nodejs
Reference:
Beta Was this translation helpful? Give feedback.
All reactions