Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Improve docker for local dev

Damian Legawieccf28a8d0b51c
diff --git a/.claude/settings.local.json b/.claude/settings.local.json
index d61220f216..3812559a68 100644
--- a/.claude/settings.local.json
+++ b/.claude/settings.local.json
@@ -11,7 +11,12 @@
"WebSearch",
"Task",
"mcp__*",
- "Read(//private/tmp/**)"
+ "Read(//private/tmp/**)",
+ "Bash(cp server/.env /tmp/server-env-backup)",
+ "Bash(rm -rf server)",
+ "Bash(git clone *)",
+ "Bash(rm -rf server/.git server/.gitignore)",
+ "Bash(cp /tmp/server-env-backup server/.env)"
],
"deny": [
"Bash(sudo *)",
diff --git a/package.json b/package.json
index 25363baa29..e62782d4c7 100644
--- a/package.json
+++ b/package.json
@@ -11,14 +11,14 @@
"format": "turbo format",
"typecheck": "turbo typecheck",
"clean": "turbo clean",
- "server:setup": "git clone --depth 1 https://github.com/spree/spree-starter.git server && rm -rf server/.git server/.gitignore && echo 'SPREE_PATH=..' > server/.env && echo '\nServer cloned to server/. Next steps:\n cd server\n # edit .env if needed (DATABASE_USERNAME, etc.)\n bin/setup\n bin/dev\n'",
- "server:dev": "cd server && bin/dev",
- "server:console": "cd server && bin/rails c",
+ "server:create": "git clone --depth 1 https://github.com/spree/spree-starter.git server && rm -rf server/.git server/.gitignore && printf 'SPREE_PATH=..\\nSECRET_KEY_BASE=%s\\n' \"$(openssl rand -hex 64)\" > server/.env",
+ "server:setup": "bash scripts/server-setup.sh",
+ "server:start": "SPREE_PATH=$PWD docker compose -f server/docker-compose.dev.yml -f scripts/docker-compose.edge.yml up -d --force-recreate web worker",
+ "server:stop": "SPREE_PATH=$PWD docker compose -f server/docker-compose.dev.yml -f scripts/docker-compose.edge.yml down",
+ "server:console": "cd server && node ../packages/cli/dist/index.js console",
"server:logs": "cd server && tail -f log/development.log",
- "server:seed": "cd server && bin/rails db:seed",
- "server:load_sample_data": "cd server && bin/rails spree:load_sample_data",
- "server:up": "docker compose up -d",
- "server:down": "docker compose down"
+ "server:seed": "cd server && node ../packages/cli/dist/index.js rails db:seed",
+ "server:load_sample_data": "cd server && node ../packages/cli/dist/index.js task load_sample_data"
},
"devDependencies": {
"@biomejs/biome": "^2.4.14",
diff --git a/scripts/docker-compose.edge.yml b/scripts/docker-compose.edge.yml
new file mode 100644
index 0000000000..e37b204924
--- /dev/null
+++ b/scripts/docker-compose.edge.yml
@@ -0,0 +1,41 @@
+# Internal Spree development overlay — mounts a local checkout of the
+# spree/spree monorepo at the same path inside the container so the
+# Gemfile's path-based gem block (`path "#{ENV['SPREE_PATH']}/spree"`)
+# resolves identically on the host and in the container.
+#
+# Activate by chaining this file after the dev compose:
+#
+# SPREE_PATH=/absolute/host/path/to/spree-monorepo \
+# docker compose -f docker-compose.dev.yml -f docker-compose.edge.yml \
+# up -d --force-recreate web worker
+#
+# Without -f docker-compose.edge.yml, regular dev usage is unaffected:
+# no extra mounts, no SPREE_PATH dependency, runs identically on
+# Docker Desktop, OrbStack, and stock Docker Engine on Linux.
+#
+# Gotcha: enabling edge rewrites Gemfile.lock with a PATH block pointing
+# at the host monorepo. Run `git checkout Gemfile.lock` before committing
+# unrelated changes.
+
+services:
+ web:
+ # Override SPREE_PATH inside the container to match the absolute host
+ # path we mirror-mount. The starter's .env may carry a different
+ # SPREE_PATH (e.g. `..` for the native bin/dev flow); the overlay-set
+ # value here wins because `environment:` beats `env_file:` in Compose.
+ environment:
+ SPREE_PATH: ${SPREE_PATH}
+ volumes:
+ - ${SPREE_PATH}:${SPREE_PATH}:ro
+ # Hard-bypass the starter's bin/docker-entrypoint, which auto-runs
+ # `db:prepare` when the command is `bin/rails server`. The dev compose
+ # claims to bypass it but doesn't actually set entrypoint: [], so we
+ # do it here. server:setup runs DB prep explicitly via the CLI.
+ entrypoint: []
+
+ worker:
+ environment:
+ SPREE_PATH: ${SPREE_PATH}
+ volumes:
+ - ${SPREE_PATH}:${SPREE_PATH}:ro
+ entrypoint: []
diff --git a/scripts/server-setup.sh b/scripts/server-setup.sh
new file mode 100755
index 0000000000..4c0630cfe5
--- /dev/null
+++ b/scripts/server-setup.sh
@@ -0,0 +1,82 @@
+#!/usr/bin/env bash
+# server-setup.sh — bootstrap a development backend in ./server/ wired up to
+# load Spree gems from the monorepo (edge mode).
+#
+# Sequence:
+# 1. Tear down any prior stack + volumes (clean slate, even after a failed run)
+# 2. Force-remove ./server/ if it exists (Docker bind-mounted files end up
+# owned by UID 1000 inside the container; on macOS Docker Desktop maps
+# this to the host user, but we still need elevated rm if a prior run
+# left files in odd states).
+# 3. Clone spree-starter into server/
+# 4. Write server/.env with SPREE_PATH=.. (for the native bin/dev path —
+# Docker edge flow overrides via compose env) + a fresh SECRET_KEY_BASE
+# 5. Build @spree/cli so node ../packages/cli/dist/index.js works
+# 6. Start the edge stack
+# 7. Wait until web container is `running` (not `healthy` — healthcheck
+# polls /up which 500s without a DB)
+# 8. CLI: bundle install (rewrites Gemfile.lock to use path-based gems)
+# 9. CLI: rake spree:install:migrations (copies migrations from edge gems
+# into server/db/migrate/)
+# 10. CLI: rails db:prepare (create + migrate + seed)
+#
+# Idempotent: re-running this from any state should converge. The volume
+# nuke in step 1 is what makes it idempotent in the face of partially-failed
+# prior runs.
+
+set -euo pipefail
+
+ROOT="$(cd "$(dirname "$0")/.." && pwd)"
+cd "$ROOT"
+
+DEV_COMPOSE="server/docker-compose.dev.yml"
+EDGE_OVERLAY="scripts/docker-compose.edge.yml"
+SERVER_DIR="$ROOT/server"
+
+step() { printf '\n→ %s\n' "$1"; }
+
+step "Tearing down any prior stack (this also wipes the dev volumes)"
+# Reference the project by name (-p server) rather than via compose files,
+# so this works even after a prior run deleted ./server/. Orphan volumes
+# from a partially-failed prior run (server_bundle_cache, etc.) cause
+# `failed to mkdir /var/lib/docker/volumes/.../ruby: file exists` on the
+# next `up` if we don't wipe them here.
+docker compose -p server down -v --remove-orphans 2>/dev/null || true
+
+step "Removing any prior $SERVER_DIR"
+# Volumes are now released; bind-mounted files are accessible to rm again.
+rm -rf "$SERVER_DIR"
+
+step "Cloning spree-starter into server/"
+git clone --depth 1 https://github.com/spree/spree-starter.git "$SERVER_DIR"
+rm -rf "$SERVER_DIR/.git" "$SERVER_DIR/.gitignore"
+
+step "Writing server/.env (SPREE_PATH + SECRET_KEY_BASE)"
+printf 'SPREE_PATH=..\nSECRET_KEY_BASE=%s\n' "$(openssl rand -hex 64)" > "$SERVER_DIR/.env"
+
+step "Building @spree/cli (so node ../packages/cli/dist/index.js works)"
+pnpm --filter @spree/cli build
+
+step "Starting the edge stack"
+pnpm server:start
+
+step "Waiting for web container to come up"
+until docker compose -f "$DEV_COMPOSE" ps web --format '{{.State}}' | grep -q running; do
+ sleep 1
+done
+# Even when running, Rails takes a few seconds to be ready to accept exec.
+# A quick warmup avoids the first `docker compose exec` racing the boot.
+sleep 5
+
+step "bundle install (rewrites Gemfile.lock against monorepo gems)"
+cd "$SERVER_DIR"
+SPREE_CLI="$ROOT/packages/cli/dist/index.js"
+node "$SPREE_CLI" bundle install
+
+step "spree:install:migrations (copies edge migrations into server/db/migrate/)"
+node "$SPREE_CLI" rake spree:install:migrations
+
+step "db:prepare (create + migrate + seed)"
+node "$SPREE_CLI" rails db:prepare
+
+printf '\nServer ready: http://localhost:3000\nAdmin: http://localhost:3000/admin\n\n'