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>
... | ... |
@@ -2,7 +2,7 @@ ARG GO_VERSION=1.12.10 |
2 | 2 |
ARG BUILDX_COMMIT=v0.3.0 |
3 | 3 |
ARG BUILDX_REPO=https://github.com/docker/buildx.git |
4 | 4 |
|
5 |
-FROM golang:${GO_VERSION}-stretch |
|
5 |
+FROM golang:${GO_VERSION}-stretch AS build |
|
6 | 6 |
ARG BUILDX_REPO |
7 | 7 |
RUN git clone "${BUILDX_REPO}" /buildx |
8 | 8 |
WORKDIR /buildx |
... | ... |
@@ -21,4 +21,7 @@ RUN GOOS="${GOOS}" GOARCH="${GOARCH}" BUILDX_COMMIT="${BUILDX_COMMIT}"; \ |
21 | 21 |
-X \"${pkg}/version.Package=buildx\" \ |
22 | 22 |
"; \ |
23 | 23 |
go build -ldflags "${ldflags}" -o /usr/bin/buildx ./cmd/buildx |
24 |
+ |
|
25 |
+FROM golang:${GO_VERSION}-stretch |
|
26 |
+COPY --from=build /usr/bin/buildx /usr/bin/buildx |
|
24 | 27 |
ENTRYPOINT ["/usr/bin/buildx"] |