diff --git a/.env.docker b/.env.docker new file mode 100644 index 0000000..7d5d23a --- /dev/null +++ b/.env.docker @@ -0,0 +1,10 @@ +# Database URL for connecting from the API container to the Postgres container +# The hostname 'postgres' matches the service name in docker-compose.yml +DATABASE_URL=postgres://user:password@postgres:5432/rustforms_db + +# We can also move our other secrets here to keep them all in one place for Docker +FORM_SECRET=your-super-secret-unguessable-token +RECIPIENT_EMAIL=your-email@example.com +SMTP_HOST=smtp.example.com +SMTP_USERNAME=your-smtp-username +SMTP_PASSWORD=your-smtp-password \ No newline at end of file diff --git a/apps/api/Dockerfile b/apps/api/Dockerfile new file mode 100644 index 0000000..0d64923 --- /dev/null +++ b/apps/api/Dockerfile @@ -0,0 +1,29 @@ +# ---- Stage 1: The Builder ---- +# Use the official Rust image as a build environment. +FROM rust:1.79-slim as builder + +# Set the working directory +WORKDIR /usr/src/rustforms + +# Copy the entire monorepo context to leverage Nx caching +COPY . . + +# Build the API in release mode using Nx's runner +# This creates an optimized binary. +RUN npx nx run api:build --skip-nx-cache + +# ---- Stage 2: The Final Image ---- +# Use a minimal, secure base image for the final container. +FROM debian:12-slim + +# Set the working directory +WORKDIR /usr/src/rustforms + +# Copy the compiled binary from the 'builder' stage. +COPY --from=builder /usr/src/rustforms/dist/apps/api/api . + +# Expose the port the app runs on. +EXPOSE 3001 + +# The command to run when the container starts. +CMD ["./api"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f345eb5 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,38 @@ +# File: docker-compose.yml + +version: '3.8' + +services: + # 1. The PostgreSQL Database Service + postgres: + image: postgres:16-alpine + container_name: rustforms-db + ports: + - "5432:5432" # Expose the database port to your local machine + environment: + # These are the credentials your Rust app will use to connect. + # You can change these, but make sure they match your .env file. + POSTGRES_USER: user + POSTGRES_PASSWORD: password + POSTGRES_DB: rustforms_db + volumes: + - rustforms-data:/var/lib/postgresql/data # This saves your DB data even if the container stops + restart: unless-stopped + + # 2. The Rust API Service (we will create its Dockerfile next) + api: + container_name: rustforms-api + build: + context: . + dockerfile: apps/api/Dockerfile + ports: + - "3001:3001" # Expose the API port + depends_on: + - postgres # Tells Docker to start the database before starting the API + environment: + # We will create this .env.docker file next + - .env.docker + restart: unless-stopped + +volumes: + rustforms-data: # Defines the persistent volume for the database \ No newline at end of file