mirror of
https://github.com/rustfs/rustfs.git
synced 2026-01-17 01:30:33 +00:00
156 lines
5.0 KiB
Docker
156 lines
5.0 KiB
Docker
# 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.10.0/sccache-dist-v0.10.0-x86_64-unknown-linux-musl.tar.gz \
|
|
&& tar -xzf sccache-dist-v0.10.0-x86_64-unknown-linux-musl.tar.gz \
|
|
&& mv sccache-dist-v0.10.0-x86_64-unknown-linux-musl/sccache-dist /usr/local/bin/sccache \
|
|
&& chmod +x /usr/local/bin/sccache \
|
|
&& rm -rf sccache-dist-v0.10.0-x86_64-unknown-linux-musl.tar.gz sccache-dist-v0.10.0-x86_64-unknown-linux-musl
|
|
|
|
# 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.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
|
|
|
|
|
|
# Volume for data
|
|
VOLUME ["/data"]
|
|
|
|
# Set default command
|
|
CMD ["/app/rustfs"]
|