Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit 755db3d

Browse files
author
Paulo Gomes
committed
Optimise image
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
1 parent 94c6239 commit 755db3d

File tree

3 files changed

+59
-36
lines changed

3 files changed

+59
-36
lines changed

Dockerfile

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ ARG XX_VERSION=1.1.0
55

66
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
77

8-
FROM golang:${GO_VERSION}-${BASE_VARIANT} as gostable
9-
10-
FROM gostable AS go-linux
11-
12-
FROM --platform=$BUILDPLATFORM ${BASE_VARIANT} AS build-deps
8+
FROM --platform=$BUILDPLATFORM ${BASE_VARIANT} AS build-base
139

1410
RUN apk add --no-cache \
1511
bash \
@@ -26,15 +22,10 @@ RUN apk add --no-cache \
2622

2723
COPY --from=xx / /
2824

29-
ARG TARGETPLATFORM
30-
31-
RUN xx-apk add --no-cache \
32-
xx-c-essentials
33-
34-
RUN xx-apk add --no-cache \
35-
xx-cxx-essentials
25+
FROM build-base AS build-cross
3626

3727
ARG TARGETPLATFORM
28+
3829
RUN xx-apk add --no-cache \
3930
build-base \
4031
pkgconfig \
@@ -48,7 +39,6 @@ RUN xx-apk add --no-cache \
4839
WORKDIR /build
4940
COPY hack/static.sh .
5041

51-
ARG TARGETPLATFORM
5242
ENV CC=xx-clang
5343
ENV CXX=xx-clang++
5444

@@ -68,12 +58,25 @@ RUN ./static.sh build_libssh2
6858
RUN ./static.sh build_libgit2
6959

7060

71-
FROM go-${TARGETOS} AS build
61+
# trimmed removes all non necessary files (i.e. openssl binary).
62+
FROM build-cross AS trimmed
7263

73-
# Copy cross-compilation tools
74-
COPY --from=xx / /
75-
# Copy compiled libraries
76-
COPY --from=build-deps /usr/local/ /usr/local/
64+
ARG TARGETPLATFORM
65+
RUN mkdir -p /trimmed/usr/local/$(xx-info triple)/ && \
66+
mkdir -p /trimmed/usr/local/$(xx-info triple)/share
67+
68+
RUN cp -r /usr/local/$(xx-info triple)/lib/ /trimmed/usr/local/$(xx-info triple)/ && \
69+
cp -r /usr/local/$(xx-info triple)/lib64/ /trimmed/usr/local/$(xx-info triple)/ | true && \
70+
cp -r /usr/local/$(xx-info triple)/include/ /trimmed/usr/local/$(xx-info triple)/ && \
71+
cp -r /usr/local/$(xx-info triple)/share/doc/ /trimmed/usr/local/$(xx-info triple)/share/
72+
73+
FROM scratch as libs-arm64
74+
COPY --from=trimmed /trimmed/ /
75+
76+
FROM scratch as libs-amd64
77+
COPY --from=trimmed /trimmed/ /
78+
79+
FROM scratch as libs-armv7
80+
COPY --from=trimmed /trimmed/ /
7781

78-
COPY ./hack/Makefile /Makefile
79-
COPY ./hack/static.sh /static.sh
82+
FROM libs-$TARGETARCH$TARGETVARIANT as libs

Dockerfile.test

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,59 @@
11
ARG BASE_VARIANT=alpine
2+
ARG GO_VERSION=1.17.6
3+
ARG XX_VERSION=1.1.0
24

35
ARG LIBGIT2_IMG
46
ARG LIBGIT2_TAG
57

6-
FROM --platform=linux/amd64 ${LIBGIT2_IMG}:${LIBGIT2_TAG} as build-amd64
7-
FROM --platform=linux/arm64 ${LIBGIT2_IMG}:${LIBGIT2_TAG} as build-arm64
8-
FROM --platform=linux/arm/v7 ${LIBGIT2_IMG}:${LIBGIT2_TAG} as build-armv7
8+
FROM ${LIBGIT2_IMG}:${LIBGIT2_TAG} AS build-deps
99

10-
FROM --platform=$BUILDPLATFORM build-$TARGETARCH$TARGETVARIANT AS build
10+
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
1111

12-
RUN apk add clang lld pkgconfig ca-certificates
12+
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} as gostable
1313

14-
ARG TARGETPLATFORM
14+
FROM gostable AS go-linux
1515

16-
# Some dependencies have to installed
17-
# for the target platform: https://github.com/tonistiigi/xx#go--cgo
18-
RUN xx-apk add --no-cache \
19-
musl-dev gcc lld binutils-gold
16+
# Build-base consists of build platform dependencies and xx.
17+
# These will be used at current arch to yield execute the cross compilations.
18+
FROM go-${TARGETOS} AS build-base
19+
20+
RUN apk add clang lld pkgconfig
21+
22+
COPY --from=xx / /
23+
24+
# build-go-mod can still be cached at build platform architecture.
25+
FROM build-base as build-go-mod
2026

2127
WORKDIR /root/smoketest
2228
COPY tests/smoketest/go.mod .
2329
COPY tests/smoketest/go.sum .
2430
RUN go mod download
2531

32+
# Build stage install per target platform
33+
# dependency and effectively cross compile the application.
34+
FROM build-go-mod as build
35+
36+
ARG TARGETPLATFORM
37+
38+
# Some dependencies have to installed
39+
# for the target platform: https://github.com/tonistiigi/xx#go--cgo
40+
RUN xx-apk add musl-dev gcc clang lld
41+
42+
WORKDIR /root/smoketest
43+
2644
COPY tests/smoketest/main.go .
45+
COPY --from=build-deps /usr/local/ /usr/local/
2746

2847
ENV CGO_ENABLED=1
29-
RUN export LIBRARY_PATH="/usr/local/$(xx-info triple)/lib:/usr/local/$(xx-info triple)/lib64:${LIBRARY_PATH}" && \
48+
RUN export LIBRARY_PATH="/usr/local/$(xx-info triple):/usr/local/$(xx-info triple)/lib64" && \
3049
export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig" && \
31-
export FLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2)" && \
32-
CGO_LDFLAGS="${FLAGS} -static" \
33-
xx-go build \
50+
export FLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2)" && \
51+
export CGO_LDFLAGS="${FLAGS} -static" && \
52+
xx-go build \
3453
-ldflags "-s -w" \
3554
-tags 'netgo,osusergo,static_build' \
36-
-o static-test-runner -trimpath main.go;
55+
-o static-test-runner -trimpath main.go
56+
3757

3858
# Ensure that the generated binary is valid for the target platform
3959
RUN xx-verify --static static-test-runner

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ build:
4848
test:
4949
docker buildx build \
5050
--platform=$(PLATFORMS) \
51-
--tag $(IMG):$(TAG) \
51+
--tag $(IMG):$(TAG)-test \
5252
--build-arg LIBGIT2_IMG=$(IMG) \
5353
--build-arg LIBGIT2_TAG=$(TAG) \
5454
--file Dockerfile.test \

0 commit comments

Comments
 (0)