skip to Main Content

I’m new with docker and I’m trying to build a container with development and production version of my api.

If I run the command "docker-compose up dev" everything works fine, but when I run the production command "docker-compose up prod" I get the following error:

[+] Running 2/2
 - Network sip-api_nestjs-network  Created                                                                                                                                                                                  0.7s 
 - Container api-prod              Created                                                                                                                                                                                  0.1s
Attaching to api-prod
api-prod  | 
api-prod  | > [email protected] start:prod
api-prod  | > node dist/main
api-prod  |
api-prod  | node:internal/modules/cjs/loader:959
api-prod  |   throw err;
api-prod  |   ^
api-prod  |
api-prod  | Error: Cannot find module '/app/dist/main'
api-prod  |     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
api-prod  |     at Function.Module._load (node:internal/modules/cjs/loader:804:27)
api-prod  |     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
api-prod  |     at node:internal/main/run_main_module:17:47 {
api-prod  |   code: 'MODULE_NOT_FOUND',
api-prod  |   requireStack: []
api-prod  | }
api-prod exited with code 1

What I understand from the error is that it is not copying or creating the directories with the necessary files, but I am not sure.

This is my Dockerfile:

```
#############################
# BUILD FOR LOCAL DEVELOPMENT
#############################
FROM node:16-alpine AS development

USER node

WORKDIR /app

COPY --chown=node:node package*.json ./

RUN npm ci

COPY --chown=node:node . .

######################
# BUILD FOR PRODUCTION
######################

FROM node:16-alpine AS build

USER node

WORKDIR /app

COPY --chown=node:node package*.json ./

COPY --chown=node:node --from=development /app/node_modules ./node_modules

COPY --chown=node:node . .

RUN npm run build

ENV NODE_ENV=production

RUN npm ci --only=production && npm cache clean --force

###################
# PRODUCTION
###################

FROM node:16-alpine AS production

COPY --chown=node:node --from=build /app/node_modules ./node_modules
COPY --chown=node:node --from=build /app/views ./views
COPY --chown=node:node --from=build /app/dist ./dist

CMD [ "node", "dist/main.js" ]

This is my docker-compose.yml

version: '3.8'

services:

    dev:
        container_name: api-dev
        image: api-dev
        build:
            context: .
            target: development
            dockerfile: ./Dockerfile
        ports:
            - 3200:3200
            - 9229:9229
        networks:
            - nestjs-network
        working_dir: /app
        volumes:
            - ./:/app
            - /app/node_modules
        command: npm run start:dev
        restart: unless-stopped

    prod:
        container_name: api-prod
        image: api-prod
        build:
            context: .
            target: production
            dockerfile: ./Dockerfile
        ports:
            - 3200:3200
            - 9230:9230
        networks:
            - nestjs-network
        working_dir: /app
        volumes:
            - ./:/app
            - /app/node_modules
        command: npm run start:prod
        restart: unless-stopped

networks:
    nestjs-network:

Can anyone help me with this error? Thanks in advance

2

Answers


  1. Chosen as BEST ANSWER

    The problem was in the package.json The command npm run start:prod was pointing to dist/main when it should point to dist/src/main That was all


  2. You didn’t specify WORKDIR /app in your last build stage, all the files are being copied to root.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search