简体   繁体   中英

How do you setup Nodemon in a Docker container?

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"
  }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM