From 1b300865d9cc5407eb9e7dda9f6f8e6e0e7f2c91 Mon Sep 17 00:00:00 2001 From: zhenyi <434836402@qq.com> Date: Thu, 11 Jun 2026 13:58:20 +0800 Subject: [PATCH] build(docker): add Dockerfile and .dockerignore for containerization - Create multi-stage Dockerfile with cargo-chef optimization - Add .dockerignore to exclude unnecessary files from build context - Include protobuf compiler and development dependencies in builder stage - Set up proper user permissions and health check - Configure release build with strip for smaller image size - Add curl dependency for health check functionality --- .dockerignore | 22 ++++++++++++++++++++++ Dockerfile | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..63f1b7a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,22 @@ +# Build artifacts +target/ + +# IDE +.idea/ +.codegraph/ + +# Git +.git/ +.gitignore + +# Documentation (not needed at build time) +*.md +!rpc.md + +# Environment files +.env +.env.local + +# OS files +.DS_Store +Thumbs.db diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cc820f7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +FROM rust:1.96-bookworm AS chef +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + protobuf-compiler libprotobuf-dev mold clang && \ + rm -rf /var/lib/apt/lists/* +RUN cargo install cargo-chef +WORKDIR /app + +FROM chef AS planner +COPY . . +RUN cargo chef prepare --recipe-path recipe.json + +FROM chef AS builder +COPY --from=planner /app/recipe.json recipe.json +RUN cargo chef cook --release --recipe-path recipe.json +COPY . . +RUN cargo build --release --bin imks && \ + strip target/release/imks + +FROM ubuntu:26.04 + +RUN apt-get update && \ + apt-get install -y --no-install-recommends ca-certificates curl && \ + rm -rf /var/lib/apt/lists/* + +COPY --from=builder /app/target/release/imks /usr/local/bin/imks +COPY --from=builder /app/migrate/ /app/migrate/ + +WORKDIR /app + +RUN useradd -m -u 1000 imks && chown -R imks:imks /app +USER imks + +EXPOSE 3000 + +HEALTHCHECK --interval=15s --timeout=3s --start-period=10s --retries=3 \ + CMD curl -sf http://localhost:3000/health || exit 1 + +ENTRYPOINT ["imks"]