Chasen Bettinger
Chasen Bettinger

Reputation: 7464

Nodemon in Docker Container

I am unable to run nodemon in a docker container. 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'

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:

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, but I haven't figured out a way to do it (appears impossible to do so in compose/Dockerfile).

docker container stats is fine:

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:

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

Upvotes: 2

Views: 2468

Answers (1)

Adiii
Adiii

Reputation: 60046

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.

## 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.

docker run --privileged -ti myimage

Upvotes: 1

Related Questions