Docker Community Forums

Share and learn in the Docker community.

What is the best way to run a container for a maximum of N seconds?

Hello,

I want to run a container and give it a maximum of N seconds to run after which I’d like the container terminated.

Here is what I’m doing:

1. docker run ... & # run it in the bg, get pid into $pid, etc.
2. get the container id in bash: cid=`docker ps -aq | awk '{print $1}'`, 
3. nohup something like (sleep N && docker kill $cid) & wait $pid

However, I’m convinced there must be a better way. Can you please help with a suggestion or recommendation.

Thanks much,

&

Hi, the correct way of doing this would be for the code, in the container, to stop it.

1 Like

Of course! What was I thinking!

Thanks Martin,

&