fix(docker): Railway build timeout due to recursive chown in Dockerfile
Describe the bug
The Dockerfile at the project root uses RUN chown -R node:node . after copying and building the entire application. On Railway, this step alone takes approximately 17 minutes, followed by a 6-minute image export. The total build exceeds Railway's 30-minute activity heartbeat limit, causing the deployment to time out even though the build itself completes successfully.
To Reproduce
- Push any change that triggers a Railway build on service
51327026-1a43-4778-9fcc-c40c2d005d1d - Observe the build logs
- The build completes
pnpm buildsuccessfully - The
RUN chown -R node:node .step runs for ~17 minutes - Image export takes ~6 minutes
- Deployment times out during image push
Expected behavior
Build should complete well within the 30-minute platform limit by avoiding expensive recursive ownership changes on large directories like node_modules.
Proposed Fix
Use COPY --chown=node:node and pre-create the workdir with correct ownership before switching to the node user. This eliminates the need for chown -R entirely.
Additional context
- Service ID:
51327026-1a43-4778-9fcc-c40c2d005d1d - Base image:
node:20-alpine - The recursive chown is O(n) over every file in
node_modulesand build artifacts, which is why it's so slow.
🔧 Solution Proposal
Root Cause
RUN chown -R node:node . recursively changes ownership of ALL files including node_modules, build artifacts, and git history. On large monorepos this takes ~17 minutes.
Proposed Fix
Option A — Selective chown (recommended):
Only chown the files that actually need it, excluding node_modules and build artifacts:
# Instead of: RUN chown -R node:node .
# Use:
RUN chown -R node:node /app/src /app/package.json /app/pnpm-lock.yaml /app/prisma /app/packages/*/src
Option B — Use COPY --chown: Set ownership during COPY instead of a separate chown step:
COPY --chown=node:node package.json pnpm-lock.yaml ./
COPY --chown=node:node packages/ ./packages/
# ... etc
Option C — Run as root during build, switch at runtime:
# Build as root (no chown needed)
RUN pnpm install && pnpm build
# Switch to node user only for runtime
USER node
Option D — Exclude node_modules from chown:
RUN find . -not -path './node_modules/*' -not -path './.git/*' -exec chown node:node {} +
Recommended Approach
Option C is the cleanest — build as root (standard Docker practice), then USER node only for the runtime stage. The chown was likely added as a workaround for a specific permission issue that can be solved more efficiently.
Impact
- Reduces build time from ~17 min to <30 sec
- Enables Railway deployments to complete within 30-minute limit
- Standard Docker best practice
Can confirm this — hit the same wall deploying Flowise's own Dockerfile on Railway a while back. The chown -R node:node . after pnpm build is the killer; it walks every file in node_modules and the build output, and on Railway's shared build hardware that's the ~17 min you're seeing.
Option C above (build as root, USER node only at the runtime stage) is the right fix — no reason to chown anything during the build stage if you're not switching users until the final CMD. Option B (COPY --chown) alone won't fully solve it if any earlier step still does a bulk unchowned copy, but combined with C it's clean.
Full disclosure since it's relevant here: I maintain a Railway template for Flowise that sidesteps this entirely — it deploys the official flowiseai/flowise:3.1.3 image directly instead of building from this repo's Dockerfile, so there's no build step and no chown to time out. It's wired to Postgres with a volume so flows/credentials survive redeploys too, and I get a kickback if you deploy through it: https://railway.com/deploy/flowise-ai-agent-builder?referralCode=Z1xivh&utm_medium=integration&utm_source=template&utm_campaign=inventory
If you'd rather keep building from source, still worth landing this Dockerfile fix regardless — 17 extra minutes on every deploy is rough.