Run docker-compose up --build.
Make changes to index.js and see the server restart.
| node_modules |
| node_modules |
| version: '3' | |
| services: | |
| app: | |
| build: | |
| context: . | |
| dockerfile: Dockerfile.dev | |
| volumes: | |
| - ./:/usr/src/app | |
| - /usr/src/app/node_modules # Remove this if you have pure JS dependencies | |
| ports: | |
| - "3000:3000" |
| FROM node:16-alpine | |
| # Create app directory | |
| RUN mkdir -p /usr/src/app | |
| WORKDIR /usr/src/app | |
| # Install dependencies | |
| COPY package.json . | |
| RUN npm install | |
| # Bundle app source | |
| COPY index.js ./ | |
| # Exports | |
| EXPOSE 3000 | |
| CMD [ "npm", "run", "start.dev" ] |
| 'use strict' | |
| const express = require('express') | |
| const { PORT = '3000' } = process.env | |
| const app = express() | |
| app.use((req, res, next) => { | |
| res.send('Hello Jack') | |
| }) | |
| app.listen(PORT) |
| { | |
| "main": "index.js", | |
| "scripts": { | |
| "start": "node index", | |
| "start.dev": "nodemon" | |
| }, | |
| "dependencies": { | |
| "express": "^4.17.2" | |
| }, | |
| "devDependencies": { | |
| "nodemon": "^2.0.15" | |
| } | |
| } |
Even using --poll it doesn't auto restart.
Using ts-node with docker it have never auto restarted in my api.
I was able to make this gist work on my machine with the following changes:
package.json
{
"main": "index.js",
"scripts": {
"start": "node index",
"start.dev": "ts-node-dev index"
},
"dependencies": {
"express": "^4.17.2"
},
"devDependencies": {
"nodemon": "^2.0.15",
"ts-node-dev": "^1.1.8"
}
}More complex environments may require more tweaking. It may also matter what version of docker you're using and your operating system. I'm currently using:
macOS 12.1 21C52 arm64
Docker version 20.10.11, build dea9396
The -L worked for me thanks!
For those who are having the issue of it not updating you must start nodemon with the -L flag to catch file changes
That worked for thank you ❤️
-L alone didn't help me. adding specific config file nodemon.json or config within package.json can help
{
"ext": "js,json,ts",
"watch": ["src"],
"ignore": ["node_modules/**/node_modules"],
"exec": "ts-node ./src/index.ts",
"verbose": true,
"legacyWatch": true,
"env": {
"NODE_ENV": "development"
}
}
@guilhermealbino33 If you're using
ts-node-devyou don't neednodemon. So your script might look something like this:If that doesn't auto restart on file changes, it looks like it takes a
--pollflag:But that does make it much more CPU heavy since it's reading the container file system every so often to detect changes.