Skip to content

Commit 42367ad

Browse files
committed
Fix build on arm/v7 and amd64
Ref: docker/buildx#395 Ref: rust-lang/cargo#4133
1 parent 61c100f commit 42367ad

File tree

1 file changed

+57
-13
lines changed

1 file changed

+57
-13
lines changed

docker/Dockerfile

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,56 @@
1-
# Because 1.62.0-alpine3.16 does not have a build for arm/v7
2-
FROM rust:1.62.0-slim as build-env
1+
# See: https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/
2+
3+
FROM --platform=x86_64 rust:1.62-alpine3.16 as build-env-x86_64-musl
4+
5+
ARG TARGETPLATFORM
6+
ARG BUILDPLATFORM
7+
ARG TARGETARCH
8+
ARG TARGETVARIANT
9+
10+
WORKDIR /workspace
11+
12+
ADD https://github.com/FedericoPonzi/Horust/archive/c6a4cedb5f53836f14d12fd809272fdac9c8969b.tar.gz Horust.tar.gz
13+
RUN echo "Building arch: ${TARGETARCH}, variant: ${TARGETVARIANT} for ${TARGETPLATFORM} on ${BUILDPLATFORM}" && \
14+
if [ "${TARGETARCH}" = "amd64" ]; then \
15+
apk add --no-cache musl-dev openssl-dev; \
16+
tar -C ./ --strip-components=1 -xzf ./Horust.tar.gz; \
17+
# https://users.rust-lang.org/t/sigsegv-with-program-linked-against-openssl-in-an-alpine-container/52172
18+
RUSTFLAGS="-C target-feature=-crt-static" cargo build --release; \
19+
else \
20+
mkdir -p /workspace/target/release/; \
21+
# provide a fake file for Docker copy to suceess
22+
touch /workspace/target/release/horust; \
23+
fi
24+
25+
# Because rust:1.62-alpine3.16 does not have a build for arm/v7 but arm32v7/rust:1.62-alpine3.16 has it
26+
# fetch the vendor with the builder platform to avoid qemu issues (https://github.com/docker/buildx/issues/395)
27+
FROM --platform=x86_64 blackdex/rust-musl:armv7-musleabihf-stable-1.62.1 as build-env-horust
28+
29+
ARG TARGETPLATFORM
30+
ARG BUILDPLATFORM
31+
ARG TARGETARCH
32+
ARG TARGETVARIANT
333

434
WORKDIR /workspace
535

6-
RUN apk add --no-cache curl musl-dev openssl-dev && \
7-
curl -s -L -# -o ./Horust.tar.gz https://github.com/FedericoPonzi/Horust/archive/c6a4cedb5f53836f14d12fd809272fdac9c8969b.tar.gz && \
8-
tar -C ./ --strip-components=1 -xzf ./Horust.tar.gz && \
9-
# https://users.rust-lang.org/t/sigsegv-with-program-linked-against-openssl-in-an-alpine-container/52172
10-
RUSTFLAGS="-C target-feature=-crt-static" cargo build --release
36+
COPY --from=build-env-x86_64-musl /workspace/target/release/horust /workspace/horust_x86_64
37+
38+
ADD https://github.com/FedericoPonzi/Horust/archive/c6a4cedb5f53836f14d12fd809272fdac9c8969b.tar.gz Horust.tar.gz
39+
RUN echo "Building arch: ${TARGETARCH}, variant: ${TARGETVARIANT} for ${TARGETPLATFORM} on ${BUILDPLATFORM}" && \
40+
if [ "${TARGETARCH}${TARGETVARIANT}" = "armv7" ]; then \
41+
tar -C ./ --strip-components=1 -xzf ./Horust.tar.gz; \
42+
# https://users.rust-lang.org/t/sigsegv-with-program-linked-against-openssl-in-an-alpine-container/52172
43+
RUSTFLAGS="-C target-feature=-crt-static" cargo build --release --target=armv7-unknown-linux-musleabihf; \
44+
mv /workspace/target/armv7-unknown-linux-musleabihf/release/horust /workspace/horust; \
45+
else \
46+
mv /workspace/horust_x86_64 /workspace/horust; \
47+
fi
1148

12-
FROM alpine:3.16 as build-env-sasl
49+
# fetch the vendor with the builder platform to avoid qemu issues (https://github.com/docker/buildx/issues/395)
50+
FROM --platform=$TARGETPLATFORM alpine:3.16 as build-env-sasl
51+
52+
ARG TARGETPLATFORM
53+
ARG BUILDPLATFORM
1354

1455
WORKDIR /workspace
1556

@@ -24,10 +65,13 @@ RUN apk add --no-cache --update alpine-sdk && \
2465
apk add --no-cache gdbm-dev openssl1.1-compat-dev linux-pam-dev py3-sphinx sqlite-dev openldap-dev automake autoconf libtool && \
2566
abuild -F && \
2667
mv /root/.abuild/[email protected]*.rsa.pub /root/packages/${RSA_PRIVATE_KEY_NAME}.pub && \
27-
mv /root/.abuild/[email protected]*.rsa /root/packages/${RSA_PRIVATE_KEY_NAME} && \
28-
abuild-sign -k /root/packages/${RSA_PRIVATE_KEY_NAME} /root/packages/x86_64/APKINDEX.tar.gz
68+
mv /root/.abuild/[email protected]*.rsa /root/packages/${RSA_PRIVATE_KEY_NAME}
69+
70+
# fetch the vendor with the builder platform to avoid qemu issues (https://github.com/docker/buildx/issues/395)
71+
FROM --platform=$TARGETPLATFORM alpine:3.16
2972

30-
FROM alpine:3.16
73+
ARG TARGETPLATFORM
74+
ARG BUILDPLATFORM
3175

3276
# See: https://www.openldap.org/software/release/changes.html
3377
# See: https://git.openldap.org/openldap/openldap/-/tree/OPENLDAP_REL_ENG_2_6_3
@@ -42,7 +86,7 @@ ARG BUILD_DATE
4286
ENV LDAP_LOG_LEVEL=0
4387
ENV LDAP_NOFILE=1024
4488

45-
COPY --from=build-env /workspace/target/release/horust /sbin/horust
89+
COPY --from=build-env-horust /workspace/horust /usr/bin/horust
4690
COPY --from=build-env-sasl /root/packages/ /root/packages/
4791
RUN apk add --no-cache --update \
4892
# https://github.com/mischov/meeseeks/issues/98#issuecomment-636615680
@@ -70,7 +114,7 @@ RUN apk add --no-cache --update \
70114
# Enable ldapi:///
71115
mkdir /var/lib/openldap/run && \
72116
touch /var/lib/openldap/run/ldapi && \
73-
/sbin/horust --version && \
117+
horust --version && \
74118
# Add dependencies that the next step can not add without a network repo
75119
apk add --no-cache linux-pam sqlite-libs && \
76120
echo '/root/packages/' > /root/packages/repositories && \

0 commit comments

Comments
 (0)