【问题标题】:Nodemon in Docker ContainerDocker 容器中的 Nodemon
【发布时间】:2020-01-07 22:07:05
【问题描述】:

我无法在 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

【问题讨论】:

  • 你不需要看'/usr/src/app/dist它的静态文件并且服务器不需要在这样的变化上重新启动
  • @Adiii dist 不是静态文件,它正在被 tsc-watch 监视并在更改后重新编译
  • 如果我没记错的话,我们在 ng build 后得到了dist,我们不在 dist 上工作,但它生成了?
  • @Adiii 我不确定我是否理解。我没有在任何地方运行 ng build。
  • 您可能会找到解决方案,但您很快就会意识到 docker 会大大减慢开发速度。 Docker 非常适合发布您的应用程序并在不同的上下文中一致地运行它们。但是在开发方面,我建议您在主机上进行,而不要使用 Docker。您的问题与 docker 机器(docker 实际运行的底层虚拟机)有关。如果我是对的,你需要在那里更改设置,你不能只使用 Dockerfile 来做到这一点。

标签: node.js docker docker-compose nestjs


【解决方案1】:

正如@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

【讨论】:

  • 感谢您的回答。我一会儿试试这个! :)
  • 很高兴,如果有任何问题,请告诉我:)
猜你喜欢
  • 2018-06-27
  • 2021-03-29
  • 2020-04-09
  • 2019-12-27
  • 2020-11-02
  • 2021-09-29
  • 1970-01-01
  • 2023-03-26
  • 2021-10-21
相关资源
最近更新 更多