make buildx: use multi-stage to make tagged image smaller

The build-stage would still be in the local cache (and can
be cleaned up with `docker system prune`) but the tagged
image (which usually wouldn't be removed) will take up
less space now.

Note that

 - the binary is not statically linked, so we cannot use
   a "from scratch" image
 - in cases where the binary is cross-compiled (e.g.
   on a non-linux machine), the image itself is not
   really useful (we may want to consider not tagging
   the image in that situation)

Before:

    REPOSITORY      TAG     IMAGE ID      CREATED         SIZE
    moby-buildx     latest  c9b2af465baf  7 minutes ago   1.71GB

After:

    REPOSITORY      TAG     IMAGE ID      CREATED         SIZE
    moby-buildx     latest  345501e2df0a  2 minutes ago   820MB

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2019-10-14 15:27:52 +02:00
parent a602ecf8fb
commit b6ae3c2058
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -2,7 +2,7 @@ ARG GO_VERSION=1.12.10
ARG BUILDX_COMMIT=v0.3.0
ARG BUILDX_REPO=https://github.com/docker/buildx.git
FROM golang:${GO_VERSION}-stretch
FROM golang:${GO_VERSION}-stretch AS build
ARG BUILDX_REPO
RUN git clone "${BUILDX_REPO}" /buildx
WORKDIR /buildx
@ -21,4 +21,7 @@ RUN GOOS="${GOOS}" GOARCH="${GOARCH}" BUILDX_COMMIT="${BUILDX_COMMIT}"; \
-X \"${pkg}/version.Package=buildx\" \
"; \
go build -ldflags "${ldflags}" -o /usr/bin/buildx ./cmd/buildx
FROM golang:${GO_VERSION}-stretch
COPY --from=build /usr/bin/buildx /usr/bin/buildx
ENTRYPOINT ["/usr/bin/buildx"]