diff --git a/Dockerfile b/Dockerfile index 392785a..9e3e2f8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 @@ -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