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
This commit is contained in:
zhenyi
2026-06-11 13:58:20 +08:00
parent 0dbac480ae
commit 1b300865d9
2 changed files with 61 additions and 0 deletions
+22
View File
@@ -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
+39
View File
@@ -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"]