简体   繁体   English

Docker 容器 CPU 和内存利用率

[英]Docker container CPU and Memory Utilization

I have a Docker container running with this command in my Jenkins job:我的 Jenkins 作业中有一个使用此命令运行的 Docker 容器:

docker run --name="mydoc" reportgeneration:1.0 start=$START end=$END config=$myfile

This works very well.这很好用。 The image is created from a DockerFile which is executing a shell script with ENTRYPOINT .该图像是从 DockerFile 创建的,该 DockerFile 使用ENTRYPOINT执行 shell 脚本。

Now I want to know how much CPU and memory has been utilized by this container.现在我想知道这个容器使用了多少 CPU 和内存。 I am using a Jenkins job, where in the "execute shell command", I am running the above Docker run command.我正在使用 Jenkins 作业,在“执行 shell 命令”中,我正在运行上面的 Docker 运行命令。

I saw about 'docker stats' command.我看到了“docker stats”命令。 It works very well in my Ubuntu machine.它在我的 Ubuntu 机器上运行良好。 But I want it to run via Jenkins as my container is running via Jenkins console.但我希望它通过 Jenkins 运行,因为我的容器是通过 Jenkins 控制台运行的。 So here follows the limitations I have.所以这里遵循我的限制。

  1. I don't know if there is any way to stop docker stats command.我不知道是否有任何方法可以停止docker stats命令。 In Ubuntu command line, we hit 'ctrl+c' to stop it.在 Ubuntu 命令行中,我们按“ctrl+c”来停止它。 How will I do it in Jenkins?我将如何在詹金斯做到这一点?
  2. Even if I figure out a way to stop docker stats , once the 'docker run' command gets executed, the container will not be active and will be exited.即使我找到停止docker stats的方法,一旦执行了“docker run”命令,容器将不会处于活动状态并将退出。 For exited container, CPU and memory utilisation will be zero.对于退出的容器,CPU 和内存利用率将为零。
docker run 'image' 

docker stats container id/name

With the above two lines, docker stats command will only get an exited container and I don't think docker stats will even work with Jenkins console as it cannot be stopped.通过以上两行, docker stats命令只会得到一个退出的容器,我认为docker stats甚至无法与 Jenkins 控制台一起使用,因为它无法停止。

Is there any way that I can get container's resource utilization (CPU, memory) in a better way via Jenkins console?有什么方法可以通过 Jenkins 控制台更好地获取容器的资源利用率(CPU、内存)?

Suggestion is to not run docker stats interactively, but have a piece of a shell script with a loop like this:建议不要以交互方式运行docker stats ,而是使用一段带有如下循环的 shell 脚本:

#!/bin/sh

# First, start the container
CONTAINER_ID=$(docker run -d ...)

# Then start watching that it's running (with inspect)
while [ "$(docker inspect -f {{.State.Running}} $CONTAINER_ID 2>/dev/null)" = "true" ]; do
    # And while it's running, check stats
    docker stats --no-stream $CONTAINER_ID
    sleep 1
done

# When the script reaches this point, the container had stopped.
# For example, let's clean it up (assuming you haven't used --rm in run).
docker rm $CONTAINER_ID

The condition checks whenever the container is running or not, and docker stats --no-stream prints stats once then exits, making it suitable for non-interactive use.条件检查容器是否正在运行,并且docker stats --no-stream打印一次统计信息然后退出,使其适合非交互式使用。

I believe you can use a variant of such shell script file (obviously, updated to do something useful, rather than just starting the container and watching its stats) as a build step .我相信您可以使用此类 shell 脚本文件的变体(显然,更新后可以做一些有用的事情,而不是仅仅启动容器并查看其统计信息) 作为构建步骤


But if you need/want/have an interactive process that you want to stop, kill is the command you're looking for.但是如果你需要/想要/有一个你想要停止的交互式进程,那么kill就是你正在寻找的命令。 Ctrl-C in a terminal just sends a SIGINT to the process.终端中的 Ctrl-C 只是向进程发送 SIGINT

You need to know an PID, of course.当然,您需要知道 PID。 I'm not sure about Jenkins, but if you've just started a child process from a shell script with child-process & (eg docker stats & ), then its PID would be in the $!我不确定 Jenkins,但如果您刚刚从带有child-process & (例如docker stats & )的 shell 脚本启动子进程,那么它的 PID 将$! variable .变数 Or you can try to figure it using pidof or ps commands, but that may be error-prone in case of concurrent jobs (unless they're all isolated).或者您可以尝试使用pidofps命令来计算它,但在并发作业的情况下这可能容易出错(除非它们都是孤立的)。

Here I've assumed that your Jenkins jobs are shell scripts that do the actual work.在这里,我假设您的 Jenkins 作业是执行实际工作的 shell 脚本。 If your setup is different (eg if you use some plugins so Jenkins talk to Docker directly), things may be different and more complicated.如果您的设置不同(例如,如果您使用一些插件以便 Jenkins 直接与 Docker 对话),事情可能会有所不同并且更加复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM