2

I am a docker beginner and trying to dockerize simple projects as part of my learning journey.. I am trying to dockerize a NestJS project, but the docker-compose up command fails.

My Dockerfile and docker-compose.* files look as below. I have tried various changes to Dockerfile and docker-compose files but with no luck.. Can some spot the issue or something that I am missing.

Dockerfile

FROM node:12.10

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

RUN npm build

docker-compose.yml

version: '3'
services:
  app:
    build: .
    ports:
      - '8081:8081'

docker-compose.dev.yml

version: '3'
services:
  app:
    command: npm start
    volumes:
      - .:/usr/src/app
    environment:
      - NODE_ENV=development

And, here's the command I run...

docker-compose -f docker-compose.yml -f docker-compose.dev.yml up

The command fails with below error

Attaching to nestjs-api-server_app_1
app_1  |
app_1  | > BookAPI@0.0.1 start /usr/src/app
app_1  | > npm run copy-resources && nodemon --watch src -e ts,tsx --exec ts-node src/main.ts
app_1  |
app_1  |
app_1  | > BookAPI@0.0.1 copy-resources /usr/src/app
app_1  | > ts-node scripts/copy-resources.ts
app_1  |
app_1  | sh: 1: ts-node: not found
app_1  | npm ERR! code ELIFECYCLE
app_1  | npm ERR! syscall spawn
app_1  | npm ERR! file sh
app_1  | npm ERR! errno ENOENT
app_1  | npm ERR! BookAPI@0.0.1 copy-resources: `ts-node scripts/copy-resources.ts`
app_1  | npm ERR! spawn ENOENT
app_1  | npm ERR!
app_1  | npm ERR! Failed at the BookAPI@0.0.1 copy-resources script.
app_1  | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
app_1  | npm WARN Local package.json exists, but node_modules missing, did you mean to install?
app_1  |
app_1  | npm ERR! A complete log of this run can be found in:
app_1  | npm ERR!     /root/.npm/_logs/2020-02-27T20_07_39_822Z-debug.log
app_1  | npm ERR! code ELIFECYCLE
app_1  | npm ERR! errno 1
app_1  | npm ERR! BookAPI@0.0.1 start: `npm run copy-resources && nodemon --watch src -e ts,tsx --exec ts-node src/main.ts`
app_1  | npm ERR! Exit status 1
app_1  | npm ERR!
app_1  | npm ERR! Failed at the BookAPI@0.0.1 start script.
app_1  | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
app_1  | npm WARN Local package.json exists, but node_modules missing, did you mean to install?
app_1  |
app_1  | npm ERR! A complete log of this run can be found in:
app_1  | npm ERR!     /root/.npm/_logs/2020-02-27T20_07_39_847Z-debug.log
nestjs-api-server_app_1 exited with code 1

Guna
  • 21
  • 1
  • 2
  • So we know that node_modules is missing... could it be that you are running `npm install` on build and then you're mounting your local directory to the same path – essentially overwriting the entire directory (and removing your node_modules dir)? Try removing your volume mount (lines 5 and 6) from docker-compose.dev.yml then `docker-compose up --build` – Andy Mardell Feb 27 '20 at 21:25
  • 1
    yeah, that seems to be the problem. Nice catch. Thank you Andy. – Guna Feb 28 '20 at 05:21

1 Answers1

2

Hey i tried this solution and it worded with me, i hope it works with you as well https://stackoverflow.com/a/50454232/13549437

The basic idea is that we are going to invoke the ts-node from node_modules folder rather than from the local /bin folders (the global commands folder).

  1. Install ts-node and typescript npm i ts-node typescript

  2. The start script in the package.json "start": "./node_modules/.bin/ts-node index.ts"

  3. The run script in the docker file RUN npm install

Ahmed Abdalla
  • 102
  • 1
  • 4