Skip to content

Commit

Permalink
refactor: consolidate Dockerfile into a single multistage build and u…
Browse files Browse the repository at this point in the history
…pdate cross-compilation settings
  • Loading branch information
lmcdonough committed Oct 28, 2024
1 parent 524da79 commit 575cdc8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 134 deletions.
65 changes: 32 additions & 33 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
# Use the official Rust image as the base image
FROM rust:latest
# Stage 1: Build Stage
FROM rust:latest AS builder

# Set the working directory inside the container
WORKDIR /app
# Set target for cross-compilation
ENV CARGO_BUILD_TARGET=aarch64-unknown-linux-gnu

# Set the target directory explicitly for Rust outputs
ENV CARGO_TARGET_DIR=/target
WORKDIR /app

# Copy the Cargo.toml and Cargo.lock files
# Copy the root Cargo.toml and Cargo.lock to resolve dependencies
COPY Cargo.toml Cargo.lock ./

# Copy the Cargo.toml files from each crate
# Copy each crate's Cargo.toml for efficient caching
COPY web/Cargo.toml web/Cargo.toml
COPY service/Cargo.toml service/Cargo.toml
COPY entity_api/Cargo.toml entity_api/Cargo.toml
COPY entity/Cargo.toml entity/Cargo.toml
COPY migration/Cargo.toml migration/Cargo.toml

# Copy the source code
# Copy the full source code
COPY . .

# Install necessary system dependencies for runtime
RUN apt-get update && apt-get install -y \
libssl3 \
ca-certificates \
libpq5 \
&& rm -rf /var/lib/apt/lists/*
# Install the necessary target for cross-compilation
RUN rustup target add $CARGO_BUILD_TARGET

# Build the application in release mode
RUN cargo build --release --target $CARGO_BUILD_TARGET

# Ensure rebuild_db.sh is executable
RUN chmod +x ./scripts/rebuild_db.sh
# Stage 2: Runtime Stage
FROM debian:buster-slim

# Args for username, UID, and GID for the app user
ARG USERNAME=${USERNAME:-appuser}
ARG USER_UID=${USER_UID:-1000}
ARG USER_GID=${USER_GID:-1000}
# Install bash if it's not included by default in the slim image
RUN apt-get update && apt-get install -y bash && rm -rf /var/lib/apt/lists/*

# Create appuser
RUN useradd -m appuser

# Set work directory
WORKDIR /app

# Create the app user and set appropriate permissions
RUN groupadd -g ${USER_GID} ${USERNAME} && \
useradd -u ${USER_UID} -g ${USER_GID} -m ${USERNAME} && \
chown -R ${USERNAME}:${USERNAME} /app
# Copy binaries from builder
COPY --from=builder /app/target/$CARGO_BUILD_TARGET/release/migration /app/migration
COPY --from=builder /app/target/$CARGO_BUILD_TARGET/release/seed_db /app/seed_db
COPY --from=builder /app/target/$CARGO_BUILD_TARGET/release/refactor_platform_rs /app/refactor_platform_rs

# Switch to the app user
USER ${USERNAME}
# Set permissions
RUN chown -R appuser:appuser /app && chmod +x /app/*

# Expose container ports to the bridge network
EXPOSE ${SERVICE_PORT:-4001}
EXPOSE ${ENTITY_API_PORT:-4002}
EXPOSE ${WEB_PORT:-4000}
USER appuser

# Set ENTRYPOINT to launch an interactive Bash shell for debugging or further work
ENTRYPOINT ["bash"]
# Default to an interactive bash shell
CMD ["bash"]
101 changes: 0 additions & 101 deletions Dockerfile.multistage

This file was deleted.

0 comments on commit 575cdc8

Please sign in to comment.