@molecule/api-staging-docker-compose
Provider bond · staging · API (Node) · v1.1.3 · Apache-2.0
Node Dockerfile artifacts and Docker Compose staging for local ephemeral environments
npm install @molecule/api-staging-docker-composenpm · Source on GitHub · Implements @molecule/api-staging
How it works
@molecule/api-staging-docker-compose is a provider bond on the API (Node) side: it implements the staging core interface (@molecule/api-staging) with a concrete vendor or library behind it.
Your code calls the core; you wire this provider once at startup. Swapping vendors later is one line in that wiring, not a rewrite.
import { setProvider } from '@molecule/api-staging'
import { provider } from '@molecule/api-staging-docker-compose'
setProvider(provider)Works with: @molecule/api-staging
Reference
Docker Compose staging driver for molecule.dev.
Manages ephemeral staging environments using Docker Compose. Each feature branch gets isolated API, App, and database containers with unique port allocations and networking.
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
import { setProvider } from '@molecule/api-staging'
import { provider } from '@molecule/api-staging-docker-compose'
setProvider(provider)
import { generateApiDeploymentArtifacts } from '@molecule/api-staging-docker-compose'
const files = generateApiDeploymentArtifacts({
runtimeDirectory: 'api',
runtimePaths: ['api', 'app/dist'],
runtimeCommand: ['node', 'dist/server.js'],
})
// Ordinary Dockerfile/dockerignore/build script; caller owns file writes and hosting.
Type
provider
Installation
npm install @molecule/api-staging-docker-compose @molecule/api-staging
API
Interfaces
ComposeGeneratorConfig
Configuration for Docker Compose file generation.
interface ComposeGeneratorConfig {
/** Allocated API port on the host. */
apiPort: number
/** Allocated App port on the host. */
appPort: number
/** Allocated database port on the host. */
dbPort: number
/** Relative path from compose file to the API project root. */
apiContext?: string
/** Relative path from compose file to the App project root. */
appContext?: string
/** Path to generated Dockerfiles directory, relative to the compose file. */
dockerfilePath?: string
}
ComposeContainerStatus
A single service row parsed from docker compose ps --format json.
interface ComposeContainerStatus {
Service: string
State: string
Health: string
}
ApiDeploymentArtifactOptions
Explicit project facts used to generate a native-compatible Node deployment.
interface ApiDeploymentArtifactOptions {
/** One Debian/apt-based image for build and runtime; an immutable digest is supported. */
nodeImage?: string
/** Build command as argv, executed after npm ci with install scripts enabled. */
buildCommand?: readonly string[]
/** Server command as argv, executed without a shell. */
runtimeCommand?: readonly string[]
/** Output paths to carry. For workspaces, include the server workspace and frontend output. */
runtimePaths?: readonly string[]
/** Server working directory relative to the build context root. */
runtimeDirectory?: string
/** Additional Debian build prerequisites, beyond Python/make/C++. */
buildPackages?: readonly string[]
/** Additional Debian runtime libraries/tools. */
runtimePackages?: readonly string[]
/** Exposed container port; does not configure a hosting provider. */
port?: number
}
ApiDeploymentArtifacts
Ordinary project files; callers own their write/merge policy for existing files.
interface ApiDeploymentArtifacts {
/** Self-contained multi-stage Dockerfile; does not depend on the external build script. */
Dockerfile: string
/** The identical POSIX install/build/prune body embedded in the Dockerfile. */
'.molecule/deploy/build.sh': string
/** Build-context exclusions for host modules, old output and local secrets. */
'.dockerignore': string
}
Functions
generateComposeFile(env, config)
Generates a Docker Compose YAML string for a staging environment.
The API service uses runtime env_file layering (Node.js reads process.env). The App service uses Docker build args for VITE_* variables (Vite inlines them at build time; runtime env vars have no effect on the served bundle).
Requires Docker Compose v2.24+ (long-syntax env_file with required: false — the base .env / .env.staging layers are OPTIONAL; short syntax
hard-fails up on any project that doesn't have both files) and BuildKit
(additional_contexts, which lets the app image copy the generated
nginx.conf from the staging directory — it is NOT inside the app build
context).
The api and app services each define a healthcheck: (previously only
db did) so health() can read the real Health field from docker compose ps instead of treating container State === 'running' as
healthy — a process that is up but never came up serving (or is
crash-looping between polls) used to read as a fully healthy environment.
api's probe hits the /health route every molecule-scaffolded API
server exposes (server.ts — this driver already assumes that shape via
the hardcoded node dist/server.js Dockerfile CMD); app's probe hits the
Nginx root, which always serves the built index.html.
function generateComposeFile(env: StagingEnvironment, config: ComposeGeneratorConfig): string
env— The staging environment descriptor.config— Port and path configuration.
Returns: A Docker Compose YAML string.
generateApiDeploymentArtifacts(options)
Generate ordinary deployment files for a Node package or npm workspace root. Uses the project's lockfile, standard npm lifecycle scripts, a native build toolchain and npm prune. Build and runtime use the same Node/libc image; runtime copies the built modules instead of reinstalling without a compiler. Native dependencies are built for the Docker target, never the host's modules.
function generateApiDeploymentArtifacts({
nodeImage = NODE_DEPLOYMENT_IMAGE,
buildCommand = ['npm', 'run', 'build'],
runtimeCommand = ['node', 'dist/server.js'],
runtimePaths = ['dist'],
runtimeDirectory = '.',
buildPackages = [],
runtimePackages = [],
port = 4000,
}?: ApiDeploymentArtifactOptions): ApiDeploymentArtifacts
options— Layout, commands and system dependencies supplied by the caller.
Returns: A self-contained Dockerfile, paired dockerignore and identical build script.
generateApiDockerfile(options)
Generates a multi-stage Dockerfile for the API server.
Uses generateApiDeploymentArtifacts, including its native install toolchain and matching build/runtime image. The paired ignore is available from that function; callers using another Dockerfile name should write it alongside that Dockerfile as <Dockerfile-name>.dockerignore.
function generateApiDockerfile(options?: ApiDeploymentArtifactOptions): string
options— Explicit build/runtime facts; defaults preserve the package layout.
Returns: Dockerfile content as a string.
generateAppDockerfile(envVars)
Generates a multi-stage Dockerfile for the frontend app.
Stage 1 (build): installs deps, injects Vite env vars via Docker ARG, then runs the Vite build. ARG → ENV ensures `process.env.VITE_*` is available to Vite at build time (takes precedence over .env files).
Stage 2 (serve): copies built static assets into Nginx. The nginx.conf is copied from the `staging` named build context (the `.molecule/staging/` directory, wired via `additional_contexts` in the generated compose file) — NOT from the app build context: the app project has no nginx.conf of its own, so a plain `COPY nginx.conf` would fail every build with "not found in build context".
function generateAppDockerfile(envVars?: Record<string, string>): string
envVars— Build-time environment variables to inject (e.g. `{ VITE_API_URL: '...' }`).
Returns: Dockerfile content as a string.
generateNginxConf()
Generates an SPA-compatible Nginx configuration.
Uses `try_files` to serve `index.html` for all routes, which is required for client-side routing (React Router, Vue Router, etc.).
Only content-hashed build output is cached for a year: Vite's `/assets/` and SvelteKit's `/_app/immutable/`, whose names change with their bytes. A missing hashed file is a 404, never the app page. Every other file (`index.html`, the service worker, `public/` icons, SvelteKit's `_app/version.json`) keeps its name across deploys, so it revalidates and a new deploy shows up on the next load.
function generateNginxConf(): string
Returns: Nginx config content as a string.
parseComposeVersion(output)
Parses a major.minor pair out of docker compose version output (e.g.
'Docker Compose version v2.24.5' or the bare '2.24.5' from --short).
Exported for testing — no need to shell out to docker to verify the
parsing logic against every version-string shape Compose has printed.
function parseComposeVersion(output: string): { major: number; minor: number } | null
output— Raw stdout fromdocker compose version.
Returns: The parsed { major, minor }, or null if no version substring is found.
isComposeVersionSufficient(version)
Checks a parsed Compose version against REQUIRED_COMPOSE_VERSION.
function isComposeVersionSufficient(version: { major: number; minor: number }): boolean
version— A parsed{ major, minor }version.
Returns: true if the version meets or exceeds the minimum this driver requires.
fallbackPort(base, slug, range)
Deterministically derives a fallback host port from an environment slug so
that TWO environments falling back to this path (no driverMeta — direct
provider callers only; mlcl stage up always allocates real ports via
allocatePort()) don't collide on the SAME fixed port. Uses FNV-1a, a
stable, dependency-free 32-bit hash — the same slug always maps to the same
port across repeated up() calls, so redeploying an existing slug doesn't
relocate it.
function fallbackPort(base: number, slug: string, range?: number): number
base— The base port for this role (api/app/db).slug— The environment slug to derive an offset from.range— Width of the offset window (default 100 — kept narrow enough that api/app/db's offset windows never overlap each other).
Returns: base plus a slug-derived offset in [0, range).
isContainerHealthy(container)
Determines whether a parsed docker compose ps container row counts as
healthy. generateComposeFile() now defines a healthcheck: for the api
AND app services (it always did for db), so Health is populated for
both — State === 'running' alone used to be treated as healthy, which is
also true of a process that is up but never came up serving (or is
crash-looping between polls). Falls back to State === 'running' only for
a container with NO healthcheck (an environment staged with a compose file
generated before this fix, or a hand-edited one) — an empty Health
string is how docker compose ps reports "no healthcheck defined".
function isContainerHealthy(container?: ComposeContainerStatus): boolean
container— The parsed container row, orundefinedif the service wasn't found.
Returns: true if the container is healthy.
containerStatus(container)
Returns the human-readable status for a parsed docker compose ps
container row — the Health value when a healthcheck is defined,
otherwise the raw container State, or 'not found' when the service
isn't running at all.
function containerStatus(container?: ComposeContainerStatus): string
container— The parsed container row, orundefinedif the service wasn't found.
Returns: The status string.
Constants
NODE_DEPLOYMENT_IMAGE
Default compatible Node/libc base, shared by build and runtime stages.
const NODE_DEPLOYMENT_IMAGE: 'node:22.23.4-trixie-slim'
provider
Docker Compose staging driver implementation.
const provider: StagingDriver
Core Interface
Implements @molecule/api-staging interface.
Bond Wiring
Setup function to register this provider with the core interface:
import { setProvider } from '@molecule/api-staging'
import { provider } from '@molecule/api-staging-docker-compose'
export function setupStagingDockerCompose(): void {
setProvider(provider)
}
Injection Notes
Requirements
Peer dependencies:
@molecule/api-staging^1.0.1
Runtime Dependencies
-
@molecule/api-staging -
API artifacts use a patched Node 22 Debian base and build native modules with Python/make/C++. Install lifecycle scripts run with project source present; dev dependencies are pruned before built modules move into the matching, nonroot runtime. No second production install recompiles native modules without tools. Use the generated dockerignore to exclude host node_modules and local secrets. For a workspace root, explicitly carry its server workspace (including manifests/nested dependencies) and frontend output. Extra system libraries and alternative apt-based images are caller configuration; native compatibility must be verified on the target platform.
-
Artifact generation is pure: it starts no container, infers no database or route and overwrites no project file. The Compose provider uses these files for its existing separate API/App contexts; other deployment callers can use the same generator without adopting Compose's service topology.
-
Requires Docker Compose v2.24+ and BuildKit: the generated compose file uses long-syntax
env_file(required: false— a project without.envor.env.stagingmust still stage) and a namedadditional_contextsbuild context (the generatednginx.conflives in.molecule/staging/, outside the app build context).checkPrerequisites()parsesdocker compose versionand names'docker-compose >= 2.24 (found X.Y)'inmissingwhen the installed engine predates this — beforeup()hits the engine's opaque compose parse error. -
Inside the containers the API always listens on port 4000 and Postgres on 5432; the allocated
driverMetaports (apiPort,appPort,dbPort) are HOST-side mappings only. The branch env file (.env.staging.<slug>) intentionally holds the host-side values for tooling run outside Docker — the composeenvironment:block overridesPORT/DATABASE_URLback to the in-container values. -
When
env.driverMetahas no port overrides (direct provider callers only —mlcl stage upalways allocates real ports viaallocatePort()),up()derivesapiPort/appPort/dbPortdeterministically fromenv.slug(fallbackPort()) instead of three fixed ports, so two concurrently-staged slugs don't collide on identical ports. -
health()reads the REALHealthfield fromdocker compose psfor theapiandappservices (both now define ahealthcheck:hitting/healthand/respectively) — a booted-but-not-yet-serving or crash-looping container no longer reads as healthy just because its process state isrunning.logs()still needs a look for WHY an unhealthy service is unhealthy. -
logs({ follow: true })throws — this provider returns a singlePromise<EnvironmentLogs>snapshot, not a stream, sofollowcannot be honored; it is rejected explicitly instead of silently returning a static tail. -
Environments live on the machine that ran
up()(containers + state are local) — running this driver on an ephemeral CI runner produces an environment that dies with the job.