2025-09-01 22:52:33 +03:00
|
|
|
# Build stage with memory optimization
|
2024-11-13 12:03:32 +03:00
|
|
|
FROM rust:slim-bookworm AS build
|
2024-11-13 09:00:11 +03:00
|
|
|
|
2025-09-02 09:09:34 +03:00
|
|
|
# Create larger temporary swap file for extreme memory optimization
|
2025-09-22 09:18:54 +03:00
|
|
|
RUN dd if=/dev/zero of=/tmp/swapfile bs=1M count=4096 && \
|
2025-09-01 22:52:33 +03:00
|
|
|
chmod 600 /tmp/swapfile && \
|
|
|
|
|
mkswap /tmp/swapfile && \
|
2025-09-02 09:09:34 +03:00
|
|
|
swapon /tmp/swapfile || true && \
|
|
|
|
|
# Set swappiness to use swap more aggressively
|
2025-09-22 09:18:54 +03:00
|
|
|
echo 80 > /proc/sys/vm/swappiness || true
|
2025-09-01 22:52:33 +03:00
|
|
|
|
2024-11-13 11:32:50 +03:00
|
|
|
# Install build dependencies
|
2024-11-13 10:31:24 +03:00
|
|
|
RUN apt-get update && \
|
2024-11-13 11:32:50 +03:00
|
|
|
apt-get install -y \
|
|
|
|
|
git \
|
|
|
|
|
pkg-config \
|
|
|
|
|
make \
|
|
|
|
|
g++ \
|
|
|
|
|
libssl-dev \
|
|
|
|
|
libtiff-dev \
|
|
|
|
|
clang \
|
|
|
|
|
libclang-dev \
|
|
|
|
|
pkg-config \
|
|
|
|
|
&& apt-get clean \
|
|
|
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
|
|
|
|
|
|
# Add target
|
|
|
|
|
RUN rustup target add x86_64-unknown-linux-gnu
|
|
|
|
|
|
|
|
|
|
# Create a new empty shell project
|
2024-08-30 21:05:51 +03:00
|
|
|
RUN USER=root cargo new --bin quoter
|
2024-11-13 11:32:50 +03:00
|
|
|
|
2024-08-30 21:05:51 +03:00
|
|
|
WORKDIR /quoter
|
2023-10-02 14:33:26 +03:00
|
|
|
|
2025-09-01 22:52:33 +03:00
|
|
|
# Copy manifests and cargo config
|
2023-09-28 02:08:48 +03:00
|
|
|
COPY ./Cargo.lock ./Cargo.lock
|
|
|
|
|
COPY ./Cargo.toml ./Cargo.toml
|
2025-09-01 22:52:33 +03:00
|
|
|
COPY ./.cargo ./.cargo
|
2023-10-02 14:33:26 +03:00
|
|
|
|
2025-09-02 09:09:34 +03:00
|
|
|
# Extreme memory optimization environment variables
|
2023-09-28 02:08:48 +03:00
|
|
|
ENV CARGO_NET_GIT_FETCH_WITH_CLI=true
|
2025-09-02 09:09:34 +03:00
|
|
|
ENV CARGO_NET_RETRY=3
|
|
|
|
|
ENV CARGO_NET_TIMEOUT=60
|
|
|
|
|
ENV CARGO_HTTP_TIMEOUT=60
|
|
|
|
|
ENV CARGO_HTTP_LOW_SPEED_LIMIT=10
|
|
|
|
|
ENV RUSTC_FORCE_INCREMENTAL=0
|
2025-09-22 09:18:54 +03:00
|
|
|
# Additional memory optimization for Redis connection pool compilation
|
|
|
|
|
ENV CARGO_BUILD_JOBS=1
|
|
|
|
|
ENV RUSTFLAGS="-C opt-level=s -C target-cpu=generic"
|
2025-09-02 09:09:34 +03:00
|
|
|
# Build dependencies only with extreme memory conservation
|
2025-09-02 14:00:54 +03:00
|
|
|
RUN cargo build --release && \
|
2025-09-01 22:52:33 +03:00
|
|
|
# Force cleanup of intermediate files to free memory
|
|
|
|
|
cargo clean -p quoter && \
|
2025-09-02 14:00:54 +03:00
|
|
|
# Keep only the dependency artifacts (suppressing error if dir doesn't exist)
|
|
|
|
|
find target/release/deps -name "quoter*" -delete 2>/dev/null || true
|
2024-11-13 09:08:34 +03:00
|
|
|
|
2025-09-22 09:32:45 +03:00
|
|
|
# Remove the default source file created by cargo new (but keep lib.rs)
|
|
|
|
|
RUN rm src/main.rs || true
|
2023-10-02 14:33:26 +03:00
|
|
|
|
2024-11-13 11:32:50 +03:00
|
|
|
# Copy source code
|
2023-09-28 02:08:48 +03:00
|
|
|
COPY ./src ./src
|
2023-10-02 14:33:26 +03:00
|
|
|
|
2025-09-01 22:52:33 +03:00
|
|
|
# Build for release with aggressive memory cleanup
|
|
|
|
|
RUN cargo build --release && \
|
2025-09-22 09:18:54 +03:00
|
|
|
# Verify the binary was created
|
|
|
|
|
ls -la target/release/quoter && \
|
2025-09-01 22:52:33 +03:00
|
|
|
# Immediately strip the binary to save space
|
|
|
|
|
strip target/release/quoter || true && \
|
|
|
|
|
# Clean up swap file
|
|
|
|
|
swapoff /tmp/swapfile || true && \
|
|
|
|
|
rm -f /tmp/swapfile
|
2023-09-28 02:08:48 +03:00
|
|
|
|
2024-11-13 11:32:50 +03:00
|
|
|
# Final stage
|
2024-11-13 10:31:24 +03:00
|
|
|
FROM debian:bookworm-slim
|
2023-09-28 02:08:48 +03:00
|
|
|
|
2024-11-13 11:42:39 +03:00
|
|
|
# Install runtime dependencies and CA certificates
|
2024-11-13 10:31:24 +03:00
|
|
|
RUN apt-get update && \
|
2024-11-13 11:14:53 +03:00
|
|
|
apt-get install -y --no-install-recommends \
|
2024-11-13 11:42:39 +03:00
|
|
|
ca-certificates \
|
2024-11-13 11:14:53 +03:00
|
|
|
libssl3 \
|
|
|
|
|
libtiff6 \
|
2024-11-13 11:32:50 +03:00
|
|
|
&& apt-get clean \
|
2024-11-13 11:42:39 +03:00
|
|
|
&& rm -rf /var/lib/apt/lists/* \
|
|
|
|
|
&& update-ca-certificates
|
|
|
|
|
|
|
|
|
|
# Set environment variables
|
|
|
|
|
ENV RUST_BACKTRACE=1
|
|
|
|
|
ENV RUST_LOG=warn
|
2023-09-28 02:08:48 +03:00
|
|
|
|
2024-11-13 11:32:50 +03:00
|
|
|
# Copy the build artifact from the build stage
|
2024-08-30 21:05:51 +03:00
|
|
|
COPY --from=build /quoter/target/release/quoter .
|
2023-10-11 23:06:27 +03:00
|
|
|
|
2025-08-02 00:18:09 +03:00
|
|
|
EXPOSE 8080
|
|
|
|
|
|
2024-11-13 11:42:39 +03:00
|
|
|
# Create healthcheck
|
|
|
|
|
HEALTHCHECK --interval=30s --timeout=3s \
|
|
|
|
|
CMD curl -f http://localhost:8080/ || exit 1
|
|
|
|
|
|
2024-11-13 11:32:50 +03:00
|
|
|
# Set the startup command
|
2024-11-13 09:08:34 +03:00
|
|
|
CMD ["./quoter"]
|