Dockerfile update

This commit is contained in:
Ryan 2025-04-08 19:44:58 +10:00
parent 3eb38a6d84
commit d4ef0e7120

View File

@ -1,53 +1,46 @@
# Stage 1: Build Stage Create a new Medusa application and build it # Stage 1: Builder Stage Generate and build the Medusa project silently
FROM node:20-alpine AS builder FROM node:20-alpine AS builder
# Set the working directory for generation and installation
WORKDIR /workspace WORKDIR /workspace
# Run the Medusa app generator non-interactively using default options. # Run the Medusa generator non-interactively.
# This will create a new Medusa project named "my-medusa-store" in the /workspace directory. # Options used:
RUN npx create-medusa-app@latest my-medusa-store --yes # --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 directory into the generated Medusa project. # Change into the generated project directory.
WORKDIR /workspace/my-medusa-store WORKDIR /workspace/my-medusa-store
# Install project dependencies (Medusa and its related packages are installed by the generator). # Install dependencies and build the project.
RUN npm install RUN npm install && npm run build
# Build the Medusa project for production. # Stage 2: Production Stage Build the final image to run the Medusa application
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 FROM node:20-alpine AS production
# Set production environment and default configuration variables. # Define default environment variables.
# These environment variables can be overridden via Coolify. # These values can be overridden via Coolifys UI.
ENV NODE_ENV=production ENV NODE_ENV=production
ENV DATABASE_URL=postgres://medusa:medusa@db:5432/medusa ENV DATABASE_URL=postgres://medusa:medusa@db:5432/medusa
ENV MEDUSA_STORE_NAME="My Medusa Store" ENV MEDUSA_STORE_NAME="My Medusa Store"
ENV MEDUSA_ADMIN_EMAIL="admin@medusajs.com" ENV MEDUSA_ADMIN_EMAIL="admin@medusajs.com"
ENV MEDUSA_ADMIN_PASSWORD="supersecret" ENV MEDUSA_ADMIN_PASSWORD="supersecret"
# Set to "true" in Coolify to run seed scripts on startup ENV SEED_DATA="false" # Set to "true" (via Coolify) if you want to run a seed command at startup.
ENV SEED_DATA="false"
WORKDIR /app WORKDIR /app
# Copy the built Medusa application from the builder stage into the final image. # Copy the built application from the builder stage.
COPY --from=builder /workspace/my-medusa-store . COPY --from=builder /workspace/my-medusa-store .
# Expose the default port that the Medusa server listens on. # Expose the default port for Medusa (which runs on 9000).
EXPOSE 9000 EXPOSE 9000
# (Optional) Create an admin user if needed. # Start the Medusa application.
# If you want the container itself to create an admin user at startup, # This command conditionally runs a seeding command if SEED_DATA is "true" and then starts the server.
# 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"] CMD ["sh", "-c", "if [ \"$SEED_DATA\" = \"true\" ]; then npx medusa seed; fi && npm start"]