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,240 points
3,556 views

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 7, 2018 by shubham
• 7,340 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
• 52,360 points

Related Questions In DevOps & Agile

0 votes
1 answer
0 votes
1 answer

How do I assign a port mapping to an existing Docker container?

Below are some steps which you can ...READ MORE

answered Sep 19, 2018 in DevOps & Agile by Damon Salvatore
• 5,980 points
9,268 views
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,980 points
7,398 views
0 votes
1 answer
+2 votes
1 answer
+2 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
• 58,100 points
2,655 views
+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
• 52,360 points
16,431 views
+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,220 points
2,137 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP