I have this Dockerfile that is supposed to run my nest.js project:
# syntax:docker/dockerfile:1
#base
FROM node:16.14.2-alpine AS base
ENV NODE_ENV=production
WORKDIR /app
RUN npm i -g [email protected] @nestjs/cli
COPY ["./package.json", "./"]
#dev
FROM base AS dev
RUN npm i
COPY . .
CMD ["npm", "run", "start:dev"]
#prod
FROM base AS prod
COPY . .
RUN npm i --frozen-lockfile --production
CMD ["npm", "run", "start:prod"]
and when I run it with this docker-compose.yml:
version: '3.9'
services:
backend: #THE BACKEND SIDE OF OUR APP ITSELF :
build:
context: ./back
#dockerfile: ./back/Dockerfile
target: dev #for multi stage
env_file:
- ./env/back.env
ports:
- 2000:2000
volumes:
- ./back:/app
networks:
- backend
depends_on:
- database
restart: unless-stopped
database: #THE POSTGRESQL DATABASE
image: postgres:latest
restart: unless-stopped
ports:
- 5432:5432
# env_file:
# - ./env/postgres.env
environment:
POSTGRES_USER: transcendeur
POSTGRES_PASSWORD: bigData
POSTGRES_DB: transcendb
networks:
- backend
volumes:
- postgres:/var/lib/postgresql/data
databaseadmin: #THE DATABASE 'ADMIN GRAPHICAL INTERFACE'
image: dpage/pgadmin4:latest
restart: unless-stopped
ports:
- 5050:80
env_file:
- ./env/pgadmin.env
environment:
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: dimnagp4
networks:
- backend
volumes:
- ./docker/pgadmin_servers.json:/pgadmin4/servers.json
depends_on:
- database
logging:
driver: none
# frontend: #THE FRONTEND SIDE OF OUR APP ITSELF :
# build:
# dockerfile: front/Dockerfile
# env_file:
# - ./env/front.env
# ports:
# - 8080:8080
# restart: unless-stopped
# networks:
# - backend
# volumes:
# - ./front:/app
# depends_on:
# - backend
volumes:
postgres:
back:
networks:
backend:
driver: bridge
Everything builds fine until npm run start:dev
, at compilation my files in src
folder can't resolve any modules (for example @nestjs/common
is not found)
Here's my package.json:
{
"name": "back",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "DB_ENVFILE=db nest start",
"start:dev": "DB_ENVFILE=db nest start --watch",
"start:debug": "DB_ENVFILE=db nest start --debug --watch",
"start:prod": "DB_ENVFILE=db node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "DB_ENVFILE=db jest",
"test:watch": "DB_ENVFILE=db jest --watch",
"test:cov": "DB_ENVFILE=db jest --coverage",
"test:debug": "DB_ENVFILE=db node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "DB_ENVFILE=db jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/cli": "^9.0.0",
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.2.0",
"@nestjs/core": "^9.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/swagger": "^6.1.3",
"@nestjs/typeorm": "^9.0.1",
"pg": "^8.8.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",
"swagger-ui-express": "^4.5.0",
"typeorm": "^0.3.10"
},
"devDependencies": {
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.13",
"@types/jest": "28.1.8",
"@types/multer": "^1.4.7",
"@types/node": "^16.0.0",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "28.1.3",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "28.0.8",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "4.1.0",
"typescript": "^4.7.4"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^. \\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
I'm stuck on this since this morning and I can't find any solution on other posts, my Dockerfile looks fine I don't see where could be the problem
EDIT: I forgot to say that it works perfectly fine without docker when running the same commands
The error message I get is the same as if I did npm run start::dev
without the node_modules folder
CodePudding user response:
You do copy first the package.json
and later your whole code into the working directory (/app
) of your image in your Dockerfile
and you run npm i
in that directory while building.
But in your docker-compose.yml
you bind-mount the ./back/
directory over the container's /app/
which makes the previous npm install
irrelevant since now the contents of ./back/
are in that directory and not the ones baked into the image.
So as long as there is no node_modules
directory on your host ./back/
the modules can not be found.