LoginSignup
2
3

More than 5 years have passed since last update.

Express(node.js)をdockerでデバックする時にnodemonを使って再起動なしで動作確認する

Posted at

ちょっと面倒なのを解決したい

Express(node.js)でコンテナ立ち上げているんですが、ちょっとファイルを修正したら、

docker-compose build node_debug
docker-compose up -d

で毎回面倒。

ファイルを変更したら、勝手に変更を検知して、再起動かけてくれないかな?
しかもdockerのコンテナは立ち上がったままで。

ありました。「nodemon」

nodemonを入れる

nodemon
Nodemon is a utility that will monitor for any changes in your source and automatically restart your server. Perfect for development. Install it using npm.

インストール

コンテナで利用するので、package.jsonに記述されるように。

npm install --save nodemon

package.json抜粋

{
  "name": "express",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "nodemon ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.13.2",
    "config": "^1.16.0",
    "cookie-parser": "~1.3.5",
    "nodemon": "^1.10.2"
  }
}

Dockerfile抜粋

EXPOSE 8080
WORKDIR /home/express
RUN rm -r node_modules; npm install

CMD ["npm", "start"]

面倒解決

  • ホストからコンテナにファイルをコピーする
docker cp test.js express:/home/api/models/test.js
  • 再起動してくれる
docker logs -f express

[nodemon] restarting due to changes...
[nodemon] starting `node ./bin/www`
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3