How to use timeout in dockerfile with alpine base?
Many programs drop default signal handlers when running as PID 1 (including /bin/sh
).
You need to use the --init
flag in order to make the container exit properly:
--init Run an init inside the container that forwards signals and reaps processes
This should work:
docker run --rm -it --init alpine:3.9 timeout -t 2 /bin/sh -c 'sleep 5; echo "Not to be seen..."'
In a dockerfile context, pb can be solve using https://github.com/krallin/tini
tool.
Using following Dockerfile:
FROM alpine:3.9RUN apk add --no-cache tiniRUN /sbin/tini timeout -t 2 /bin/sh -c 'sleep 5; echo "Not to be seen..."'
Executing docker build .
Result
Sending build context to Docker daemon 2.048kBStep 1/3 : FROM alpine:3.9 ---> 055936d39205Step 2/3 : RUN apk add --no-cache tini ---> Running in 018e342caa67fetch http://dl-cdn.alpinelinux.org/alpine/v3.9/main/x86_64/APKINDEX.tar.gzfetch http://dl-cdn.alpinelinux.org/alpine/v3.9/community/x86_64/APKINDEX.tar.gz(1/1) Installing tini (0.18.0-r0)Executing busybox-1.29.3-r10.triggerOK: 6 MiB in 15 packagesRemoving intermediate container 018e342caa67 ---> 915c3d5dc7feStep 3/3 : RUN /sbin/tini timeout -t 2 /bin/sh -c 'sleep 5; echo "Not to be seen..."' ---> Running in 3852a4f6a43dThe command '/bin/sh -c /sbin/tini timeout -t 2 /bin/sh -c 'sleep 5; echo "Not to be seen..."'' returned a non-zero code: 143
As seen the build has stoped with code 143.
The solution impose using external tool. Feel free to propose, if there is a more straight away solution.
PS: Thanks to @Eduardo Baitello for setting me on the right path :)