nodemon: nodemon with babel-node in docker address already in use

I configured a simple app in express to run in a docker container but whenever I make a change nodemon gives me an error saying that the port is already in use.

Error: listen EADDRINUSE: address already in use :::4000

That is my start script: nodemon -L --exec babel-node -- src/index

"nodemon": "^1.18.7"

and my index.js:

import express from 'express'

const app = express();

app.get("/", (req, res) => {
  res.send("hello world");
});

app.listen(4000, () => console.log("The server is listening on port 4000"));

It’s like it’s not closing the process and it’s trying to execute again

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 13
  • Comments: 50 (6 by maintainers)

Most upvoted comments

I’m having the same issue when running with docker-compose. However, adding --signal SIGINT to the nodemon command seems to fix it.

Live fix in nodemon@1.18.8 (core change in pstree.remy…again).

I face the same issue and I believe somehow it is a Docker + nodemon --exec flag issue

In the following package.json, if you replace:

  ...
  "start:dev": "nodemon --watch './src' --exec 'npm start'"
  ...

by:

  ...
  "start:dev": "nodemon --watch './src'"
  ...

it works perfectly under docker. Note that both work fine under OSX.

Here is the test case:

src/index.js

const express = require('express');
const app = express();
app.listen(17600, () => {
  console.log('Server MediaStreamTrackAudioSourceNode, Olé !');
});

package.json

{
  "name": "nodemon-test",
  "version": "1.0.0",
  "description": "",
  "main": "./src/index.js",
  "scripts": {
    "start": "node .",
    "start:dev": "nodemon --watch './src' --exec 'npm start'"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.4"
  },
  "devDependencies": {
    "nodemon": "^1.18.7"
  }
}

Dockerfile

FROM node:10-alpine

WORKDIR /app

COPY ./src /app/src
COPY ./package.json /app/package.json
COPY ./package-lock.json /app/package-lock.json

RUN npm install

CMD [ "npm", "start" ]

docker-compose.yml

version: '3.7'

services:
  node:
    build: .
    command: npm run start:dev
    ports:
      - 17600:17600
    volumes:
      - ./src:/app/src
      - ./package.json:/app/package.json

Note: I need --exec because I pipe the output in the bunyan parser like this --exec 'npm start' | bunyan in real use case.

To add some more details on this issue, I’ve been using nodemon to hot reload my API inside docker for the past 2 months. It was worked extremely well with no issues. After I upgraded I began to experience OPs issue.

docker-compose version 1.23.2, build 1110ad01
Docker version 18.09.0, build 4d60db4
Alpine 3.8
Node v8.11.4
Nodemon version 1.18.7

Modifying my command to use –signal SIGINT as suggested above didn’t solve the issue.

However, I tested my API outside of docker with and without –signal SIGINT and it reloads correctly in both scenarios.

I can’t debug much further right now but hopefully this bit of info can help.

I’m facing the same issue. This has started after I upgraded from 1.18.4 to 1.18.7. I think it could be related to #1476.

Also I’m controlling the shutdown of my server with the approach described at https://github.com/remy/nodemon#controlling-shutdown-of-your-script but I don’t seem to ever receive SIGUSR2.

One more thing, if I start and immediately change a file I can clearly see the logs of 2 servers starting, the first one with success and the second one failing due to EADDRINUSE. Quite clearly the process is not shutdown.

I’ve tried to debug but I can’t seem to find much, not familiar with the nodemon codebase. @remy is there a build process if I change the source or I can just push the code to my fork and npm install from there?

Just add --delay 300ms into the command solved my problem.

https://github.com/remy/nodemon#delaying-restarting

Anyone else from the future visiting this, I had to make sure my volume was mounted in docker-compose.yml to get nodemon to find the files.

Same error using linux, docker, yarn and ts-node.

Dockerfile:

FROM node:8.11.3-alpine
...
CMD ["yarn", "run", "dev"]

package.json:

"scripts": {
    "dev": "NODE_ENV=development nodemon --exec ts-node src/server.ts",
},
"devDependencies": {
    ....
    "nodemon": "^1.18.7",
    "ts-node": "^7.0.1",
    ...

Versions:

Alpine 3.8
Node v8.11.4
Nodemon version 1.18.7

Error in terminal:

...
server: [nodemon] restarting due to changes...
server: [nodemon] starting `ts-node src/server.ts`
server: Starting server...
server: Error: listen EADDRINUSE :::4000
...

But if I exec into the running container I can’t find the blocking process:

docker-composer exec server sh
fuser 4000/tcp
# No Output

Let’s wait for @remy to see what he has to say about that, the next steps.

I encountered this issue trying to get nodemon to restart a development server in a docker container and after debugging it I discovered the issue was the pstree.remy library wasn’t able to generate a list of the child process subtree for termination so it only terminates the top level child (the shell which launches node) and orphans the actual node server. This happens because:

  1. the ps util wasn’t installed by default in my node:lts-slim docker image.
  2. the user the docker process runs as did not have permission to stats /proc (which is the fallback for when ps is unavailable).

The solution for me was to install ps in my docker image:

# install ps util needed by nodemon
RUN apt-get update && apt-get install -y procps && rm -rf /var/lib/apt/lists/*

@timini not a real one. For now, just use the version 1.13.3 all version after won’t work. Or if you use yarn you can add this in your package.json :

"resolutions": {
   "nodemon/pstree.remy": "1.1.0"
 }

As mention in https://github.com/remy/nodemon/issues/1484

@tibotiber I tried to reinstall the version 1.18.4 but it didn’t solve the issue. But then I rollbacked to the version 1.12.1 and voilà ! I worked like a charm. So as a temporary fix you can do:

npm rm nodemon
npm i nodem@1.12.1 -D

Note: I didn’t checked all versions between 1.12.1 and 1.18.4

@20k-ultra To my understanding under OSX docker is not using virtualbox with vboxsf since a long time so the -L to watch the filesystem is useless in this case. I got no idea if it is still required on Windows though… But this is not relevant to the issue since the -L is use to make sure nodemon catches the file changes and tries to restart. Here it tries to restart but fail trying.

@richardkall It’s still not working for me, I noticed that you are not using the legacy watch, for me if I don’t use nodemon doesn’t work at all, specially now setting --signal even with the legacy watch it’s not working either.

[nodemon] restarting due to changes… [nodemon] restarting due to changes… [nodemon] restarting due to changes…

btw: my settings are not that different from yours.

I noticed that it only works without babel-node, if I change:

import express from 'express'

to:

const express = require('express')

and use this as script:

"start": "nodemon -L src/index"

It works, it doesn’t however when using --exec and babel-node