Issue when deploying node app using docker

litcodeurPRO

a year ago

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!

0 Replies

litcodeurPRO

a year ago

033f39f1-9797-405f-88f8-343ca4ac4254


litcodeurPRO

a year ago

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.


a year ago

Please send the dockerfile and the error that is thrown


litcodeurPRO

a year ago

Hey, thanks for the quick reply! Is there a private portal that I can send it through?


litcodeurPRO

a year ago

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"]

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: []

}

a year ago

just realized I don't have any sensitive info in the docker file
i would sure hope so 😆


a year ago

can you try removing the WORKDIR /app/apps/api line


litcodeurPRO

a year ago

sure, I'll try it and be back in a bit! appreciate the quick responses!


litcodeurPRO

a year ago

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.


a year ago

are you sure the dist folder is going to end up in /app/apps/api?


litcodeurPRO

a year 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.


a year ago

chuck in some ls commands and see what that prints on railway?


litcodeurPRO

a year 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?


a year ago

RUN ls -l


litcodeurPRO

a year ago

ah, gotcha in the docker image, right?


a year ago

Dockerfile


litcodeurPRO

a year ago

yes, got it


litcodeurPRO

a year ago

thanks! will try and get back with what shows up


a year ago

sounds good


litcodeurPRO

a year 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.


a year ago

awesome


litcodeurPRO

a year 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.


a year ago

no worries at all


Issue when deploying node app using docker - Railway Help Station