fix cross-compiled docker images

binaries for linux/amd64 were build for each target platform. apparently the
--platform in the first-stage overrides the $TARGET* variables of the build.
docker behaviour always manages to surprise me...
This commit is contained in:
Mechiel Lukkien 2023-02-27 13:46:29 +01:00
parent 92e018e463
commit f3f2c6f8ea
No known key found for this signature in database
2 changed files with 18 additions and 9 deletions

View file

@ -1,8 +1,12 @@
FROM --platform=linux/amd64 docker.io/golang:1-alpine AS build FROM --platform=linux/amd64 docker.io/golang:1-alpine AS build
# note: cannot use $TARGETOS or $TARGETARCH because apparently the --platform in
# the FROM above overrides the actual target os/arch from the command-line.
ARG goos
ARG goarch
WORKDIR / WORKDIR /
ARG moxversion ARG moxversion
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go install -mod mod -trimpath github.com/mjl-/mox@$moxversion RUN CGO_ENABLED=0 GOOS=$goos GOARCH=$goarch go install -mod mod -trimpath github.com/mjl-/mox@$moxversion
RUN test -f /go/bin/mox && cp /go/bin/mox /bin/mox || cp /go/bin/${TARGETOS}_${TARGETARCH}/mox /bin/mox RUN test -f /go/bin/mox && cp /go/bin/mox /bin/mox || cp /go/bin/${goos}_${goarch}/mox /bin/mox
# Using latest may break at some point, but will hopefully be convenient most of the time. # Using latest may break at some point, but will hopefully be convenient most of the time.
FROM --platform=$TARGETPLATFORM docker.io/alpine:latest FROM --platform=$TARGETPLATFORM docker.io/alpine:latest

View file

@ -32,18 +32,23 @@ alpineversion=alpine$(podman run alpine:latest cat /etc/alpine-release)
# We assume the alpines for all platforms have the same version... # We assume the alpines for all platforms have the same version...
echo Building with $goversion and $alpineversion echo Building with $goversion and $alpineversion
# We build the images individually so we can pass goos and goarch ourselves,
# needed because the platform in "FROM --platform <image>" in the first stage
# seems to override the TARGET* variables.
test -d empty || mkdir empty test -d empty || mkdir empty
podman build --platform $platforms -f Dockerfile.release -v $HOME/go/pkg/sumdb:/go/pkg/sumbd:ro --build-arg moxversion=$moxversion --manifest docker.io/moxmail/mox:$moxversion-$goversion-$alpineversion empty (podman manifest rm moxmail/mox:$moxversion-$goversion-$alpineversion || exit 0)
for platform in $(echo $platforms | sed 's/,/ /g'); do
goos=$(echo $platform | sed 's,/.*$,,')
goarch=$(echo $platform | sed 's,^.*/,,')
podman build --platform $platform -f Dockerfile.release -v $HOME/go/pkg/sumdb:/go/pkg/sumbd:ro --build-arg goos=$goos --build-arg goarch=$goarch --build-arg moxversion=$moxversion --manifest moxmail/mox:$moxversion-$goversion-$alpineversion empty
done
cat <<EOF cat <<EOF
# Suggested commands to push images: # Suggested commands to push images:
podman manifest push --all docker.io/moxmail/mox:$moxversion-$goversion-$alpineversion podman manifest push --all moxmail/mox:$moxversion-$goversion-$alpineversion docker.io/moxmail/mox:$moxversion-$goversion-$alpineversion
podman tag docker.io/moxmail/mox:$moxversion-$goversion-$alpineversion docker.io/moxmail/mox:$moxversion podman manifest push --all moxmail/mox:$moxversion-$goversion-$alpineversion docker.io/moxmail/mox:$moxversion
podman manifest push --all docker.io/moxmail/mox:$moxversion podman manifest push --all moxmail/mox:$moxversion-$goversion-$alpineversion docker.io/moxmail/mox:latest
podman tag docker.io/moxmail/mox:$moxversion docker.io/moxmail/mox:latest
podman manifest push --all docker.io/moxmail/mox:latest
EOF EOF