1

I have a Docker container running with this command in my Jenkins job:

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.

Now I want to know how much CPU and memory has been utilized by this container. I am using a Jenkins job, where in the "execute shell command", I am running the above Docker run command.

I saw about 'docker stats' command. It works very well in my Ubuntu machine. But I want it to run via Jenkins as my container is running via Jenkins console. So here follows the limitations I have.

  1. I don't know if there is any way to stop docker stats command. In Ubuntu command line, we hit 'ctrl+c' to stop it. 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. For exited container, CPU and memory utilisation will be zero.
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.

Is there any way that I can get container's resource utilization (CPU, memory) in a better way via Jenkins console?

Paulie-C
  • 1,674
  • 1
  • 13
  • 29
Raji
  • 857
  • 6
  • 18
  • 37

1 Answers1

4

Suggestion is to not run docker stats interactively, but have a piece of a shell script with a loop like this:

#!/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.

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.


But if you need/want/have an interactive process that you want to stop, kill is the command you're looking for. Ctrl-C in a terminal just sends a SIGINT to the process.

You need to know an PID, of course. I'm not sure about Jenkins, but if you've just started a child process from a shell script with child-process & (e.g. docker stats &), then its PID would be in the $! 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).

Here I've assumed that your Jenkins jobs are shell scripts that do the actual work. If your setup is different (e.g. if you use some plugins so Jenkins talk to Docker directly), things may be different and more complicated.

Community
  • 1
  • 1
drdaeman
  • 11,159
  • 7
  • 59
  • 104
  • Thanks a lot drdaemon. That helped a lot with 'docker stats' issue in my case. I am able to run it in Jenkins. Would you be kind enough to help me understand/guide, how can I implement both the above statements together ('docker run' and 'docker stats')? When I run docker run, container gets exited and then docker stats give me zero CPU and memory (basically point number 2). Right now I am executing both in different jobs and hence triggering jobs manually. – Raji Apr 28 '17 at 11:43
  • @Raji I haven't used Jenkins much (and that was some years ago), so can't tell the exact steps, but I think the simplest way is to create a shell script with all the commands, and make Jenkins run it (http://stackoverflow.com/a/40888664/116546). The logic is, matching `run` and `stats` commands need to be in a single job to work correctly (having them in different jobs will cause unnecessary complications). And shell scripts are simple yet very flexible when you need to run a few commands in specific order, with loops etc (IIRC, Jenkins' build steps don't have loops). – drdaeman Apr 28 '17 at 12:05
  • Updated the answer with a slightly better example how the shell script would look like, including the link (same as in the comment above) on using shell scripts as a Jenkins CI build step. Hope this would help. – drdaeman Apr 28 '17 at 12:15
  • Thanks a ton Drdaeman :) I will check it and will put all both in the same Jenkins job. Thanks again!! – Raji Apr 28 '17 at 12:19
  • `watch -n1 docker stats` – ulidtko Sep 14 '17 at 12:36
  • @ulidtko No, that's just wrong. First, without `--no-stream`, `docker stats` is already interactive, basically having `watch` built in. Second, OP needed non-interactive use. – drdaeman Sep 14 '17 at 12:40