R
Railway•3mo ago
Jay

Issue when deploying node app using docker

I'm having issues when I'm trying to deploy a node app with docker, when I build and run the image locally it runs fine but on railway it shows me the build the built node file not found. Also, this is something that is fairly recent and my docker config file hasn't changed so not sure why this might be happening. Help appreciated, thanks!
Solution:
ah thanks, just found out the cause of the issue. It was a change that was pushed yesterday that resulted in dist folder not being generated in CI builds which is why it worked locally but not when it was deployed via CI.
Jump to solution
19 Replies
Percy
Percy•3mo ago
Project ID: 033f39f1-9797-405f-88f8-343ca4ac4254
Jay
Jay•3mo ago
033f39f1-9797-405f-88f8-343ca4ac4254 For some context, the build succeeds but when the cmd command is ran is when the error is thrown and the healthcheck fails, causing the deployment to fail.
Brody
Brody•3mo ago
Please send the dockerfile and the error that is thrown
Jay
Jay•3mo ago
Hey, thanks for the quick reply! Is there a private portal that I can send it through? nvm, sorry I just realized I don't have any sensitive info in the docker file so here it goes
FROM node:20-alpine AS alpine
RUN apk add --no-cache libc6-compat
RUN apk update

# Setup pnpm and turbo on the alpine base
FROM alpine as base
RUN npm install pnpm turbo --global
RUN pnpm config set store-dir ~/.pnpm-store

# Prune projects
FROM base AS pruner

ARG PROJECT="@repo/api"

WORKDIR /app
COPY . .
RUN turbo prune --scope=${PROJECT} --docker

# Build the project
FROM base AS builder
ARG PROJECT="@repo/api"

WORKDIR /app

# Copy lockfile and package.json's of isolated subworkspace
COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
COPY --from=pruner /app/out/pnpm-workspace.yaml ./pnpm-workspace.yaml
COPY --from=pruner /app/out/json/ .

# First install the dependencies (as they change less often)
RUN pnpm install --frozen-lockfile

# Copy source code of isolated subworkspace
COPY --from=pruner /app/out/full/ .

# Env vars required to build the project
ARG DATABASE_URL
ARG SOME_OTHER_KEY

RUN turbo build --filter=${PROJECT}
RUN rm -rf ./node_modules
RUN pnpm install --prod --frozen-lockfile

# Final image
FROM alpine AS runner

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nodejs
USER nodejs

WORKDIR /app
COPY --from=builder --chown=nodejs:nodejs /app .
WORKDIR /app/apps/api

ARG PORT=8080
ENV PORT=${PORT}

CMD ["node","dist/index.js"]
FROM node:20-alpine AS alpine
RUN apk add --no-cache libc6-compat
RUN apk update

# Setup pnpm and turbo on the alpine base
FROM alpine as base
RUN npm install pnpm turbo --global
RUN pnpm config set store-dir ~/.pnpm-store

# Prune projects
FROM base AS pruner

ARG PROJECT="@repo/api"

WORKDIR /app
COPY . .
RUN turbo prune --scope=${PROJECT} --docker

# Build the project
FROM base AS builder
ARG PROJECT="@repo/api"

WORKDIR /app

# Copy lockfile and package.json's of isolated subworkspace
COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
COPY --from=pruner /app/out/pnpm-workspace.yaml ./pnpm-workspace.yaml
COPY --from=pruner /app/out/json/ .

# First install the dependencies (as they change less often)
RUN pnpm install --frozen-lockfile

# Copy source code of isolated subworkspace
COPY --from=pruner /app/out/full/ .

# Env vars required to build the project
ARG DATABASE_URL
ARG SOME_OTHER_KEY

RUN turbo build --filter=${PROJECT}
RUN rm -rf ./node_modules
RUN pnpm install --prod --frozen-lockfile

# Final image
FROM alpine AS runner

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nodejs
USER nodejs

WORKDIR /app
COPY --from=builder --chown=nodejs:nodejs /app .
WORKDIR /app/apps/api

ARG PORT=8080
ENV PORT=${PORT}

CMD ["node","dist/index.js"]
And here are the logs:
node:internal/modules/cjs/loader:1146

throw err;

^

Error: Cannot find module '/app/apps/api/dist/index.js'

at Module._resolveFilename (node:internal/modules/cjs/loader:1143:15)

at Module._load (node:internal/modules/cjs/loader:984:27)

at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)

at node:internal/main/run_main_module:28:49 {

code: 'MODULE_NOT_FOUND',

requireStack: []

}
node:internal/modules/cjs/loader:1146

throw err;

^

Error: Cannot find module '/app/apps/api/dist/index.js'

at Module._resolveFilename (node:internal/modules/cjs/loader:1143:15)

at Module._load (node:internal/modules/cjs/loader:984:27)

at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)

at node:internal/main/run_main_module:28:49 {

code: 'MODULE_NOT_FOUND',

requireStack: []

}
Brody
Brody•3mo ago
just realized I don't have any sensitive info in the docker file
i would sure hope so 😆 can you try removing the WORKDIR /app/apps/api line
Jay
Jay•3mo ago
sure, I'll try it and be back in a bit! appreciate the quick responses! okay, so it just finished a deployment but I'm still getting the same error. I updated the dockerfile to remove WORKDIR /app/apps/api and updated the CMD command to run node apps/api/dist/index.js but I'm still seeing the same error.
Brody
Brody•3mo ago
are you sure the dist folder is going to end up in /app/apps/api?
Jay
Jay•3mo ago
mhm, yeah I checked the built image locally and the index.js file to be run is inside the /app/apps/api/dist directory.
Brody
Brody•3mo ago
chuck in some ls commands and see what that prints on railway?
Jay
Jay•3mo ago
I'm sorry but I don't know how to do that in railway? is there an article i can follow to see how to do so?
Brody
Brody•3mo ago
RUN ls -l
Jay
Jay•3mo ago
ah, gotcha in the docker image, right?
Brody
Brody•3mo ago
Dockerfile
Jay
Jay•3mo ago
yes, got it thanks! will try and get back with what shows up
Brody
Brody•3mo ago
sounds good
Solution
Jay
Jay•3mo ago
ah thanks, just found out the cause of the issue. It was a change that was pushed yesterday that resulted in dist folder not being generated in CI builds which is why it worked locally but not when it was deployed via CI.
Brody
Brody•3mo ago
awesome
Jay
Jay•3mo ago
Thanks again for all the help, appreciate it! Kinda feel bad that it is a simple fix and I took up a decent bit of your time.
Brody
Brody•3mo ago
no worries at all