I have a bunch of scripts that I run inside docker containers. They're called by scripts and are not meant to be interactive. The outer scripts are sometimes run in a terminal, and sometimes not (as in CI).
I want to support users killing the scripts using Ctrl+C, so I currently pass -t
(--tty
). But I discovered that this option combines stdout
and stderr
. I don't want that.
Is there a way I can support Ctrl+C and still have stderr
?
The best solution I found is tini. Specifically, add the following to the Dockerfile
:
ENV TINI_VERSION v0.18.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini
RUN chmod +x /tini
ENTRYPOINT ["/tini", "-g", "--"]
This is taken mostly verbatim from the tini README, and adds the -g
, which makes Ctrl+C work more like you would expect.
Now there's no need to use --tty
, and no smushing of stdout
and stderr
.
tini
's author does a great job explaining it. But to summarize, docker runs your process as PID 1. PID 1 is normally init, and it has some responsibilities, like handling signals. bash
, and most other programs you might run in your container, don't do those things. tini
does, and it does such a good job, they added it to docker
as --init
. The only problem with that flag is that it doesn't add -g
, which is important for shell scripts. So I recommend adding it to the Dockerfile
manually.