Update docker stuff with script

This commit is contained in:
Ryan 2025-04-08 20:32:15 +10:00
parent 3c86ce99ae
commit d85bc81b7e
3 changed files with 62 additions and 54 deletions

View File

@ -1,47 +1,14 @@
# Stage 1: Builder Stage Generate and build the Medusa project silently FROM node:20-alpine
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 Coolifys 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.
# Set working directory in the container.
WORKDIR /app WORKDIR /app
# Copy the built application from the builder stage. # Copy the entrypoint script into /app and make it executable.
COPY --from=builder /workspace/my-medusa-store . COPY entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh
# Expose the default port for Medusa (which runs on 9000). # (Optional) You might also want to copy other files that your Medusa app may need,
EXPOSE 9000 # but in this case, the project is created during runtime and stored in a persistent volume.
# Start the Medusa application. # Set the default command to run the entrypoint script.
# This command conditionally runs a seeding command if SEED_DATA is "true" and then starts the server. CMD ["/app/entrypoint.sh"]
CMD ["sh", "-c", "if [ \"$SEED_DATA\" = \"true\" ]; then npx medusa seed; fi && npm start"]

View File

@ -1,25 +1,31 @@
version: "3.8" version: "3.8"
services: services:
app: medusa:
build: build:
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
target: production # Optional: use if your Dockerfile defines multiple stages ports:
- "9000:9000"
environment: environment:
# Define your app-specific environment variables # Default environment variables
DATABASE_URL: postgres://user:pass@db:5432/app DATABASE_URL: "postgres://medusa:medusa@db:5432/medusa"
# Do not define "ports" here—Coolify will handle them for you # You can include additional environment variables as needed, e.g.:
depends_on: MEDUSA_STORE_NAME: "My Medusa Store"
- db 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: db:
image: postgres:13 image: postgres:13
environment: environment:
POSTGRES_USER: user POSTGRES_USER: medusa
POSTGRES_PASSWORD: pass POSTGRES_PASSWORD: medusa
POSTGRES_DB: app POSTGRES_DB: medusa
volumes: volumes:
- postgres-data:/var/lib/postgresql/data - postgres_data:/var/lib/postgresql/data
volumes: volumes:
postgres-data: medusa_app_data:
postgres_data:

35
entrypoint.sh Normal file
View File

@ -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