How do I set max lifetime for Docker Container?

+3 votes
I wanted to know that is it possible to start a Docker container and make sure it stops after a specified period time?

Suppose that I have an Ubuntu container, which I start, executed some commands and exited in the end. But it’s possible that those commands may include infinite loops or may just last a very long time, so how do I make sure that the Docker container is automatically killed after an amount of time.

Thank you.
Apr 6, 2018 in DevOps & Agile by Atul
• 10,250 points

2 answers to this question.

0 votes

Try this:

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

In the busy box container what I am doing is 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.

answered Apr 6, 2018 by shubham
• 6,910 points
0 votes

There is one more way which you can try:

timeout --signal=SIGKILL 5 docker run --rm -it busybox:1.26.2-glibc /bin/sh
answered Oct 18, 2018 by Kalgi
• 51,830 points

Related Questions In DevOps & Agile

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do I restart a single container with docker-compose?

Actually it's not that difficult. Here is ...READ MORE

answered Nov 8, 2018 in DevOps & Agile by Damon Salvatore
• 5,540 points
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 57,530 points
+14 votes
5 answers

How do I run a cron job inside a docker container

If you can SSH into your server ...READ MORE

answered Jul 27, 2018 in DevOps & Agile by Kalgi
• 51,830 points
+1 vote
2 answers

How do I run Apache server from Docker without mapping to a location?

If you're seeing a 500 error, that's ...READ MORE

answered Oct 18, 2018 in DevOps & Agile by lina
• 8,200 points