Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit `docker run` execution time?

Tags:

docker

I want to run a command inside a docker container. If the command takes more than 3 seconds to finish, the container should be deleted.

I thought I can achieve this goal by using --stop-timeout option in docker run.

But it looks like something goes wrong with my command.

For example, docker run -d --stop-timeout 3 ubuntu:14.04 sleep 100 command creates a docker container that lasts for more than 3 seconds. The container is not stopped or deleted after the 3rd second.

Do I misunderstand the meaning of --stop-timeout?

The document says

--stop-timeout Timeout (in seconds) to stop a container

Here's my docker version:

Client:
 Version:       17.12.0-ce
 API version:   1.35
 Go version:    go1.9.2
 Git commit:    c97c6d6
 Built: Wed Dec 27 20:03:51 2017
 OS/Arch:       darwin/amd64

Server:
 Engine:
  Version:      17.12.0-ce
  API version:  1.35 (minimum version 1.12)
  Go version:   go1.9.2
  Git commit:   c97c6d6
  Built:        Wed Dec 27 20:12:29 2017
  OS/Arch:      linux/amd64
  Experimental: true

The API version is newer than 1.25.

like image 556
Brian Avatar asked Jan 17 '18 10:01

Brian


People also ask

Does docker have a timeout?

If your docker start and docker create API calls take longer than four minutes, then AWS Batch returns a DockerTimeoutError error. Note: The default timeout limit set by the Amazon Elastic Container Service (Amazon ECS) container agent is four minutes.

How do I stop docker runtime?

Start the daemon manually To stop Docker when you have started it manually, issue a Ctrl+C in your terminal.

How do I change my docker RAM usage?

To limit the maximum amount of memory usage for a container, add the --memory option to the docker run command. Alternatively, you can use the shortcut -m . Within the command, specify how much memory you want to dedicate to that specific container.

How do I keep docker containers running forever?

The simplest way to keep the container running is to pass a command that never ends. We can use never-ending commands in any of the following ways: ENTRYPOINT or CMD directive in the Dockerfile. Overriding ENTRYPOINT or CMD in the docker run command.


2 Answers

You can try

timeout 3 docker run...

there is a PR on that subject

https://github.com/moby/moby/issues/1905

See also

Docker timeout for container?

like image 63
user2915097 Avatar answered Sep 21 '22 03:09

user2915097


Depending on what exactly you want to achieve, the --ulimit parameter to docker run may do what you need. For example:

docker run --rm -it --ulimit cpu=1 debian:buster bash -c '(while true; do true; done)'

After about 1s, this will print Killed and return. With the --ulimit option, it would rune forever.

However, note that this only limits the CPU time, not the wall clock time. You can happily run sleep 24h with a --ulimit cpu=1 because sleep does not consume CPU time.

like image 43
ingomueller.net Avatar answered Sep 18 '22 03:09

ingomueller.net