Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node v5.x.x does not exit on Ctrl-C in Docker container #4182

Closed
adam-beck opened this issue Dec 8, 2015 · 17 comments
Closed

Node v5.x.x does not exit on Ctrl-C in Docker container #4182

adam-beck opened this issue Dec 8, 2015 · 17 comments

Comments

@adam-beck
Copy link

server.js
var http = require('http');
var server = http.createServer();


server.listen(3000, function() {
  console.log('listening on port 3000...');
});

docker run --rm -it -v /home/foo/repos/test/server.js:/usr/test/server.js -w /usr/test node node server.js

You are unable to kill the server by using Ctrl-C anymore. I've tried testing this with an older version of node (0.10.41) and it seems to be working properly. I am able to use docker stop $CONTAINER_ID and it will stop the container.

May be something similar to this

@evanlucas
Copy link
Contributor

There is another issue (somewhere in the archive repo) that goes into detail on why that is the intended behavior (I'm searching for it). But, to work around, you can add listeners for those signals and then call process.exit()

@chrisdopuch
Copy link

I was able to reproduce this problem using Node 5.2.0, and I also reproduced the correct behavior with Node 0.10.41.

@evanlucas your workaround does work great, added the following to @adam-beck 's code and was able to end the process in Node 5.2.0 with Ctrl-c

process.on('SIGINT', function() {
    process.exit();
});

Docker version 1.8.2
docker-machine version 0.4.1
OSX Yosemite 10.10.5

@adam-beck
Copy link
Author

The above code worked. Thanks @evanlucas & @chrisdopuch.

@samsalisbury
Copy link

Should this issue be closed @adam-beck @evanlucas @chrisdopuch ? It seems like broken behaviour, since node usually exits on SIGINT on Mac, why should it need to have explicit handlers registered when running inside a Ubuntu-14 Docker container? It means that Node code is not portable in this regard, which seems like an odd decision if it is really intended.

@chrisdopuch
Copy link

I agree @samsalisbury, and I would also still like to see this issue resolved.

@MylesBorins
Copy link
Contributor

/cc @nodejs/ctc

@samsalisbury
Copy link

Also note that this is broken at least as early as node 4.2.3, which is what I'm using now, and probably earlier.

@misterdjules
Copy link

@evanlucas Were you looking for nodejs/node-v0.x-archive#9131, as indicated at the bottom of the issue's description (maybe edited after your comment was posted)?

@MylesBorins
Copy link
Contributor

nodejs/node-v0.x-archive#9131 (comment) seems to do a really good job of describing what is happening, and why this appears to be an issue with docker not node. The issue tracking this on docker was closed as fixed, and it seems if process.on('sigint') is firing than perhaps docker is passing the SIGINT to pid 1 as expected.

I think someone with a bit more docker understanding needs to take a peak @nodejs/docker

@evanlucas
Copy link
Contributor

Thanks @misterdjules for the link. I was thinking there was another issue, but I have been unable to find it. I agree with @thealphanerd that the description in the previous issue explains it pretty well. But, since @indutny signed off on the commit that changed this behavior, perhaps he can shed some light on this.

meekrosoft pushed a commit to meekrosoft/inventory-service that referenced this issue Sep 28, 2016
Based on this issue:

 - nodejs/node#4182

This is the proposed explanation and fix:

 - nodejs/node-v0.x-archive#9131 (comment)
petrsloup added a commit to maptiler/tileserver-gl that referenced this issue Dec 21, 2016
ahmetb added a commit to ahmetb/container-engine-samples that referenced this issue Mar 29, 2017
- Ctrl+C on a Docker container sends SIGINT, which is not handled
  properly by default (see nodejs/node#4182). Added a signal handler.
- Adding trailing newline to response for curl-friendliness.
- Added log message indicating server is starting.
- Added hostname to indicate that the container.

Signed-off-by: Ahmet Alp Balkan <[email protected]>
ahmetb added a commit to GoogleCloudPlatform/kubernetes-engine-samples that referenced this issue Mar 29, 2017
* Handle SIGINT exit, add hostname and \n to resp

- Ctrl+C on a Docker container sends SIGINT, which is not handled
  properly by default (see nodejs/node#4182). Added a signal handler.
- Adding trailing newline to response for curl-friendliness.
- Added log message indicating server is starting.
- Added hostname to indicate that the container.

Signed-off-by: Ahmet Alp Balkan <[email protected]>

* Address code review comments

Signed-off-by: Ahmet Alp Balkan <[email protected]>
@eljefedelrodeodeljefe
Copy link
Contributor

eljefedelrodeodeljefe commented May 20, 2017

This should be reopened.
Also I am pretty sure our closing behaviour is faulty in a number of places.

E.g. in C++ we should register the default handler with std::signal, since std::raise would cause implementation defined behaviour. Which though I cal rule out to be the cause of this.

Again for reference, this commit c61b0e9 was believed to cause this.

@bnoordhuis
Copy link
Member

bnoordhuis commented May 20, 2017

@eljefedelrodeodeljefe Then please file a new issue. This one is about PID 1 being special when it comes to signals, it's not a node issue.

@eljefedelrodeodeljefe
Copy link
Contributor

Will do.

awick pushed a commit to arkime/arkime that referenced this issue Dec 11, 2017
node ignores sigint when running as pid 1 in a docker container.  An explicit
sigint handler is needed in order for ^C to work or for systemd to manage the
process.  E.g. when running the viewer like ...

docker run --rm moloch /data/moloch/bin/node /data/moloch/viewer/viewer.js

See nodejs/node#4182 for details.
@lakruzz
Copy link

lakruzz commented Jun 11, 2018

Another solution, which doesn't require any change in the code, is to add --pid=host to the docker run startup arguments.

That will allow you to kill the host, with <CTRL>+C

@thelebster
Copy link

@lakruzz this worked for me. I have a running gulp watch task from the docker on macosx.
2018-06-15_08-43-30

@slavafomin
Copy link

Another solution, which doesn't require any change in the code, is to add --pid=host to the docker run startup arguments.

That will allow you to kill the host, with <CTRL>+C

This really helped me 4 years later, thanks :)

@smijar
Copy link

smijar commented May 9, 2022

Another solution, which doesn't require any change in the code, is to add --pid=host to the docker run startup arguments.
That will allow you to kill the host, with <CTRL>+C

This really helped me 4 years later, thanks :)

Thanks, brilliant!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests