How to use timeout in dockerfile with alpine base? How to use timeout in dockerfile with alpine base? docker docker

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 :)


Timeout option in Dockerfile, you can embed timeout <value> directly for commands in Dockerfile

Eg:

ARG GOLANG=golang:1.16.4-alpine3.12FROM ${GOLANG}RUN timeout 60 curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh```