From afaebaa82165ea730914a46608ace1491e3cebde Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 8 Apr 2025 13:38:06 +1000 Subject: [PATCH] Dockerfile update --- Dockerfile | 56 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index b309539..197ae32 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,52 @@ -# Use an official Node.js image as the base -FROM node:16-alpine AS base -WORKDIR /app +# Stage 1: Build Stage – Create a new Medusa application and build it +FROM node:20-alpine AS builder -# Install dependencies -COPY package*.json ./ +# Set the working directory for generation and installation +WORKDIR /workspace + +# Run the Medusa app generator non-interactively using default options. +# This will create a new Medusa project named "my-medusa-store" in the /workspace directory. +RUN npx create-medusa-app@latest my-medusa-store --yes + +# Change directory into the generated Medusa project. +WORKDIR /workspace/my-medusa-store + +# Install project dependencies (Medusa and its related packages are installed by the generator). RUN npm install -# Copy code and build for production -FROM base AS production -COPY . . +# Build the Medusa project for production. RUN npm run build + +# Optionally, you could run a seed script here if you want seeding done during the build: +# RUN if [ "$SEED_DATA" = "true" ]; then npx medusa seed; fi + + +# Stage 2: Runner Stage – Create the final production image +FROM node:20-alpine AS runner + +# Set production environment and default configuration variables. +# These environment variables can be overridden via Coolify. +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" in Coolify to run seed scripts on startup + +WORKDIR /app + +# Copy the built Medusa application from the builder stage into the final image. +COPY --from=builder /workspace/my-medusa-store . + +# Expose the default port that the Medusa server listens on. EXPOSE 9000 -CMD ["npm", "start"] + +# (Optional) Create an admin user if needed. +# If you want the container itself to create an admin user at startup, +# you could include a command like the following. +# It depends on having your CLI support non-interactive admin creation. +# RUN if [ "$CREATE_ADMIN" = "true" ]; then npx medusa user -e "$MEDUSA_ADMIN_EMAIL" -p "$MEDUSA_ADMIN_PASSWORD"; fi + +# Start up the Medusa application. +# This command conditionally runs the seed command if SEED_DATA is set to "true" and then starts the server. +CMD ["sh", "-c", "if [ \"$SEED_DATA\" = \"true\" ]; then npx medusa seed; fi && npm start"]