I'm writing a dockerfile where I run a server application based on alpine:3.9
docker image. I test if my application is correctly installed by running it and sending a GET
request with curl
.
I send requests until I get an answer. So in case of trouble I want to timeout my tests using the built-in timeout
function.
I'm using docker version 19.03.4, build 9013bf583a
on debian.
The command:
docker run --rm -it alpine:3.9 timeout -t 2 /bin/sh -c 'sleep 5; echo "Not to be seen..."'
Exit displaying Not to be seen...
.
If I use timeout this way, the timeout seems to be ignored... I got the same problem if I put this in a docker file.
FROM alpine:3.9
RUN timeout -t 2 /bin/sh -c 'sleep 5; echo "Not to be seen..."'
docker build -t test .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM alpine:3.9
---> 055936d39205
Step 2/2 : RUN timeout -t 2 /bin/sh -c 'sleep 5; echo "Not to be seen..."'
---> Running in 0fa22cd02173
Not to be seen...
Removing intermediate container 0fa22cd02173
---> e88107c3811b
Successfully built e88107c3811b
Successfully tagged test:latest
We can see the Not to be seen...
displayed in console.
How to solve this issue ?
I test multiple things and the timeout function works well. It should be a problem when using it in dockerfile or passing it in docker run
function...
When I try:
docker run --rm -it alpine:3.9 /bin/sh
# Then in container
timeout -t 2 /bin/sh -c 'sleep 5; echo "Not to be seen..."'
Exit with status code 143. And display Terminated
.
When I try:
docker run --rm --name test -td alpine:3.9 /bin/sh
docker exec -it test timeout -t 2 /bin/sh -c 'sleep 5; echo "Not to be seen..."'
Exit with status code 143. Nothing displayed as expected.
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..."'