feat: update workspace members, add backend service to docker-compose, and create Dockerfile for static linking

This commit is contained in:
Mika 2025-04-09 21:35:52 +02:00
parent ad45a34cf3
commit 0ca4c56b0d
6 changed files with 77 additions and 32 deletions

View file

30
Cargo.lock generated
View file

@ -1045,16 +1045,6 @@ dependencies = [
"cipher", "cipher",
] ]
[[package]]
name = "ctrlc"
version = "3.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90eeab0aa92f3f9b4e87f258c72b139c207d251f9cbc1080a0086b86a8870dd3"
dependencies = [
"nix",
"windows-sys 0.59.0",
]
[[package]] [[package]]
name = "darling" name = "darling"
version = "0.20.10" version = "0.20.10"
@ -2088,18 +2078,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e94e1e6445d314f972ff7395df2de295fe51b71821694f0b0e1e79c4f12c8577" checksum = "e94e1e6445d314f972ff7395df2de295fe51b71821694f0b0e1e79c4f12c8577"
[[package]]
name = "nix"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [
"bitflags",
"cfg-if",
"cfg_aliases",
"libc",
]
[[package]] [[package]]
name = "num-bigint" name = "num-bigint"
version = "0.4.6" version = "0.4.6"
@ -4178,14 +4156,6 @@ dependencies = [
"tap", "tap",
] ]
[[package]]
name = "xtask"
version = "0.1.0"
dependencies = [
"clap",
"ctrlc",
]
[[package]] [[package]]
name = "yansi" name = "yansi"
version = "1.0.1" version = "1.0.1"

View file

@ -1,5 +1,5 @@
[workspace] [workspace]
members = ["crates/backend", "crates/migration", "crates/xtask"] members = ["crates/backend", "crates/migration"]
resolver = "3" resolver = "3"
[workspace.package] [workspace.package]

52
backend.Dockerfile Normal file
View file

@ -0,0 +1,52 @@
# ---- Stage 1: Build (Static Linking) ----
FROM rust:latest as builder
WORKDIR /usr/src/app
# Install the musl target and musl-tools
RUN rustup target add x86_64-unknown-linux-musl
RUN apt-get update && apt-get install -y musl-tools
# Create a .cargo directory and configure for static linking with musl
RUN mkdir -p .cargo
RUN echo '[target.x86_64-unknown-linux-musl]' > .cargo/config.toml
RUN echo 'linker = "rust-lld"' >> .cargo/config.toml
# Copy configuration and lock files needed to resolve dependencies
COPY Cargo.toml ./Cargo.toml
COPY Cargo.lock ./Cargo.lock
COPY crates/backend/Cargo.toml crates/backend/Cargo.toml
COPY crates/migration/Cargo.toml crates/migration/Cargo.toml
# Fetch dependencies based on the lock file.
RUN cargo fetch
# Copy the actual source code for all workspace members
COPY crates/backend/ crates/backend/
COPY crates/migration/ crates/migration/
# Build the release binary for the musl target
RUN cargo build --release --target x86_64-unknown-linux-musl
# Debug: List the built binaries to verify what we have
RUN ls -la /usr/src/app/target/x86_64-unknown-linux-musl/release/
# ---- Stage 2: Runtime ----
FROM alpine:latest
# Install minimal runtime dependencies (ca-certificates is often needed)
RUN apk add --no-cache ca-certificates
WORKDIR /app
# Copy only the statically linked compiled binary from the builder stage
COPY --from=builder /usr/src/app/target/x86_64-unknown-linux-musl/release/backend /app/backend
# Verify the binary exists and is executable
RUN ls -la /app && chmod +x /app/backend
# Expose the port the application listens on
EXPOSE 8080
# Set the command to run the application
CMD ["/app/backend"]

View file

@ -32,6 +32,9 @@ dotenvy = "0.15"
[dev-dependencies] [dev-dependencies]
temp-env = "*" temp-env = "*"
[features] [features]
serve = [] serve = []
[[bin]]
name = "backend"
path = "src/main.rs"

View file

@ -52,6 +52,26 @@ services:
interval: 30s interval: 30s
retries: 3 retries: 3
backend:
build:
context: .
dockerfile: ./backend.Dockerfile
image: peer-group-grading:latest
restart: unless-stopped
environment:
DB_NAME: ${DB_NAME}
DB_USER: ${DB_USER}
DB_PASSWORD: ${DB_PASSWORD}
DB_HOST: db
DB_PORT: 5432
REDIS_HOST: redis
REDIS_PORT: 6379
ports:
- "8080:8080"
depends_on:
- db
- redis
volumes: volumes:
postgres_data: postgres_data:
redis: redis: