简体   繁体   English

Docker容器中的Nodemon

[英]Nodemon in Docker Container

I am unable to run nodemon in a docker container. 我无法在docker容器中运行nodemon。 Here's the error I am getting: [nodemon] Internal watch failed: ENOSPC: System limit for number of file watchers reached, watch '/usr/src/app/dist' 这是我得到的错误: [nodemon] Internal watch failed: ENOSPC: System limit for number of file watchers reached, watch '/usr/src/app/dist'

Here is my image file: 这是我的图像文件:

## 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"]

Here is the compose: 这是组成:

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

and here is start:dev: 这是start:dev:

cross-env NODE_ENV=development tsc-watch -p tsconfig.build.json --onSuccess \\"nodemon dist/main.js\\"

I have tried to increase the file watchers limit like so echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p 我试图增加文件监视程序的限制,例如echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p , but I haven't figured out a way to do it (appears impossible to do so in compose/Dockerfile). echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p ,但是我还没有想办法(在compose / Dockerfile中似乎不可能这样做)。

docker container stats is fine: 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

Here is ps aux inside the docker container: 这是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

As Mentioned by @Mihae this will slow your development, but for your learning purpose, you need to modify your dockerfile, as such operation required privileged mode and is only available when your container bootup, you can modify them at build time. 就像@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"]

Build: 建立:

docker build -t myimage .

Now, do not forget to pass --privileged this is required for fs.inotify.max_user_watches to change its value. 现在,不要忘记传递--privileged这是fs.inotify.max_user_watches更改其值所必需的。

docker run --privileged -ti myimage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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