-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dockerfile
118 lines (96 loc) · 3.22 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
FROM --platform=$BUILDPLATFORM rust:1.81 AS build
# Install Protocol Buffers.
RUN apt-get update && apt-get install -y protobuf-compiler clang musl-tools musl-dev
# Create a new empty project.
RUN cargo new --bin deltio
WORKDIR /deltio
# The target platform we are compiling for.
# Populated by BuildX
ARG TARGETPLATFORM
# The build platform we are compiling on.
# Populated by BuildX
ARG BUILDPLATFORM
# Install the required cross-compiler toolchain based on the target platform.
# Basically, if the target platform is ARM, then we'll need
# the `gcc-arm-linux-gnueabihf` linker, otherwise we'll need
# the `gcc-multilib`.
# IMPORTANT: This only seems to work on a x86_64 Linux build platform.
RUN <<EOF
set -e;
# This is the file we will be writing the compilation target to for
# subsequent steps.
touch .target
if [ "$TARGETPLATFORM" = "linux/arm64" ]; then
# musl-cross isn't available via apt-get, so have to download and install it manually.
mkdir /opt/musl-cross
wget -P /opt/musl-cross https://musl.cc/aarch64-linux-musl-cross.tgz
tar -xvf /opt/musl-cross/aarch64-linux-musl-cross.tgz -C "/opt/musl-cross"
rustup target add aarch64-unknown-linux-musl
echo -n "aarch64-unknown-linux-musl" > .target
else
if [ "$TARGETPLATFORM" = "linux/amd64" ]; then
rustup target add x86_64-unknown-linux-musl
echo -n "x86_64-unknown-linux-musl" > .target
elif [ "$TARGETPLATFORM" = "linux/386" ]; then
rustup target add i686-unknown-linux-musl
echo -n "i686-unknown-linux-musl" > .target
fi
fi
EOF
# In case we installed the musl-cross tools, add it to the path.
ENV PATH="/opt/musl-cross/aarch64-linux-musl-cross/bin:${PATH}"
# Copy manifests.
COPY ./.cargo/config.toml ./.cargo/config.toml
COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml
# Build the shell project first to get a dependency cache.
RUN <<EOF
set -e;
# If the build platform is the same as the target platform, we don't
# need to use any target.
TARGET=$(cat .target)
# Use clang except for aarch64 musl
if [ "$TARGET" != "aarch64-unknown-linux-musl" ]; then
export CC="clang"
export CXX="clang++"
fi
if [ -z "$TARGET" ]; then
cargo build --release
rm ./target/release/deps/deltio*
else
cargo build --target "$TARGET" --release
rm ./target/*/release/deps/deltio*
fi
# Remove the shell project's code files.
rm src/*.rs
EOF
# Copy the actual source.
COPY ./build.rs ./build.rs
COPY ./proto ./proto
COPY ./src ./src
# Build for release
RUN <<EOF
set -e;
# If the build platform is the same as the target platform, we don't
# need to use any target.
TARGET=$(cat .target)
# Use clang except for aarch64 musl
if [ "$TARGET" != "aarch64-unknown-linux-musl" ]; then
export CC="clang"
export CXX="clang++"
fi
if [ -z "$TARGET" ]; then
cargo build --release
exit 0
fi
cargo build --target "$TARGET" --release
mv "target/$TARGET/release/deltio" "target/release/deltio"
EOF
# Our final base image.
FROM scratch AS deltio
# Copy the build artifact from the build stage
COPY --from=build /deltio/target/release/deltio .
# Expose the default port.
EXPOSE 8085
# Set the startup command to run the binary.
CMD ["./deltio", "--bind", "0.0.0.0:8085"]