forked from ferronweb/ferron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.test
More file actions
106 lines (90 loc) · 4.28 KB
/
Dockerfile.test
File metadata and controls
106 lines (90 loc) · 4.28 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
102
103
104
105
106
# Use the official Rust image as a build stage
FROM --platform=$BUILDPLATFORM rust:trixie AS builder
# Define ARGs for target and build platforms
ARG TARGETPLATFORM
ARG BUILDPLATFORM
# Install packages for cross-compiling software
RUN --mount=type=cache,sharing=private,target=/var/cache/apt \
--mount=type=cache,sharing=private,target=/var/lib/apt \
--mount=type=cache,sharing=private,target=/usr/local/cargo/git \
--mount=type=cache,sharing=private,target=/usr/local/cargo/registry \
# Install packages for cross-compiling software with musl libc
apt update && \
if ! [ "$BUILDPLATFORM" = "$TARGETPLATFORM" ]; then \
case "$TARGETPLATFORM" in \
"linux/386") DEBIAN_FRONTEND=noninteractive apt install -y gcc-i686-linux-gnu ;; \
"linux/amd64") DEBIAN_FRONTEND=noninteractive apt install -y gcc-x86-64-linux-gnu ;; \
"linux/arm64") DEBIAN_FRONTEND=noninteractive apt install -y gcc-aarch64-linux-gnu ;; \
"linux/arm/v7") DEBIAN_FRONTEND=noninteractive apt install -y gcc-arm-linux-gnueabihf ;; \
"*") echo "Unsupported target platform for cross-compilation: $TARGETPLATFORM" && exit 1 ;; \
esac \
fi && \
# Install cmake, bindgen CLI, and required dependencies
DEBIAN_FRONTEND=noninteractive apt install -y cmake clang libclang-dev && \
cargo install bindgen-cli
# Install the right Rust target and configure Cargo
RUN \
# Determine the target
if ! [ "$BUILDPLATFORM" = "$TARGETPLATFORM" ]; then \
TARGET_TRIPLE="" && \
TARGET_GCC="" && \
case "$TARGETPLATFORM" in \
"linux/386") TARGET_TRIPLE="i686-unknown-linux-gnu" && TARGET_GCC="i686-linux-gnu-gcc" ;; \
"linux/amd64") TARGET_TRIPLE="x86_64-unknown-linux-gnu" && TARGET_GCC="x86_64-linux-gnu-gcc" ;; \
"linux/arm64") TARGET_TRIPLE="aarch64-unknown-linux-gnu" && TARGET_GCC="aarch64-linux-gnu-gcc" ;; \
"linux/arm/v7") TARGET_TRIPLE="armv7-unknown-linux-gnueabihf" && TARGET_GCC="arm-linux-gnueabihf-gcc" ;; \
"*") echo "Unsupported target platform for cross-compilation: $TARGETPLATFORM" && exit 1 ;; \
esac && \
# Install the Rustup target
rustup target add $TARGET_TRIPLE && \
# Configure Cargo
echo "[target.$TARGET_TRIPLE]\nlinker = \"$TARGET_GCC\"" >> /usr/local/cargo/config.toml; \
else \
TARGET_TRIPLE="$(rustc --print host-tuple)"; \
fi && \
# Save target triple
echo "$TARGET_TRIPLE" > /tmp/target_triple
# Set the working directory
WORKDIR /usr/src/ferron
# Copy the source code
COPY . .
# Build the application and copy binaries to an accessible location
RUN --mount=type=cache,sharing=private,target=/usr/local/cargo/git \
--mount=type=cache,sharing=private,target=/usr/local/cargo/registry \
--mount=type=cache,sharing=private,target=/usr/src/ferron/target \
--mount=type=cache,sharing=private,target=/usr/src/ferron/build/prepare/target \
# Set target triple and path
TARGET_TRIPLE="$(cat /tmp/target_triple)" && \
TARGET_PATH="target/$TARGET_TRIPLE/debug" && \
# Build Ferron binary (use "build-dev" instead of "build" recipe, to build faster, and because the Dockerfile is for testing)
CARGO_FINAL_EXTRA_ARGS="--bin ferron --features ferron/config-docker-auto" \
TARGET="$TARGET_TRIPLE" \
make build-dev && \
# Copy executables out of the cache
mkdir .dist && cp $TARGET_PATH/ferron .dist
# Use a Debian base image for the final image
FROM debian:trixie
# Install CA certificates
RUN --mount=type=cache,sharing=private,target=/var/cache/apt \
--mount=type=cache,sharing=private,target=/var/lib/apt \
apt update && \
apt install -y ca-certificates && \
apt clean && \
rm -rf /var/lib/apt/lists/* && \
rm -rf /var/cache/apt/*
# Copy the compiled binaries from the builder stage
COPY --from=builder /usr/src/ferron/.dist /usr/sbin
# Switch to "nobody" user to make commands like WORKDIR use the correct owner
USER nobody
# Copy the web server configuration
COPY --chown=nobody configs/ferron.docker.kdl /etc/ferron.kdl
# Copy the web root contents
COPY --chown=nobody wwwroot /var/www/ferron/
# Create an ACME cache directory
WORKDIR /var/cache/ferron-acme
# Create a directory where Ferron logs are stored
WORKDIR /var/log/ferron
# Expose the port 80 (used for HTTP)
EXPOSE 80
# Set the command to run the binary
CMD ["/usr/sbin/ferron", "--config-adapter", "docker-auto"]