-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDockerfile
More file actions
101 lines (80 loc) · 2.52 KB
/
Copy pathDockerfile
File metadata and controls
101 lines (80 loc) · 2.52 KB
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
FROM --platform=$BUILDPLATFORM rust:1.94 AS build
# Install Protocol Buffers and curl.
RUN apt-get update && apt-get install -y protobuf-compiler curl xz-utils
# Install Zig (used by cargo-zigbuild for cross-compilation).
ARG ZIG_VERSION=0.14.0
RUN ARCH=$(uname -m) && \
mkdir -p /opt/zig && \
curl -sSL "https://ziglang.org/download/${ZIG_VERSION}/zig-linux-${ARCH}-${ZIG_VERSION}.tar.xz" \
| tar -xJ -C /opt/zig --strip-components=1
ENV PATH="/opt/zig:${PATH}"
# Install cargo-zigbuild.
RUN cargo install --locked cargo-zigbuild
# 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
# Add the required Rust target based on the target platform.
RUN <<EOF
set -e;
touch .target
if [ "$TARGETPLATFORM" = "linux/arm64" ]; then
rustup target add aarch64-unknown-linux-musl
echo -n "aarch64-unknown-linux-musl" > .target
elif [ "$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
EOF
# Copy manifests.
COPY ./.cargo/config.toml ./.cargo/config.toml
COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml
# Copy bench sources so Cargo can validate the [[bench]] targets
# in the manifest during the dependency caching step.
COPY ./benches ./benches
# Build the shell project first to get a dependency cache.
RUN <<EOF
set -e;
TARGET=$(cat .target)
if [ -z "$TARGET" ]; then
cargo build --release
rm ./target/release/deps/deltio*
else
cargo zigbuild --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;
TARGET=$(cat .target)
if [ -z "$TARGET" ]; then
cargo build --release
exit 0
fi
cargo zigbuild --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"]