Skip to content

Commit

Permalink
refactor: enhance Dockerfile for improved dependency management and c…
Browse files Browse the repository at this point in the history
…ross-compilation
  • Loading branch information
lmcdonough committed Nov 1, 2024
1 parent 515663b commit 6a2204e
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM rust:latest

# 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/*
RUN apt-get update && apt-get install -y bash && rm -rf /var/lib/apt/lists/*

# Set target for cross-compilation
ENV CARGO_BUILD_TARGET=x86_64-unknown-linux-gnu
Expand All @@ -20,34 +20,44 @@ COPY entity_api/Cargo.toml entity_api/Cargo.toml
COPY entity/Cargo.toml entity/Cargo.toml
COPY migration/Cargo.toml migration/Cargo.toml

# Create a dummy main.rs file to build dependencies
# Create a dummy main.rs file for the root crate to build dependencies
RUN mkdir src && echo "fn main() {}" > src/main.rs

# Build the dependencies and cache them then remove the dummy main.rs
RUN cargo build --release && rm -f target/release/deps/refactor_platform_rs*

# Copy the source code to the container
# Create dummy main.rs files for each sub-crate to build dependencies
RUN mkdir -p entity/src && echo "fn main() {}" > entity/src/main.rs && \
mkdir -p entity_api/src && echo "fn main() {}" > entity_api/src/main.rs && \
mkdir -p service/src && echo "fn main() {}" > service/src/main.rs && \
mkdir -p web/src && echo "fn main() {}" > web/src/main.rs && \
mkdir -p migration/src && echo "fn main() {}" > migration/src/main.rs

# Build the dependencies and cache them, then remove the dummy main.rs files
RUN cargo build --release && \
rm -f src/main.rs \
entity/src/main.rs \
entity_api/src/main.rs \
service/src/main.rs \
web/src/main.rs \
migration/src/main.rs

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

# 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

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

# Expose the port the web server will listen on
EXPOSE 4000

# Create appuser
# Create a non-root user for running the application
RUN useradd -m appuser

# Set permissions
RUN chown -R appuser:appuser /app && chmod +x /app/*
# Set ownership and permissions for the application directory
RUN chown -R appuser:appuser /usr/src/app && chmod +x /usr/src/app/target/release/*

# Switch to the appuser
# Switch to the non-root user
USER appuser

# Default to an interactive bash shell when the container starts if no command is specified
Expand Down

0 comments on commit 6a2204e

Please sign in to comment.