From d85bc81b7e9436906ec27cc6a46e0a519eb1edd6 Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 8 Apr 2025 20:32:15 +1000 Subject: [PATCH] Update docker stuff with script --- Dockerfile | 51 ++++++++------------------------------------- docker-compose.yaml | 30 +++++++++++++++----------- entrypoint.sh | 35 +++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 54 deletions(-) create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 90d59d9..cce6e94 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,47 +1,14 @@ -# Stage 1: Builder Stage – Generate and build the Medusa project silently -FROM node:20-alpine AS builder - -WORKDIR /workspace - -# Run the Medusa generator non-interactively. -# Options used: -# --db-url "postgres://medusa:medusa@db:5432/medusa" : Provide the database URL so it connects (and runs migrations & seeding). -# --no-browser : Do not open the browser after installation. -# --directory-path : Specify the installation directory. -# --seed : Seed the database with demo data. -RUN npx create-medusa-app@latest my-medusa-store \ - --db-url "postgres://medusa:medusa@db:5432/medusa" \ - --no-browser \ - --directory-path /workspace/my-medusa-store \ - --seed - -# Change into the generated project directory. -WORKDIR /workspace/my-medusa-store - -# Install dependencies and build the project. -RUN npm install && npm run build - -# Stage 2: Production Stage – Build the final image to run the Medusa application -FROM node:20-alpine AS production - -# Define default environment variables. -# These values can be overridden via Coolify’s UI. -ENV NODE_ENV=production -ENV DATABASE_URL=postgres://medusa:medusa@db:5432/medusa -ENV MEDUSA_STORE_NAME="My Medusa Store" -ENV MEDUSA_ADMIN_EMAIL="admin@medusajs.com" -ENV MEDUSA_ADMIN_PASSWORD="supersecret" -ENV SEED_DATA="false" -# Set to "true" (via Coolify) if you want to run a seed command at startup. +FROM node:20-alpine +# Set working directory in the container. WORKDIR /app -# Copy the built application from the builder stage. -COPY --from=builder /workspace/my-medusa-store . +# Copy the entrypoint script into /app and make it executable. +COPY entrypoint.sh /app/entrypoint.sh +RUN chmod +x /app/entrypoint.sh -# Expose the default port for Medusa (which runs on 9000). -EXPOSE 9000 +# (Optional) You might also want to copy other files that your Medusa app may need, +# but in this case, the project is created during runtime and stored in a persistent volume. -# Start the Medusa application. -# This command conditionally runs a seeding command if SEED_DATA is "true" and then starts the server. -CMD ["sh", "-c", "if [ \"$SEED_DATA\" = \"true\" ]; then npx medusa seed; fi && npm start"] +# Set the default command to run the entrypoint script. +CMD ["/app/entrypoint.sh"] diff --git a/docker-compose.yaml b/docker-compose.yaml index ccab663..9ec05c2 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,25 +1,31 @@ version: "3.8" services: - app: + medusa: build: context: . dockerfile: Dockerfile - target: production # Optional: use if your Dockerfile defines multiple stages + ports: + - "9000:9000" environment: - # Define your app-specific environment variables - DATABASE_URL: postgres://user:pass@db:5432/app - # Do not define "ports" here—Coolify will handle them for you - depends_on: - - db + # Default environment variables + DATABASE_URL: "postgres://medusa:medusa@db:5432/medusa" + # You can include additional environment variables as needed, e.g.: + MEDUSA_STORE_NAME: "My Medusa Store" + MEDUSA_ADMIN_EMAIL: "admin@medusajs.com" + MEDUSA_ADMIN_PASSWORD: "supersecret" + volumes: + # Persist the generated Medusa project so that installation only runs once. + - medusa_app_data:/app/my-medusa-app db: image: postgres:13 environment: - POSTGRES_USER: user - POSTGRES_PASSWORD: pass - POSTGRES_DB: app + POSTGRES_USER: medusa + POSTGRES_PASSWORD: medusa + POSTGRES_DB: medusa volumes: - - postgres-data:/var/lib/postgresql/data + - postgres_data:/var/lib/postgresql/data volumes: - postgres-data: + medusa_app_data: + postgres_data: diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..aff358e --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,35 @@ +#!/bin/sh +set -e + +PROJECT_DIR="/app/my-medusa-app" + +# Check if the Medusa project has already been created. +if [ ! -d "$PROJECT_DIR" ]; then + echo "Medusa project not found. Running installation..." + + # Run the Medusa CLI generator silently with the desired options: + # --db-url: Uses the provided DATABASE_URL to connect and run migrations. + # --no-browser: Skip opening the admin dashboard. + # --directory-path: Specify where to install the project. + # --seed: Seed the database with demo data. + npx create-medusa-app@latest my-medusa-app \ + --db-url "$DATABASE_URL" \ + --no-browser \ + --directory-path "$PROJECT_DIR" \ + --seed + + # Change into the project folder, install dependencies and build the app. + cd "$PROJECT_DIR" + npm install + npm run build +else + echo "Medusa project already exists. Skipping installation." +fi + +# Change directory to the Medusa project folder. +cd "$PROJECT_DIR" + +# Finally, start the Medusa server in development/production mode as desired. +# For example, here we run the development server: +echo "Starting Medusa server..." +exec npm start