Simon
Simon

Reputation: 409

dockerized Nestjs app not running on Digital ocean App Platform

I am using DO App platform and I try to run the docker image that is the one below:

FROM node:20-alpine AS base
RUN apk add --no-cache openssl
RUN npm i -g pnpm

FROM base AS dependencies
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
COPY prisma ./prisma/
RUN pnpm install 

FROM base AS build
WORKDIR /app
COPY . .
COPY --from=dependencies /app/node_modules ./node_modules
COPY --from=dependencies /app/prisma ./prisma
RUN npx prisma generate
RUN pnpm build
RUN pnpm prune --prod

FROM base AS deploy
WORKDIR /app
COPY --from=build /app/package.json /app/pnpm-lock.yaml ./
COPY --from=build /app/dist/ ./dist/
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/prisma ./prisma
COPY scripts/run_docker.sh /app/run_docker.sh
RUN chmod +x /app/run_docker.sh

EXPOSE 8080

CMD [ "/app/run_docker.sh" ]

And here is the run_docker script:

if [ "$NODE_ENV" = "staging" ]; then
  echo "Starting in staging mode..."
  pnpm run prisma:migrate:deploy
  pnpm run start:staging
elif [ "$NODE_ENV" = "production" ]; then
  echo "Starting in production mode..."
  pnpm run prisma:migrate:deploy
  pnpm run start:prod
else
  echo "No NODE_ENV configured or unrecognized environment: $NODE_ENV"
  echo "Please set NODE_ENV to either 'staging' or 'production'."
  exit 1  # Exit with an error status code
fi

It builds perfectly fine but when I try to run it I have this error:

node:internal/modules/cjs/loader:1228
throw err;

Error: Cannot find module '/app/node_modules/.pnpm/[email protected]/node_modules/argon2/lib/binding/napi-v3/argon2.node'

The argon package is present in package.json file, but it might be important to point that it is the first package present in the list dependencies.

Upvotes: 0

Views: 21

Answers (1)

Umair Sarfraz
Umair Sarfraz

Reputation: 6079

Your docker environment is either properly not setup to build the node bindings for argon2.

You could either build the bindings on a specific version and then ship as part of your application binary. This will require you to explicitly copy the bindings binary to the correct location in node_modules.

Another way is to rebuild the binary every time you build the image. See the updated docker script.

FROM node:20-alpine AS base

# Install necessary build dependencies
RUN apk add --no-cache build-base python3 openssl

RUN npm i -g pnpm
RUN npm install -g node-pre-gyp

FROM base AS dependencies
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
COPY prisma ./prisma/
RUN pnpm install

# Rebuild the argon2 bindings using node-pre-gyp
RUN node-pre-gyp rebuild -C ./node_modules/argon2

FROM base AS build
WORKDIR /app
COPY . .
COPY --from=dependencies /app/node_modules ./node_modules
COPY --from=dependencies /app/prisma ./prisma
RUN npx prisma generate
RUN pnpm build
RUN pnpm prune --prod

FROM base AS deploy
WORKDIR /app
COPY --from=build /app/package.json /app/pnpm-lock.yaml ./
COPY --from=build /app/dist/ ./dist/
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/prisma ./prisma
COPY scripts/run_docker.sh /app/run_docker.sh
RUN chmod +x /app/run_docker.sh

EXPOSE 8080

CMD [ "/app/run_docker.sh" ]

Upvotes: 0

Related Questions