0

I am trying to setup Nodemon in a Docker container. It says that nodemon is running, but when I change code in my index.js file it does not reload like it does outside of docker. I've tried adding -L to the command, but no luck. I've also tried installing nodemon in the docker file instead, but no luck.

I have to do docker-compose up --build anytime I change my index.js file.

Any ideas?

Here is my file structure:

-api
  -node_modules
  -.dockerignore
  -Dockerfile
  -index.js
  -package.json
  -package-lock.json
-docker-compose.yml

docker-compose.yml:

version: '3.4'

services:
  api:
    build:
      context: ./api
    container_name: api
    environment:
      - PORT=3001
    volumes:
      - ./api/src:/usr/app/src
    ports:
      - '3001:3001'
    command: npm run dev

Dockerfile:

FROM node:14.15.2-alpine3.12

WORKDIR /usr/app

COPY package*.json ./

RUN npm install

COPY . .

package.json:

{
 "name": "api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "express": "^4.17.1"
  },
  "scripts": {
    "dev": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "nodemon": "^2.0.6"
  }
}
Connor Marble
  • 63
  • 1
  • 8
  • Are you editing the index.js from inside the Docker container? – cam Dec 18 '20 at 20:23
  • Nevermind I see your volume mount now. Try changing CMD to `command: nodemon index.js` – cam Dec 18 '20 at 20:32
  • @noisewaterphd When I changed "command: npm run dev" to ---> "command: npm run dev" I get this error: Error: Cannot find module '/usr/app/nodemon' – Connor Marble Dec 18 '20 at 20:35
  • try installing nodemon globally in a separate RUN instruction `RUN npm install -g nodemon` – cam Dec 18 '20 at 20:42
  • Can you use Node and Nodemon on the host, outside of Docker? That will probably be much simpler than trying to configure an isolation environment like Docker. Possibly also see [Nodemon Doesn't Restart in Windows Docker Environment](https://stackoverflow.com/questions/39239686/nodemon-doesnt-restart-in-windows-docker-environment) – David Maze Dec 18 '20 at 20:47
  • @noisewaterphd it gets rid of the error. But it brings me back to the initial issue. It still doesn't reload when the index.js file changes. – Connor Marble Dec 18 '20 at 20:50
  • Try running nodemon with npx. npx nodemon – Jay Garzon Dec 18 '20 at 21:11
  • @JayGarzontried tried this in both my script and my command, but it still does not reload when I save my index.js file. – Connor Marble Dec 18 '20 at 21:17
  • @DavidMaze It does work outside of docker. I see that a lot of people use docker with nodemon and don't seem to have issues. I don't know if they are on windows like me or not though. I'd like to use nodemon with Docker as one of the benefits of working with docker this way is to make sure your docker file works when it is time to deploy. – Connor Marble Dec 18 '20 at 21:21

1 Answers1

3

I FIGURED IT OUT!!!

After a lot of trial and error. It has to do with my volumes in my docker compose as well as nodemon. Not 100% sure why any insight would be helpful too.

The fix was to change my volume from

- ./api/src:/usr/app/src

to:

- ./api:/usr/src/app

Then I had to add the -L flag to my nodemon command in order for it to reload.

Connor Marble
  • 63
  • 1
  • 8