node.js - Docker容器中的Nodemon

标签 node.js docker docker-compose nestjs

我无法在docker容器中运行nodemon。这是我得到的错误:[nodemon] Internal watch failed: ENOSPC: System limit for number of file watchers reached, watch '/usr/src/app/dist'
这是我的图像文件:

## Use specific version of node
FROM node:10.16

## Get anything we may need for our container and run updates
RUN apt-get update -qq && apt-get install -y build-essential

## CREATE DIRECTORY
WORKDIR /usr/src/app

# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package.json ./
COPY yarn.lock ./

## Install packages
RUN yarn install

# BUNDLE APP SOURCE
COPY . .

# EXPOSE TARGET PORT
EXPOSE 3001

CMD ["yarn", "start:dev"]

这是组成:
    build: ./server/
    volumes:
      - ./server/:/usr/src/app
      - /usr/src/app/node_modules
    ports: 
      - "3001:3001"
    restart: always
    depends_on:
      - db

这是start:dev:
cross-env NODE_ENV=development tsc-watch -p tsconfig.build.json --onSuccess \"nodemon dist/main.js\"
我曾尝试像echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p这样增加文件监视程序的限制,但我还没有找到一种方法来做到这一点(在compose / Dockerfile中似乎无法做到这一点)。
docker container stats很好:
R ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS

4050a37b3352        server     8.87%               324.6MiB / 15.57GiB   2.04%               79.1kB / 6.87kB     48MB / 1.26MB       50

这是docker容器内的ps aux:
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.3 760916 57112 ?        Ssl  03:24   0:00 node /opt/yarn-v1.17.3/bin/yarn.js start:dev
root        29  0.0  0.0   4280   488 ?        S    03:24   0:00 /bin/sh -c cross-env NODE_ENV=development tsc-watch -p tsconfig.build.json --onSuccess "nodemon dist/main.js"
root        30  0.0  0.1 561684 21880 ?        Sl   03:24   0:00 /usr/local/bin/node /usr/src/app/node_modules/.bin/cross-env NODE_ENV=development tsc-watch -p tsconfig.build.json --onSuccess nodemon dist/
root        37  0.0  0.1 561768 23564 ?        Sl   03:24   0:00 /usr/local/bin/node /usr/src/app/node_modules/.bin/tsc-watch -p tsconfig.build.json --onSuccess nodemon dist/main.js
root        46 12.9  1.2 781216 196912 ?       Sl   03:24   2:09 /usr/local/bin/node /usr/src/app/node_modules/typescript/bin/tsc -p tsconfig.build.json --watch
root        70  0.0  0.0      0     0 ?        Z    03:24   0:00 [sh] <defunct>
root        71  0.3  0.5 765520 96992 ?        Sl   03:24   0:03 /usr/local/bin/node dist/main dist/main.js
root        82  0.0  0.0  18180  2924 pts/0    Ss+  03:25   0:00 bash
root        92  6.0  0.0  18180  3064 pts/1    Ss   03:40   0:00 bash
root        99  0.0  0.0  36632  2828 pts/1    R+   03:40   0:00 ps aux

最佳答案

就像@Mihae提到的那样,这会减慢您的开发速度,但是出于学习目的,您需要修改dockerfile,因为该操作需要privileged模式,并且仅在容器启动时可用,您可以在构建时进行修改。

## Use specific version of node
FROM node:10.16
## Get anything we may need for our container and run updates
RUN apt-get update -qq && apt-get install -y build-essential
## Install packages
RUN yarn install


#creating entrypoint script with some debug log you can remove after debuging
RUN echo "#!/bin/sh \n\
echo "fs.inotify.max_user_watches before update" \n\
cat /etc/sysctl.conf\n\
echo "______________________updating inotify __________________________" \n\
echo fs.inotify.max_user_watches=524288 | tee -a /etc/sysctl.conf && sysctl -p \n\
echo "updated value is" \n\
cat /etc/sysctl.conf | grep fs.inotify \n\
exec yarn start:dev \
" >> /usr/local/bin/entrypoint.sh

RUN chmod +x /usr/local/bin/entrypoint.sh
# EXPOSE TARGET PORT
EXPOSE 3001
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

建立:
docker build -t myimage .

现在,不要忘记传递--privileged,这是fs.inotify.max_user_watches更改其值所必需的。
docker run --privileged -ti myimage

关于node.js - Docker容器中的Nodemon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57798121/

相关文章:

docker - 如何从 Docker 容器访问主机中的文件?

amazon-web-services - 是否可以在 EC2 实例上执行 AWS CLI 命令而不在 EC2 上放置 AWS 凭证?

docker - 如何在没有互联网访问的情况下使用 Helm chart

java - Dockerfile 构建提高了不足范围 : authorization failed with public openjdk image

mysql - 如何通过解释 docker-compose.yml 中的卷来定位备份的主机存储?

javascript - npm init 中的 "entry point"是什么

node.js - 使用 module.exports 的 NodeJS 回调

python - Docker-compose 日志仅显示 "Attaching to"而没有其他内容

node.js - Loopback JS模型与MongoDB的连接

javascript - socket.on() 未接收数据