# Stage 1: Build Stage – Create a new Medusa application and build it FROM node:20-alpine AS builder # 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 # 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 production # 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" # Set to "true" in Coolify to run seed scripts on startup ENV SEED_DATA="false" 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 # (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"]