Skip to content

Commit 82f25e8

Browse files
committed
perl: Document PID 1 signal handling behavior
Describe how Perl behaves when being run as PID 1 under containers, particularly on receiving SIGTERM/SIGINT, showing example for explicit signal handling. Ref Perl/docker-perl#44
1 parent a3e66b1 commit 82f25e8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

perl/content.md

+26
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@ For many simple, single file projects, you may find it inconvenient to write a c
3232
$ docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp %%IMAGE%%:5.20 perl your-daemon-or-script.pl
3333
```
3434

35+
## Signal handling behavior notice
36+
37+
As Perl will run as PID 1 by default in containers (unless an [ENTRYPOINT](https://docs.docker.com/engine/reference/builder/#entrypoint) is set,) special care needs to be considered when expecting to send signals (particularly SIGINT or SIGTERM) to it. For example, running
38+
39+
```console
40+
$ docker run -it --name sleeping_beauty --rm %%IMAGE%%:5.20 perl -E 'sleep 300'
41+
```
42+
43+
and doing on another terminal,
44+
45+
```console
46+
$ docker exec sleeping_beauty kill 1
47+
```
48+
49+
will *not* stop the perl running on the `sleeping_beauty` container (it will keep running until the `sleep 300` finishes.) To do so, one must set a signal handler like this:
50+
51+
```console
52+
$ docker run -it --name quick_nap --rm %%IMAGE%%:5.20 perl -E '$SIG{TERM} = sub { say "recv TERM" }; sleep 300'
53+
```
54+
55+
so doing `docker exec quick_nap kill 1` (or the simpler `docker stop quick_nap`) will immediately stop the container, and print `recv TERM` in the other terminal.
56+
57+
If your Perl program is expected to handle signals and fork child processes, it is encouraged to use an init-like program for ENTRYPOINT, such as [dumb-init](https://github.com/Yelp/dumb-init) or [tini](https://github.com/krallin/tini) (the latter is available since Docker 1.13 via the `docker run --init` flag.)
58+
59+
See also [Signals in perlipc](https://perldoc.pl/perlipc#Signals) as well as [Perl/docker-perl#44](https://github.com/Perl/docker-perl/issues/44).
60+
3561
## Example: Creating a reusable Carton image for Perl projects
3662

3763
Suppose you have a project that uses [Carton](https://metacpan.org/pod/Carton) to manage Perl dependencies. You can create a `%%IMAGE%%:carton` image that makes use of the [ONBUILD](https://docs.docker.com/engine/reference/builder/#onbuild) instruction in its `Dockerfile`, like this:

0 commit comments

Comments
 (0)