For me the only thing that worked was using **exec** to run the main container process.
Example setup with init script:
Dockerfile:
```Dockerfile
CMD init.sh
```
init.sh inside the container:
```
#!/usr/bin/env bash
...
exec my-long-running-script
```
Key is to use `exec my-long-running-script` instead of simply `my-long-running-script`.
`exec` works by replacing current program in the current process, without `fork`ing a new process. Then your target script ends up receiving signals, instead of the init script, and it should gracefully exit.
For my setup, none of the other options worked: `docker run` with `--init` option, or manually installing [tini](https://github.com/krallin/tini), or using `Ctrl+\`.
See [this blogpost](https://medium.com/@letientai299/handle-docker-stop-properly-for-containers-that-start-with-shell-script-2ef4a1941e35) (not mine) for slightly deeper explanation on how this works.
For me the only thing that worked was using **exec** to run the main container process.
Example setup with init script:
Dockerfile:
```Dockerfile
CMD init.sh
```
init.sh inside the container:
```
#!/usr/bin/env bash
...
exec my-long-running-script
```
Key is to use `exec my-long-running-script` instead of simply `my-long-running-script`.
`exec` works by replacing current program in the current process, without `fork`ing a new process. Then your target script ends up receiving signals, instead of the init script, and it should gracefully exit.
For my setup, none of the other options worked: `docker run` with `--init` option, or manually installing [tini](https://github.com/krallin/tini), or using `Ctrl+\`.
See [this blogpost](https://medium.com/@letientai299/handle-docker-stop-properly-for-containers-that-start-with-shell-script-2ef4a1941e35) (not mine) for slightly deeper explanation on how this works.