3

Is there any possibility to start a Docker container (with certain commands in CMD) and make sure it doesn't stay up for more than a specified period time?

E.g. I have an Ubuntu container, which I want to start, execute some commands and exit (kill the container) at the end. But those commands may include infinite loops or may just last a very long time, so I want to be sure the Docker container is automatically killed after a specified amount of time.

amihaiemil
  • 603
  • 6
  • 17

1 Answers1

7

Quick workaround:

docker run -d --name busybox busybox:1.26.2-glibc  sleep 25; sleep 10; docker rm -f busybox

Explanation:

I'm running a busybox container and name it busybox. In the container I'm running sleep 25 command, so the container will be alive for 25 seconds. But I want to timeout and remove the container in 10 seconds.

Another form of workaround:

timeout --signal=SIGKILL 5 docker run --rm -it busybox:1.26.2-glibc /bin/sh

Unfortunately docker isn't providing such functionality as of 17.09.0-ce. Here is the feature proposal.

JonathanDavidArndt
  • 2,245
  • 13
  • 33
  • 46
Farhad Farahi
  • 30,830
  • 7
  • 71
  • 65
  • Yes, this could be a workaround. I initially thought that I could simply specify the timeout for the commands that I want to run (e.g. like explained here: https://stackoverflow.com/questions/10224939/how-to-run-a-process-with-a-timeout-in-bash), but still, I was curious if Docker offers such functionality or not :) – amihaiemil Dec 03 '17 at 14:22