Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to watch file changes developing in NestJs app inside Docker container

I'm having a problem with NestJS and Docker. I want to run the development script via npm start: dev but the problem is that the app runs ok but it dosn't detect any change in the souce files, so I cant use it to develop my app.

This is part of my docker-compose.yml:

messages:
   image: c2c/messages:v1
   command: npm run start:dev
   build:
     context: ./services/c2c-server-messages
     dockerfile: Dockerfile
   container_name: c2c_server_messages
   depends_on:
     - postgres
     - nginx
   networks:
     c2c_net:
       ipv4_address: 172.28.1.5

And the Dockerfile of my Nest app:

FROM node:10-alpine
WORKDIR /api/messages
ADD . .
RUN npm install
EXPOSE 3000
CMD ["npm", "run", "start"]

My npm start:dev script in package.json:

"start:dev": "tsc-watch -p tsconfig.build.json --onSuccess \"node dist/main.js\"",

The console output show me this, but the app dosn't detect the file changes:

c2c_server_messages | 7:26:29 PM - Found 0 errors. Watching for file changes.
c2c_server_messages | [Nest] 36   - 07/31/2019, 7:26 PM   [NestFactory] Starting Nest application...
c2c_server_messages | [Nest] 36   - 07/31/2019, 7:26 PM   [InstanceLoader] TypeOrmModule dependencies initialized +63ms
c2c_server_messages | [Nest] 36   - 07/31/2019, 7:26 PM   [InstanceLoader] AppModule dependencies initialized +2ms
c2c_server_messages | [Nest] 36   - 07/31/2019, 7:26 PM   [InstanceLoader] TypeOrmCoreModule dependencies initialized +151ms
c2c_server_messages | [Nest] 36   - 07/31/2019, 7:26 PM   [InstanceLoader] TypeOrmModule dependencies initialized +1ms
c2c_server_messages | [Nest] 36   - 07/31/2019, 7:26 PM   [InstanceLoader] MessageModule dependencies initialized +2ms
c2c_server_messages | [Nest] 36   - 07/31/2019, 7:26 PM   [RoutesResolver] AppController {/}: +8ms
c2c_server_messages | [Nest] 36   - 07/31/2019, 7:26 PM   [RouterExplorer] Mapped {/, GET} route +6ms
c2c_server_messages | [Nest] 36   - 07/31/2019, 7:26 PM   [RoutesResolver] MessageController {/messages}: +1ms
c2c_server_messages | [Nest] 36   - 07/31/2019, 7:26 PM   [RouterExplorer] Mapped {/, POST} route +2ms
c2c_server_messages | [Nest] 36   - 07/31/2019, 7:26 PM   [RouterExplorer] Mapped {/all, GET} route +2ms
c2c_server_messages | [Nest] 36   - 07/31/2019, 7:26 PM   [NestApplication] Nest application successfully started +5ms
like image 330
Matias Benedetto Avatar asked Jul 31 '19 19:07

Matias Benedetto


People also ask

What does Docker compose Depends_on do?

Docker Compose depends_on Docker will pull the images and run the containers based on the given dependencies. So, in this case, the Postgres container is the first in the queue to run. However, there are limitations because depends_on doesn't explicitly wait for dependencies to be ready.

Can you run multiple apps in a Docker container?

It's ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. You can connect multiple containers using user-defined networks and shared volumes.

What Docker compose up does?

The docker compose up command aggregates the output of each container (like docker compose logs --follow does). When the command exits, all containers are stopped. Running docker compose up --detach starts the containers in the background and leaves them running.


1 Answers

I have it working now :)

The key is to create a volume in docker-compose with the local machine root folder of your microservice folder, in this case ./services/c2c-server-messages as the root of the app in the container :/api/messages

In this way, you are using your local files to run the app and not the files copied to the container, so you can watch for the changes when you save a file.

Example:

  messages:
   image: c2c/messages:v1
   volumes:
    - ./services/c2c-server-messages:/api/messages
   command: npm run start:dev
   build:
     context: ./services/c2c-server-messages
     dockerfile: Dockerfile
   container_name: c2c_server_messages
   depends_on:
     - postgres
     - nginx
   networks:
     c2c_net:
       ipv4_address: 172.28.1.5
like image 200
Matias Benedetto Avatar answered Sep 24 '22 02:09

Matias Benedetto