Files
quoter/Dockerfile

88 lines
2.1 KiB
Docker
Raw Normal View History

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-01 22:52:33 +03:00
# Create temporary swap file to help with memory-intensive compilation
RUN dd if=/dev/zero of=/tmp/swapfile bs=1M count=1024 && \
chmod 600 /tmp/swapfile && \
mkswap /tmp/swapfile && \
swapon /tmp/swapfile || true
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-01 22:52:33 +03:00
# Cache dependencies with memory optimization
2023-09-28 02:08:48 +03:00
ENV CARGO_NET_GIT_FETCH_WITH_CLI=true
2025-09-01 22:52:33 +03:00
# Build dependencies only
RUN cargo build --release 2>&1 | head -100 && \
# Force cleanup of intermediate files to free memory
cargo clean -p quoter && \
# Keep only the dependency artifacts
find target/release/deps -name "quoter*" -delete
2024-11-13 09:08:34 +03:00
# Remove the default source file created by cargo new
2023-09-28 02:08:48 +03:00
RUN rm src/*.rs
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 && \
# 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"]