简体   繁体   中英

How do I use nodemon with docker in my node.js application?

I am new to Docker and coding. I have already added it as dev-dependency but still I have to build the image every time I make a change to the code. I have tried looking this up but have not found a solution that is suitable/working because I am using process.json file.

My Dockerfile :

FROM node:12.14.1-alpine

 # app name
ENV APP_NAME=mock-api
ENV WORK_DIR /deploy/${APP_NAME}

 # Create app directory
RUN mkdir -p ${WORK_DIR} && \
    chown node:node ${WORK_DIR}

RUN apk add --update gnupg

WORKDIR ${WORK_DIR}

COPY ["yarn.lock", "package.json", "./"]
RUN yarn global add pm2 && yarn install --prod --frozen-lockfile && yarn cache clean

COPY --chown=node:node . .

EXPOSE 3000

USER node

CMD ["pm2-runtime", "--no-daemon", "--raw", "process.json"]

Process.json :

{
  "apps": [
    {
      "name": "mock-api",
      "script": "./app.js"
    }
  ]
}

package.json:

{
  "name": "mock-api",
  "version": "1.0.0",
  "description": "Mock API for the testing environment",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "arorasannidhya@gmail.com",
  "license": "ISC",
  "dependencies": {
    "koa": "2.12.0",
    "koa-joi-router": "^6.0.2",
    "koa-logger": "3.2.1",
    "koa-router": "9.0.1",
    "openpgp": "4.10.4",
    "pm2": "^4.2.3",
    "uuid": "^7.0.2"
  },
  "devDependencies": {
    "nodemon": "^2.0.4"
  }
}

Nodemon is the utility to run the node application (it watches file in the directory and when changed runs index.js or app.js (whatever is your root file))

It cannot be used to build a docker image, you will need to do something like this https://vsupalov.com/rebuilding-docker-image-development/#:~:text=In%20Conclusion,see%20the%20results%20right%20away !

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