如何限制docker run的执行时间? [英] How to limit `docker run` execution time?

查看:577
本文介绍了如何限制docker run的执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在docker容器中运行命令。如果该命令花费了3秒钟以上的时间,则应删除该容器。

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.

我想我可以通过使用-stop来实现此目标 docker run 中的-timeout 选项。

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.

例如, docker run -d --stop-timeout 3 ubuntu:14.04 sleep 100 命令创建一个docker持续时间超过3秒的容器。第3秒后容器没有停止或删除。

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.

我是否误解了-停止超时?

文档


-停止超时超时(以秒为单位)停止容器

--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

API版本较新1.25。

The API version is newer than 1.25.

推荐答案

-停止超时选项是docker在使用 docker stop 命令时等待容器停止的最长时间。

The --stop-timeout option is the maximum amount of time docker should wait for your container to stop when using the docker stop command.

容器将在收到通知或命令运行完成时停止,因此,如果将睡眠时间从100更改为1,您将看到容器在第二秒后停止。

A container will stop when it's told to or when the command is running finishes, so if you change you sleep from 100 to 1, you'll see that the container is stopped after a second.

我建议您做的是将容器的 ENTRYPOINT 更改为您创建的脚本,该脚本将执行您想要的并跟踪执行情况

What I'll advice you to do is to change the ENTRYPOINT of your container to a script that you create, that will execute what you want and keep track of the execution time from within and exit when timeout.

之后,您可以使用-rm 选项启动容器,

After that you can start your container using the --rm option that will delete it once the script finishes.

一个小例子。

Dockerfile:

Dockerfile:

FROM ubuntu:16.04

ADD ./script.sh /script.sh

ENTRYPOINT /script.sh

script.sh:

script.sh:

#!/bin/bash

timeout=5
sleep_for=1

sleep 100 &

find_process=$(ps aux | grep -v "grep" | grep "sleep")

while [ ! -z "$find_process" ]; do
    find_process=$(ps aux | grep -v "grep" | grep "sleep")

    if [ "$timeout" -le "0" ]; then
      echo "Timeout"
      exit 1
    fi

    timeout=$(($timeout - $sleep_for))
    sleep $sleep_for
done

exit 0

使用以下命令运行它:

docker build -t testing .
docker run --rm testing

此脚本将执行 sleep 100 在后台,检查其是否仍在运行,是否达到5秒的超时,然后退出。

This script will execute sleep 100 in background, check if its still running and if the timeout of 5 seconds is reach then exit.

这可能不是最好的方法做到这一点,但是如果您想做一些简单的事情可能会有所帮助。

This might not be the best way to do it, but if you want to do something simple it may help.

这篇关于如何限制docker run的执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆