I ran the following command:
docker run python:3.8-alpine /bin/sh -c "timeout 1s sleep 5"
I expected it to terminate after 1 second, but it took 5 seconds to terminate instead.
If I enter the container and execute the command like below, it terminates correctly after 1 second:
docker run -it python:3.8-alpine /bin/sh
timeout 1s sleep 5
Why did the first command behave differently than expected?
4
Answers
In Docker container this process
/bin/sh -c "timeout 1s sleep 5"
runs asPID 1
.The
timeout
command uses theSIGTERM
to stop a process.According to this link:
And according to this link:
You must tell Docker to run the container with
--init
option (runs an init process):you can also kill the
PID 1
withtrap
and kill the process.Trap allows you to catch signals and execute code when they occur.
you can also do this:
It’s not the correct question.
when you run
you said to execute the sleep command and wait for 1 second to execute it. also when you use the -c option you said to sh execute this command as a parameter of sh. but without the -c option it would like to execute from a file and said no such file or directory to execute so you thought your timeout executed but it’s not true.
Example:
on your terminal type the following commands to catch it:
it said no such file or directory
it was executed correctly.
I hope I was able to convey my meaning.