# ============================================================================ # FOXHUNT HFT TRADING SYSTEM - PRODUCTION MULTI-STAGE DOCKERFILE # ============================================================================ # Optimized multi-stage build for production deployment # Supports all services: trading, ml-training, backtesting, tli # # Build Args: # SERVICE_NAME - Service to build (trading_service, ml_training_service, backtesting_service, tli) # TARGET_ARCH - Target architecture (x86_64, aarch64) # ENABLE_GPU - Enable CUDA/GPU support (true, false) # BUILD_MODE - Build mode (release, debug) # ============================================================================ ################################################################# # Stage 1: Dependencies and Build Environment ################################################################# FROM rust:1.75-slim-bookworm AS dependencies # Build arguments ARG TARGET_ARCH=x86_64 ARG ENABLE_GPU=false # Install system dependencies based on architecture and GPU support RUN apt-get update && apt-get install -y \ pkg-config \ libssl-dev \ libpq-dev \ ca-certificates \ curl \ build-essential \ cmake \ git \ clang \ llvm \ libc6-dev \ && rm -rf /var/lib/apt/lists/* # Install CUDA toolkit if GPU support is enabled RUN if [ "$ENABLE_GPU" = "true" ] && [ "$TARGET_ARCH" = "x86_64" ]; then \ wget https://developer.download.nvidia.com/compute/cuda/repos/debian11/x86_64/cuda-keyring_1.1-1_all.deb && \ dpkg -i cuda-keyring_1.1-1_all.deb && \ apt-get update && \ apt-get install -y cuda-toolkit-12-0 cuda-runtime-12-0 && \ rm -rf /var/lib/apt/lists/* && \ rm cuda-keyring_1.1-1_all.deb; \ fi # Setup Rust toolchain for target architecture RUN case "$TARGET_ARCH" in \ "x86_64") RUST_TARGET=x86_64-unknown-linux-gnu ;; \ "aarch64") RUST_TARGET=aarch64-unknown-linux-gnu ;; \ *) echo "Unsupported architecture: $TARGET_ARCH" && exit 1 ;; \ esac && \ rustup target add $RUST_TARGET && \ echo "RUST_TARGET=$RUST_TARGET" > /tmp/rust_target # Install additional cross-compilation tools if needed RUN if [ "$TARGET_ARCH" = "aarch64" ]; then \ apt-get update && apt-get install -y gcc-aarch64-linux-gnu && \ rm -rf /var/lib/apt/lists/*; \ fi ################################################################# # Stage 2: Dependency Pre-compilation ################################################################# FROM dependencies AS deps-builder WORKDIR /app # Copy workspace and dependency manifests COPY Cargo.toml Cargo.lock ./ COPY trading_engine/Cargo.toml ./trading_engine/ COPY ml/Cargo.toml ./ml/ COPY risk/Cargo.toml ./risk/ COPY data/Cargo.toml ./data/ COPY tli/Cargo.toml ./tli/ COPY backtesting/Cargo.toml ./backtesting/ COPY adaptive-strategy/Cargo.toml ./adaptive-strategy/ COPY services/trading_service/Cargo.toml ./services/trading_service/ COPY services/ml_training_service/Cargo.toml ./services/ml_training_service/ COPY services/backtesting_service/Cargo.toml ./services/backtesting_service/ # Create dummy source files for dependency pre-compilation RUN mkdir -p \ trading_engine/src \ ml/src \ risk/src \ data/src \ tli/src \ backtesting/src \ adaptive-strategy/src \ services/trading_service/src \ services/ml_training_service/src \ services/backtesting_service/src \ src # Create minimal lib.rs files RUN echo "pub fn main() {}" > trading_engine/src/lib.rs && \ echo "pub fn main() {}" > ml/src/lib.rs && \ echo "pub fn main() {}" > risk/src/lib.rs && \ echo "pub fn main() {}" > data/src/lib.rs && \ echo "pub fn main() {}" > backtesting/src/lib.rs && \ echo "pub fn main() {}" > adaptive-strategy/src/lib.rs && \ echo "fn main() {}" > tli/src/main.rs && \ echo "fn main() {}" > services/trading_service/src/main.rs && \ echo "fn main() {}" > services/ml_training_service/src/main.rs && \ echo "fn main() {}" > services/backtesting_service/src/main.rs && \ echo "fn main() {}" > src/lib.rs # Set environment variables for compilation ENV RUST_BACKTRACE=0 ENV CARGO_INCREMENTAL=0 # Build dependencies only (cached layer) RUN . /tmp/rust_target && \ if [ "$ENABLE_GPU" = "true" ]; then \ FEATURES="cuda"; \ else \ FEATURES=""; \ fi && \ cargo build --release --target="$RUST_TARGET" --features="$FEATURES" # Clean up dummy source files and their build artifacts RUN rm -rf \ trading_engine/src \ ml/src \ risk/src \ data/src \ tli/src \ backtesting/src \ adaptive-strategy/src \ services/*/src \ src \ target/*/release/deps/*trading_engine* \ target/*/release/deps/*ml* \ target/*/release/deps/*risk* \ target/*/release/deps/*data* \ target/*/release/deps/*tli* \ target/*/release/deps/*backtesting* \ target/*/release/deps/*adaptive* \ target/*/release/deps/*trading* \ target/*/release/deps/*foxhunt* ################################################################# # Stage 3: Application Build ################################################################# FROM deps-builder AS app-builder # Build arguments for service selection ARG SERVICE_NAME=trading_service ARG BUILD_MODE=release # Copy all source code COPY . . # Set build optimizations ENV RUSTFLAGS="-C target-cpu=native -C opt-level=3 -C lto=fat -C codegen-units=1 -C panic=abort -C prefer-dynamic=no" # Build the specified service RUN . /tmp/rust_target && \ if [ "$ENABLE_GPU" = "true" ]; then \ FEATURES="cuda"; \ else \ FEATURES=""; \ fi && \ case "$SERVICE_NAME" in \ "trading_service") \ cargo build --$BUILD_MODE --target="$RUST_TARGET" --features="$FEATURES" --bin trading_service ;; \ "ml_training_service") \ cargo build --$BUILD_MODE --target="$RUST_TARGET" --features="$FEATURES" --bin ml_training_service ;; \ "backtesting_service") \ cargo build --$BUILD_MODE --target="$RUST_TARGET" --features="$FEATURES" --bin backtesting_service ;; \ "tli") \ cargo build --$BUILD_MODE --target="$RUST_TARGET" --features="$FEATURES" --bin tli ;; \ *) \ echo "Unknown service: $SERVICE_NAME" && exit 1 ;; \ esac # Strip debug symbols for smaller binary RUN . /tmp/rust_target && \ strip "target/$RUST_TARGET/$BUILD_MODE/$SERVICE_NAME" 2>/dev/null || true ################################################################# # Stage 4: Runtime Base Image ################################################################# FROM debian:bookworm-slim AS runtime-base # Install runtime dependencies RUN apt-get update && apt-get install -y \ ca-certificates \ libssl3 \ libpq5 \ curl \ htop \ procps \ net-tools \ dumb-init \ && rm -rf /var/lib/apt/lists/* # Install CUDA runtime if GPU support was enabled ARG ENABLE_GPU=false RUN if [ "$ENABLE_GPU" = "true" ]; then \ apt-get update && \ apt-get install -y \ libnvidia-compute-535 \ libnvidia-ml1 \ libcudart12 \ libcublas12 \ && rm -rf /var/lib/apt/lists/*; \ fi # Create application user and directories RUN groupadd -g 1000 foxhunt && \ useradd -r -u 1000 -g foxhunt -d /app -s /bin/bash -c "Foxhunt Service" foxhunt WORKDIR /app # Create required directories with proper permissions RUN mkdir -p \ /app/config \ /app/data \ /app/models \ /app/cache \ /app/logs \ /app/certificates \ /app/tmp \ && chown -R foxhunt:foxhunt /app \ && chmod -R 755 /app ################################################################# # Stage 5: Service-Specific Runtime Images ################################################################# # Trading Service Runtime FROM runtime-base AS trading-service ARG SERVICE_NAME=trading_service ARG TARGET_ARCH=x86_64 ARG BUILD_MODE=release # Copy binary from builder COPY --from=app-builder /app/target/*/release/trading_service /app/foxhunt-trading # Copy service-specific configuration COPY --chown=foxhunt:foxhunt config/trading/ /app/config/ COPY --chown=foxhunt:foxhunt deployment/entrypoints/trading-entrypoint.sh /app/entrypoint.sh # Set binary permissions RUN chmod +x /app/foxhunt-trading /app/entrypoint.sh # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ CMD curl -f http://localhost:8080/health || exit 1 # Expose ports EXPOSE 8080 50051 9090 USER foxhunt ENTRYPOINT ["/usr/bin/dumb-init", "--", "/app/entrypoint.sh"] CMD ["/app/foxhunt-trading"] # ML Training Service Runtime FROM runtime-base AS ml-training-service ARG SERVICE_NAME=ml_training_service ARG TARGET_ARCH=x86_64 ARG BUILD_MODE=release # Copy binary from builder COPY --from=app-builder /app/target/*/release/ml_training_service /app/foxhunt-ml # Copy service-specific configuration COPY --chown=foxhunt:foxhunt config/ml/ /app/config/ COPY --chown=foxhunt:foxhunt deployment/entrypoints/ml-entrypoint.sh /app/entrypoint.sh # Set binary permissions RUN chmod +x /app/foxhunt-ml /app/entrypoint.sh # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=120s --retries=3 \ CMD curl -f http://localhost:8082/health || exit 1 # Expose ports EXPOSE 8082 50052 9091 USER foxhunt ENTRYPOINT ["/usr/bin/dumb-init", "--", "/app/entrypoint.sh"] CMD ["/app/foxhunt-ml"] # Backtesting Service Runtime FROM runtime-base AS backtesting-service ARG SERVICE_NAME=backtesting_service ARG TARGET_ARCH=x86_64 ARG BUILD_MODE=release # Copy binary from builder COPY --from=app-builder /app/target/*/release/backtesting_service /app/foxhunt-backtest # Copy service-specific configuration COPY --chown=foxhunt:foxhunt config/backtesting/ /app/config/ COPY --chown=foxhunt:foxhunt deployment/entrypoints/backtest-entrypoint.sh /app/entrypoint.sh # Set binary permissions RUN chmod +x /app/foxhunt-backtest /app/entrypoint.sh # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ CMD curl -f http://localhost:8083/health || exit 1 # Expose ports EXPOSE 8083 50053 9092 USER foxhunt ENTRYPOINT ["/usr/bin/dumb-init", "--", "/app/entrypoint.sh"] CMD ["/app/foxhunt-backtest"] # TLI (Terminal Line Interface) Runtime FROM runtime-base AS tli ARG SERVICE_NAME=tli ARG TARGET_ARCH=x86_64 ARG BUILD_MODE=release # Copy binary from builder COPY --from=app-builder /app/target/*/release/tli /app/foxhunt-tli # Copy service-specific configuration COPY --chown=foxhunt:foxhunt config/tli/ /app/config/ COPY --chown=foxhunt:foxhunt deployment/entrypoints/tli-entrypoint.sh /app/entrypoint.sh # Set binary permissions RUN chmod +x /app/foxhunt-tli /app/entrypoint.sh # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ CMD curl -f http://localhost:8081/health || exit 1 # Expose ports EXPOSE 8081 50054 9093 USER foxhunt ENTRYPOINT ["/usr/bin/dumb-init", "--", "/app/entrypoint.sh"] CMD ["/app/foxhunt-tli"] ################################################################# # Labels for Container Metadata ################################################################# LABEL maintainer="Foxhunt Development Team" LABEL version="1.0.0" LABEL description="Foxhunt HFT Trading System - Production Service" LABEL vendor="Foxhunt" LABEL org.opencontainers.image.title="Foxhunt HFT Service" LABEL org.opencontainers.image.description="High-frequency trading system service" LABEL org.opencontainers.image.version="1.0.0" LABEL org.opencontainers.image.vendor="Foxhunt" LABEL org.opencontainers.image.licenses="MIT OR Apache-2.0" LABEL org.opencontainers.image.source="https://github.com/user/foxhunt"