Get your projects ready for Moorage
To get the most out of it: several copies at once, each one wired to itself and never clashing with the others.
The idea in one sentence: nothing that changes between copies can be hard-coded. Ports and URLs are read from the environment; Moorage sets them
for each copy when starting it, and they win over whatever the .env says.
How it works
- Template: your project's services (API, web, app, socket, database…), each one's command, its base port and the variables it needs.
- Instance or copy: the template in a specific folder. The first one uses the base ports; each copy shifts them (+100, +200…).
-
Injected variables: when starting each service, Moorage passes it the template's variables resolved for that copy. System variables win over
the
.envin Next.js, Vite/Astro, Expo, dotenv, Laravel and CodeIgniter, so you don't need to touch each copy's .env.
1. The port is chosen from outside
Each copy needs its own ports. If the code says listen(3000), two copies clash. The port must come from a parameter or from
PORT:
| Project | Command in the template |
|---|---|
| Node / Express | node app.js + variable PORT={{port}}, and in the code listen(process.env.PORT || 3000) |
| Next.js | next dev -p {{port}} |
| Vite, Astro, Nuxt | vite --port {{port}} · astro dev --port {{port}} |
| Expo | expo start --port {{port}} |
| Laravel | php artisan serve --port={{port}} |
| CodeIgniter, PHP | php -d variables_order=EGPCS -S {{host.lan}}:{{port}} -t public (see point 3) |
| Django | python manage.py runserver 0.0.0.0:{{port}} |
Moorage detects this and suggests the command when creating the template. Make sure no two services share a base port: two services on 3000 clash inside the same copy.
2. Other services' URLs, in variables
Copy 2's web must talk to copy 2's API, not the original's. So the addresses of the other services go in variables (NEXT_PUBLIC_API_URL,
VITE_API_URL, EXPO_PUBLIC_API_URL, API_URL…), never hard-coded:
// Wrong: every copy talks to the same API fetch('http://192.168.1.35:8082/users') // Right: each copy its own fetch(`${process.env.NEXT_PUBLIC_API_URL}/users`)
And in the template, you tell each service where the others are with {{service.url}}:
web → NEXT_PUBLIC_API_URL = {{api.url}}
NEXT_PUBLIC_SOCKET_URL = {{socket.url}}
app → EXPO_PUBLIC_API_URL = {{api.url}}
socket → API_URL = {{api.url}}
You don't need to type them: when creating the stack, Moorage reads each project's .env and, if a URL points to another service's port
(API_URL=http://localhost:8080), suggests replacing it with {{api.url}}. In an existing template, use
“Detect connections”, or “Connect to” in a service's “Variables and connections” tab.
If a service is usually parked (you don't run it locally), don't point the others at it: leave them the production URL from the .env.
Otherwise that feature will fail locally.
3. The .env: shared values and secrets
-
The
.envholds what's the same in every copy: keys, credentials, third-party URLs. Never in git; commit a.env.examplewith every key and no secret values. -
What changes per copy (ports and URLs between services) is set by Moorage. You can keep a default in the
.envfor when you run without Moorage. -
When creating a copy, “Copy the .env files from…” copies them from the original (or from the copy it comes from). For copies you already have,
the
.enveditor tells you if any are missing and copies them. -
PHP with
php -S: PHP's server only passes the environment togetenv(), and CodeIgniter reads$_ENVand$_SERVER. With-d variables_order=EGPCSMoorage's variables do reach it; Moorage adds it when it detects a PHP project. In CodeIgniter, anyConfig\Appproperty can be changed withapp_name(e.g.app_baseURL={{url}}). If you also set it in the.env, write it the same way, with an underscore (app_baseURL = …): with a dot (app.baseURL), CodeIgniter gives the.envpriority over what Moorage sets.
4. CORS: let your copies in locally
If your API or socket only accepts a fixed list of origins (http://localhost:3003), copy 2's web (:3103) gets blocked. In
development, accept any port on localhost and your local network, switched on with a variable you don't set in production:
// development .env: CORS_ALLOW_LOCAL=1
const allowLocal = process.env.CORS_ALLOW_LOCAL === '1'
const isLocal = (o) => /^https?:\/\/(localhost|127\.0\.0\.1|192\.168\.\d+\.\d+|10\.\d+\.\d+\.\d+)(:\d+)?$/.test(o)
app.use(cors({ origin: (o, cb) => cb(null, !o || allowed.includes(o) || (allowLocal && isLocal(o))) }))
5. If there's a mobile app
-
The phone can't reach your PC's
localhost. In the template, set “Its URL uses: Local network IP” on the services the app uses (API, socket…), so{{api.url}}uses your IP. - Those services must listen on every interface (
0.0.0.0or the IP), not just127.0.0.1. - With Expo there can only be one Metro per device at a time: run the app of the copy you're working on.
6. The database
- One for all: on the service, “Across copies: Shared across copies”. Same port in every copy; if it's already running from another copy, that one is used.
-
One per copy (recommended, so data never clashes): a Docker service set to “One per copy”. Moorage passes it
COMPOSE_PROJECT_NAME(its own containers) andPORT; in yourdocker-compose.ymluse"${PORT:-3306}:3306". Set “Stop command” todocker compose stopso no containers are left running. - If every copy uses the production database, they share real data even if the ports don't clash.
Cookies ignore ports: two copies open on the same host share the session. With separate data, open each copy under a different name
(localhost,
127.0.0.1, your IP).
The template in Moorage
Variables you can use in each service's command and variables:
| Variable | What it is |
|---|---|
{{port}}, {{url}} |
This service's port and URL in this copy |
{{api.port}}, {{api.url}} |
Another service's, in the same copy (by its key) |
{{host.lan}}, {{host.localhost}} |
Your local network IP, or localhost |
{{instance.name}}, {{instance.offset}} |
The copy's name and its port offset |
In each service's “Startup” tab also check: “Starts after” and “Ready when the output contains” (to start in order), “Exclusive group” if only one can run at a time, and “Restart automatically if it crashes”. If something is off (two services on the same port, one that doesn't receive its port, a variable that doesn't exist), the template warns you at the top with a one-click fix.
For your team
- “Save the template to the repo” puts it in
.moorage/stack.moorage.json: your teammates get it with agit pull. - Shared Trello rules go in
.moorage/trello.json. - Add every new variable (like
PORTorCORS_ALLOW_LOCAL) to the.env.examplewith a comment.
Check it's right
- In each service's “…” menu, “Variables injected by Moorage” shows what it gets in this copy.
- Ports tells you which process uses each port and which copy it belongs to. If a copy shows up talking to another copy's port, a variable is missing.
- Run two copies at once: if neither says “port in use” and each web sees its own API's data, you're done.