tbm-git-repo-service/Dockerfile
Pakin cb98b9786e feat: build locally on dev machine
- clone from `server-m2`

Signed-off-by: Pakin <pakin.t@forth.co.th>
2026-07-06 15:11:45 +07:00

135 lines
No EOL
5.1 KiB
Docker

# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM lukemathwalker/cargo-chef:latest-rust-slim-bookworm AS chef
WORKDIR /app
# -----------------------------------------------------------------------
# Stage 1: Prepare the recipe
# -----------------------------------------------------------------------
FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
# -----------------------------------------------------------------------
# Stage 2: Build the dependencies & application
# -----------------------------------------------------------------------
FROM chef AS builder
# Capture Docker's target platform variables
ARG TARGETPLATFORM
ARG TARGETARCH
ARG TARGETVARIANT
# Install host tools needed for compilation (including cmake and clang for aws-lc-sys)
RUN apt-get update && apt-get install -y \
clang \
llvm \
cmake \
make \
pkg-config \
perl \
libssl-dev
# Enable multiarch support so we can download foreign architecture .so and .h files
RUN dpkg --add-architecture amd64 && \
dpkg --add-architecture arm64
# Setup target-specific environment variables manually based on target architecture
RUN apt-get update && \
if [ "$TARGETARCH" = "amd64" ]; then \
apt-get install -y gcc-x86-64-linux-gnu g++-x86-64-linux-gnu libssl-dev; \
echo "TARGET_TRIPLE=x86_64-unknown-linux-gnu" >> /env_config; \
echo "CC_x86_64_unknown_linux_gnu=/usr/bin/x86_64-linux-gnu-gcc" >> /env_config; \
echo "CXX_x86_64_unknown_linux_gnu=/usr/bin/x86_64-linux-gnu-g++" >> /env_config; \
echo "CC=/usr/bin/x86_64-linux-gnu-gcc" >> /env_config; \
echo "CXX=/usr/bin/x86_64-linux-gnu-g++" >> /env_config; \
echo "CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=/usr/bin/x86_64-linux-gnu-gcc" >> /env_config; \
echo "OPENSSL_DIR=/usr" >> /env_config; \
if [ "$TARGETVARIANT" = "v3" ]; then \
echo "export RUSTFLAGS='-C target-cpu=x86-64-v3'" >> /env_config; \
fi \
elif [ "$TARGETARCH" = "arm64" ]; then \
apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu libssl-dev; \
echo "TARGET_TRIPLE=aarch64-unknown-linux-gnu" >> /env_config; \
echo "CC_aarch64_unknown_linux_gnu=/usr/bin/aarch64-linux-gnu-gcc" >> /env_config; \
echo "CXX_aarch64_unknown_linux_gnu=/usr/bin/aarch64-linux-gnu-g++" >> /env_config; \
echo "CC=/usr/bin/aarch64-linux-gnu-gcc" >> /env_config; \
echo "CXX=/usr/bin/aarch64-linux-gnu-g++" >> /env_config; \
echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=/usr/bin/aarch64-linux-gnu-gcc" >> /env_config; \
echo "OPENSSL_DIR=/usr" >> /env_config; \
fi
# Force openssl-sys to download, compile, and statically link OpenSSL safely
ENV OPENSSL_STATIC=1
ENV OPENSSL_VENDED=1
# Tell cargo to allow cross-compiling build scripts
ENV PKG_CONFIG_ALLOW_CROSS=1
# Load the environment configurations and download Rust target targets
RUN . /env_config && \
rustup target add "$TARGET_TRIPLE"
# Tell aws-lc-sys exactly how to build via CMake
ENV AWS_LC_SYS_CMAKE_BUILDER=1
ENV AWS_LC_SYS_PREBUILT_NASM=1
COPY .cargo /app/.cargo
# Cache and build only the dependencies (the chef recipe)
COPY --from=planner /app/recipe.json recipe.json
RUN . /env_config && \
cargo chef cook --release --target "$TARGET_TRIPLE" --recipe-path recipe.json
# Copy actual source code
COPY . .
# Build the main application using cached dependencies
RUN . /env_config && \
cargo build --release --target "$TARGET_TRIPLE" && \
cp target/${TARGET_TRIPLE}/release/tbm-git-repo-service /tbm-git-repo-service
# -----------------------------------------------------------------------
# Stage 3: Minimal Runtime
# -----------------------------------------------------------------------
# We dynamically anchor the platform to a base architecture to stop Docker
# from panicking over micro-variants like v3 during runtime container setup.
FROM --platform=$TARGETPLATFORM debian:bookworm-slim AS runtime-base
# Trick Buildx: force the runner to resolve back to basic broad platforms
# so it can execute native container processes like apt-get without errors.
FROM --platform=linux/amd64 debian:bookworm-slim AS runtime-amd64
FROM --platform=linux/arm64 debian:bookworm-slim AS runtime-arm64
# Select the clean runtime environment matching your target layout
FROM runtime-$TARGETARCH AS final-runtime
WORKDIR /app
ENV TZ=Asia/Bangkok
# Install runtime dependencies if needed (like ca-certificates or openssl)
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates \
libssl3 \
zlib1g \
curl \
pkg-config \
ca-certificates \
openssh-client \
tzdata && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
&& rm -rf /var/lib/apt/lists/*
COPY --from=builder /tbm-git-repo-service /app/tbm-git-repo-service
COPY --from=builder /app/.tbcfg /app/.tbcfg
RUN mkdir -p /root/.ssh && \
ssh-keyscan 192.168.10.159 >> /root/.ssh/known_hosts
# RUN cp /usr/src/app/target/release/tbm-git-repo-service /usr/src/app/
# RUN cp /usr/src/app/.tbcfg /usr/src/app/
# RUN rm -rf /usr/src/app/target
EXPOSE 36583
CMD ["./tbm-git-repo-service"]