# Multi-stage Dockerfile for RustFS # Supports cross-compilation for amd64 and arm64 architectures ARG TARGETPLATFORM ARG BUILDPLATFORM # Build stage FROM --platform=$BUILDPLATFORM rust:1.88-bookworm AS builder # Install required build dependencies RUN apt-get update && apt-get install -y \ wget \ git \ curl \ unzip \ gcc \ pkg-config \ libssl-dev \ lld \ && rm -rf /var/lib/apt/lists/* # Install sccache for Rust compilation caching RUN wget https://github.com/mozilla/sccache/releases/download/v0.8.1/sccache-v0.8.1-x86_64-unknown-linux-gnu.tar.gz \ && tar -xzf sccache-v0.8.1-x86_64-unknown-linux-gnu.tar.gz \ && mv sccache-v0.8.1-x86_64-unknown-linux-gnu/sccache /usr/local/bin/ \ && chmod +x /usr/local/bin/sccache \ && rm -rf sccache-v0.8.1-x86_64-unknown-linux-gnu.tar.gz sccache-v0.8.1-x86_64-unknown-linux-gnu # Set up sccache environment ENV RUSTC_WRAPPER=sccache \ SCCACHE_DIR=/tmp/sccache \ SCCACHE_CACHE_SIZE=2G # Install cross-compilation tools for ARM64 RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \ apt-get update && \ apt-get install -y gcc-aarch64-linux-gnu && \ rm -rf /var/lib/apt/lists/*; \ fi # Install protoc RUN wget https://github.com/protocolbuffers/protobuf/releases/download/v31.1/protoc-31.1-linux-x86_64.zip \ && unzip protoc-31.1-linux-x86_64.zip -d protoc3 \ && mv protoc3/bin/* /usr/local/bin/ && chmod +x /usr/local/bin/protoc \ && mv protoc3/include/* /usr/local/include/ && rm -rf protoc-31.1-linux-x86_64.zip protoc3 # Install flatc RUN wget https://github.com/google/flatbuffers/releases/download/v25.2.10/Linux.flatc.binary.g++-13.zip \ && unzip Linux.flatc.binary.g++-13.zip \ && mv flatc /usr/local/bin/ && chmod +x /usr/local/bin/flatc && rm -rf Linux.flatc.binary.g++-13.zip # Set up Rust targets based on platform RUN case "$TARGETPLATFORM" in \ "linux/amd64") rustup target add x86_64-unknown-linux-gnu ;; \ "linux/arm64") rustup target add aarch64-unknown-linux-gnu ;; \ *) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \ esac # Set up environment for cross-compilation ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc ENV CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc ENV CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++ WORKDIR /usr/src/rustfs # Copy cargo configuration for optimized builds COPY cargo.config.toml ./.cargo/config.toml # Copy Cargo files for dependency caching COPY Cargo.toml Cargo.lock ./ COPY */Cargo.toml ./*/ # Create dummy main.rs files for dependency compilation RUN find . -name "Cargo.toml" -not -path "./Cargo.toml" | \ xargs -I {} dirname {} | \ xargs -I {} sh -c 'mkdir -p {}/src && echo "fn main() {}" > {}/src/main.rs' # Configure cargo for optimized builds ENV CARGO_NET_GIT_FETCH_WITH_CLI=true \ CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse \ CARGO_INCREMENTAL=0 \ CARGO_PROFILE_RELEASE_DEBUG=false \ CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO=off \ CARGO_PROFILE_RELEASE_STRIP=symbols # Build dependencies only (cache layer) with optimizations RUN sccache --start-server 2>/dev/null || true && \ case "$TARGETPLATFORM" in \ "linux/amd64") cargo build --release --target x86_64-unknown-linux-gnu -j $(nproc) ;; \ "linux/arm64") cargo build --release --target aarch64-unknown-linux-gnu -j $(nproc) ;; \ esac # Copy source code COPY . . # Generate protobuf code RUN cargo run --bin gproto # Build the actual application with optimizations RUN sccache --start-server 2>/dev/null || true && \ case "$TARGETPLATFORM" in \ "linux/amd64") \ cargo build --release --target x86_64-unknown-linux-gnu --bin rustfs -j $(nproc) && \ cp target/x86_64-unknown-linux-gnu/release/rustfs /usr/local/bin/rustfs \ ;; \ "linux/arm64") \ cargo build --release --target aarch64-unknown-linux-gnu --bin rustfs -j $(nproc) && \ cp target/aarch64-unknown-linux-gnu/release/rustfs /usr/local/bin/rustfs \ ;; \ esac && \ sccache --show-stats || true # Runtime stage - Ubuntu minimal for better compatibility FROM ubuntu:22.04 # Install runtime dependencies RUN apt-get update && apt-get install -y \ ca-certificates \ tzdata \ wget \ && rm -rf /var/lib/apt/lists/* # Create rustfs user and group RUN groupadd -g 1000 rustfs && \ useradd -d /app -g rustfs -u 1000 -s /bin/bash rustfs WORKDIR /app # Create data directories RUN mkdir -p /data/rustfs{0,1,2,3} && \ chown -R rustfs:rustfs /data /app # Copy binary from builder stage COPY --from=builder /usr/local/bin/rustfs /app/rustfs RUN chmod +x /app/rustfs && chown rustfs:rustfs /app/rustfs # Switch to non-root user USER rustfs # Expose ports EXPOSE 9000 # Environment variables ENV RUSTFS_ACCESS_KEY=rustfsadmin \ RUSTFS_SECRET_KEY=rustfsadmin \ RUSTFS_ADDRESS=":9000" \ RUSTFS_CONSOLE_ENABLE=true \ RUSTFS_VOLUMES=/data \ RUST_LOG=warn # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:9000/health || exit 1 # Volume for data VOLUME ["/data"] # Set default command CMD ["/app/rustfs"]