open-cli-tools / concurrently

Run commands concurrently. Like `npm run watch-js & npm run watch-less` but better.
https://www.npmjs.com/package/concurrently
MIT License
7.15k stars 234 forks source link

Teardown command? #472

Closed FezVrasta closed 3 months ago

FezVrasta commented 7 months ago

I'm trying to run a docker container along with a node.js server, and I would like to stop the container when the user stops the node.js server script.

"dev": "concurrently 'node server.js' 'docker start -i database'"

Unfortunately Docker doesn't properly shuts down the container when CTRL+C is pressed, even though it does it when I run the docker command directly.

Would it be possible to add a --teardown option that allows to specify a script to run when the process is interrupted (CTRL+C is pressed)?

"dev": "concurrently --teardown 'docker kill database' 'node server.js' 'docker start -i database'"
gustavohenke commented 7 months ago

Hey, thanks for the report. I'm interested in this bit:

Docker doesn't properly shuts down the container when CTRL+C is pressed, even though it does it when I run the docker command directly.

Does it stop the container when you press ctrl+c? If yes - I think it's probably best if we fixed concurrently, instead of adding another option.

Testing with a mysql image, it ignores every ctrl+c when in interactive mode.

FezVrasta commented 7 months ago

CleanShot 2024-04-25 at 2  43 57@2x

I see the container is properly shut down on CTRL+C on macOS. I'm using the postgres image though.

gustavohenke commented 4 months ago

Investigated this for a bit.

I imagine docker is adapting its behaviour when it's not running from a TTY, in which case to stop the container/clean shutdown, the options are

  1. Fake a TTY output. This is probably going to affect other commands in unexpected ways.
  2. Introduce a teardown command. It's new functionality, so not affecting any users. Some interactions need thinking (prefixing, whether it affects exit code, etc)

Sounds like a teardown command is the best option.