Files
quoter/Dockerfile
2025-09-22 09:32:45 +03:00

100 lines
2.7 KiB
Docker

# Build stage with memory optimization
FROM rust:slim-bookworm AS build
# Create larger temporary swap file for extreme memory optimization
RUN dd if=/dev/zero of=/tmp/swapfile bs=1M count=4096 && \
chmod 600 /tmp/swapfile && \
mkswap /tmp/swapfile && \
swapon /tmp/swapfile || true && \
# Set swappiness to use swap more aggressively
echo 80 > /proc/sys/vm/swappiness || true
# Install build dependencies
RUN apt-get update && \
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
RUN USER=root cargo new --bin quoter
WORKDIR /quoter
# Copy manifests and cargo config
COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml
COPY ./.cargo ./.cargo
# Extreme memory optimization environment variables
ENV CARGO_NET_GIT_FETCH_WITH_CLI=true
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
# Additional memory optimization for Redis connection pool compilation
ENV CARGO_BUILD_JOBS=1
ENV RUSTFLAGS="-C opt-level=s -C target-cpu=generic"
# Build dependencies only with extreme memory conservation
RUN cargo build --release && \
# Force cleanup of intermediate files to free memory
cargo clean -p quoter && \
# Keep only the dependency artifacts (suppressing error if dir doesn't exist)
find target/release/deps -name "quoter*" -delete 2>/dev/null || true
# Remove the default source file created by cargo new (but keep lib.rs)
RUN rm src/main.rs || true
# Copy source code
COPY ./src ./src
# Build for release with aggressive memory cleanup
RUN cargo build --release && \
# Verify the binary was created
ls -la target/release/quoter && \
# Immediately strip the binary to save space
strip target/release/quoter || true && \
# Clean up swap file
swapoff /tmp/swapfile || true && \
rm -f /tmp/swapfile
# Final stage
FROM debian:bookworm-slim
# Install runtime dependencies and CA certificates
RUN apt-get update && \
apt-get install -y --no-install-recommends \
ca-certificates \
libssl3 \
libtiff6 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& update-ca-certificates
# Set environment variables
ENV RUST_BACKTRACE=1
ENV RUST_LOG=warn
# Copy the build artifact from the build stage
COPY --from=build /quoter/target/release/quoter .
EXPOSE 8080
# Create healthcheck
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost:8080/ || exit 1
# Set the startup command
CMD ["./quoter"]