# Copyright 2024 RustFS Team # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Multi-stage Alpine build from source FROM rust:1.88-alpine AS builder # Install build dependencies RUN apk add --no-cache \ musl-dev \ pkgconfig \ openssl-dev \ openssl-libs-static \ curl \ unzip \ bash \ wget \ ca-certificates \ git # 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-musl.tar.gz \ && tar -xzf sccache-v0.8.1-x86_64-unknown-linux-musl.tar.gz \ && mv sccache-v0.8.1-x86_64-unknown-linux-musl/sccache /usr/local/bin/ \ && chmod +x /usr/local/bin/sccache \ && rm -rf sccache-v0.8.1-x86_64-unknown-linux-musl.tar.gz sccache-v0.8.1-x86_64-unknown-linux-musl # Set up sccache environment ENV RUSTC_WRAPPER=sccache \ SCCACHE_DIR=/tmp/sccache \ SCCACHE_CACHE_SIZE=2G # 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 WORKDIR /usr/src/rustfs # Copy cargo configuration for optimized builds COPY .docker/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 cargo build --release --target x86_64-unknown-linux-musl -j $(nproc) # Copy source code COPY . . # Build the actual application with optimizations RUN sccache --start-server 2>/dev/null || true && \ cargo build --release --target x86_64-unknown-linux-musl --bin rustfs -j $(nproc) && \ sccache --show-stats || true # Final Alpine runtime image FROM alpine:3.18 RUN apk add --no-cache \ ca-certificates \ tzdata \ bash # Create rustfs user for security RUN addgroup -g 1000 rustfs && \ adduser -D -u 1000 -G rustfs rustfs WORKDIR /app # Copy binary from builder COPY --from=builder /usr/src/rustfs/target/x86_64-unknown-linux-musl/release/rustfs /app/rustfs RUN chmod +x /app/rustfs && chown rustfs:rustfs /app/rustfs # Create data directories RUN mkdir -p /data && chown -R rustfs:rustfs /data /app # Switch to non-root user USER rustfs # Environment variables ENV RUSTFS_ACCESS_KEY=rustfsadmin \ RUSTFS_SECRET_KEY=rustfsadmin \ RUSTFS_ADDRESS=":9000" \ RUSTFS_CONSOLE_ENABLE=true \ RUSTFS_VOLUMES=/data \ RUST_LOG=warn EXPOSE 9000 # 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 CMD ["/app/rustfs"]