Browse Source

feat: dockerfile for production deployment & improve dev workflow (#34)

* feat: dockerfile for production deployment & improve dev workflow

* feat: docker build image with env

* feat: production deployment workflow with setup script
Lucas Neves Pereira 1 month ago
parent
commit
dbcb0f9390
5 changed files with 84 additions and 66 deletions
  1. 1 1
      .env.example
  2. 38 0
      Dockerfile
  3. 9 23
      Makefile
  4. 13 42
      README.md
  5. 23 0
      scripts/setup.sh

+ 1 - 1
.env.example

@@ -1,6 +1,6 @@
 # Database Configuration
 # Format: postgresql://USER:PASSWORD@HOST:PORT/DATABASE
-DATABASE_URL="postgresql://username:password@localhost:5432/workout_cool"
+DATABASE_URL=postgresql://username:password@localhost:5432/workout_cool
 
 # Authentication
 # The URL where your application is running

+ 38 - 0
Dockerfile

@@ -0,0 +1,38 @@
+FROM node:20-alpine AS base
+
+WORKDIR /app
+RUN npm install -g pnpm
+
+# Install dependencies
+FROM base AS deps
+COPY package.json pnpm-lock.yaml ./
+COPY prisma ./prisma
+RUN pnpm install --frozen-lockfile
+
+# Build the app
+FROM base AS builder
+COPY --from=deps /app/node_modules ./node_modules
+COPY --from=deps /app/prisma ./prisma
+COPY . .
+COPY .env.example .env
+RUN pnpm run build
+
+# Production image, copy only necessary files
+FROM base AS runner
+WORKDIR /app
+
+COPY --from=builder /app/public ./public
+COPY --from=builder /app/.next ./.next
+COPY --from=builder /app/node_modules ./node_modules
+COPY --from=builder /app/package.json ./package.json
+COPY --from=builder /app/prisma ./prisma
+
+COPY scripts/setup.sh /app/setup.sh
+RUN chmod +x /app/setup.sh
+
+ENTRYPOINT ["/app/setup.sh"]
+
+EXPOSE 3000
+ENV PORT=3000
+
+CMD ["pnpm", "start"]

+ 9 - 23
Makefile

@@ -1,22 +1,15 @@
-.PHONY: init dev up down db-reset db-migrate db-generate db-seed help
+.PHONY: dev up down db-migrate db-generate db-seed help
 
 help:
 	@echo "🚀 Workout Cool Development Commands"
 	@echo ""
-	@echo "📦 Database Management:"
+	@echo "  dev         - Start full dev environment (DB, migrate, seed, Next.js dev server)"
 	@echo "  up          - Start PostgreSQL database using Docker Compose"
 	@echo "  down        - Stop all Docker Compose services"
-	@echo "  db-migrate  - Run Prisma migrations to update database schema"
-	@echo "  db-generate - Generate Prisma client for type-safe database access"
-	@echo "  db-reset    - Reset database (⚠️ Destructive! Drops all data)"
-	@echo "  db-seed     - Seed database with sample data"
+	@echo "  db-migrate  - Run Prisma migrations"
+	@echo "  db-generate - Generate Prisma client"
+	@echo "  db-seed     - Seed database"
 	@echo ""
-	@echo "🛠️  Development:"
-	@echo "  dev         - Start Next.js development server"
-	@echo "  init        - Full setup: start DB, run migrations, seed data, and start dev server"
-	@echo ""
-	@echo "Usage: make <target>"
-	@echo "Example: make init"
 
 # Start Postgres with Docker Compose
 up:
@@ -34,17 +27,10 @@ db-migrate:
 db-generate:
 	npx prisma generate
 
-# Reset database (⚠️ destructive!)
-db-reset:
-	npx prisma migrate reset --force
-
-# Seed database with sample data
+# Seed database
 db-seed:
 	pnpm run import:exercises-full ./data/sample-exercises.csv
 
-# Start the dev server
-dev:
-	pnpm dev
-
-# Initialize dev environment (start DB, run migration, seed data, start dev server)
-init: up db-migrate db-seed dev
+# Start the dev server (with DB, migrate, seed)
+dev: up db-migrate db-seed
+	pnpm dev

+ 13 - 42
README.md

@@ -90,11 +90,9 @@ management._
 
 ### Prerequisites
 
-- Node.js 18+
-- Either:
-  - Docker
-  - OR PostgreSQL external database
-- pnpm (recommended) or npm
+- [Node.js](https://nodejs.org/) (v18+)
+- [pnpm](https://pnpm.io/) (v8+)
+- [Docker](https://www.docker.com/)
 
 ### Installation
 
@@ -111,48 +109,20 @@ management._
    pnpm install
    ```
 
-3. **Set up environment variables**
+3. **Copy environment variables**
 
    ```bash
    cp .env.example .env
    ```
 
-   Fill in your database URL and other required environment variables:
+4. **Start everything for development:**
 
-   ```env
-   DATABASE_URL="postgresql://username:password@localhost:5432/workout_cool"
-   BETTER_AUTH_SECRET="your-secret-key"
-   # ... other variables
+   ```sh
+   make dev
    ```
 
-4. **Set up the database**
-
-   #### Option 1: Using Docker
-
-   The project provides a convenient `make` command that handles everything:
-
-   ```bash
-   make init
-   ```
-
-   This single command will:
-
-   - Start the PostgreSQL database using Docker
-   - Run database migrations
-   - Start the development server
-
-   #### Option 2: Manual PostgreSQL Setup
-
-   If you prefer to use your own PostgreSQL installation:
-
-   ```bash
-   # Run migrations
-   npx prisma migrate deploy
-   npx prisma generate
-
-   # Start the development server
-   pnpm dev
-   ```
+   - This will start the database in Docker, run migrations, seed the DB, and start the Next.js dev server.
+   - To stop services run `make down`
 
 5. **Open your browser** Navigate to [http://localhost:3000](http://localhost:3000)
 
@@ -267,14 +237,14 @@ We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) f
 
 ## Deployment
 
-### Using Docker (Not ready yet : todo)
+### Using Docker
 
 ```bash
 # Build the Docker image
-docker build -t workout-cool .
+docker build -t yourusername/workout-cool .
 
 # Run the container
-docker run -p 3000:3000 workout-cool
+docker run -p 3000:3000 --env-file .env.production yourusername/workout-cool
 ```
 
 ### Manual Deployment
@@ -347,3 +317,4 @@ Appear in the README and on the website as supporter by donating:
   Your support helps cover hosting costs, exercise database updates, and continuous improvement.<br>
   Thank you for keeping <strong>workout.cool</strong> alive and evolving 💪
 </p>
+````

+ 23 - 0
scripts/setup.sh

@@ -0,0 +1,23 @@
+#!/bin/sh
+
+echo "Running Prisma migrations..."
+npx prisma migrate deploy
+
+echo "Generating Prisma client..."
+npx prisma generate
+
+
+if [ "$NODE_ENV" != "production" ]; then
+  echo "Non-production environment detected, importing sample data..."
+    # Import exercises if CSV exists
+    if [ -f "./data/sample-exercises.csv" ]; then
+        npx tsx scripts/import-exercises-with-attributes.ts ./sample-data/exercises.csv
+    else
+        echo "No exercises sample data found, skipping import."
+    fi
+else
+  echo "Production environment, skipping sample data import."
+fi
+
+echo "Starting the app..."
+exec "$@"  # runs the CMD from the Dockerfile