I have a docker image called my_image
which launch a command and closes.
When running the image in a container using command docker run --rm my_image
, is it possible to measure the execution time of the container ?
Edit :
I need to see those timing information after container execution, thus I can't use time
command.
I somehow hoped to find some container execution history kept by docker even if --rm
was used. But if it doesn't exist, then @tgogos' answer is suited.
The goal is to compare execution time of several images to draw a conclusion about the different tools used.
time
time docker run --rm --name=test alpine ping -c 10 8.8.8.8
...
real 0m10.261s
user 0m0.228s
sys 0m0.044s
but this will also include the time for creating and removing the container.
The information you are looking for is stored by docker and can be reached by docker container inspect
.
docker run --name=test alpine ping -c 10 8.8.8.8
* notice that I didn't use --rm
because the next step is to inpect the container. You will have to remove it afterwards. The timestamps you might be interested in are:
"Created": "2018-08-02T10:16:48.59705963Z",
"StartedAt": "2018-08-02T10:16:49.187187456Z",
"FinishedAt": "2018-08-02T10:16:58.27795818Z"
$ docker container inspect test
[
{
"Id": "96e469fdb437814817ee2e9ad2fcdbf468a88694fcc998339edd424f9689f71f",
"Created": "2018-08-02T10:16:48.59705963Z",
"Path": "ping",
"Args": [
"-c",
"10",
"8.8.8.8"
],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-08-02T10:16:49.187187456Z",
"FinishedAt": "2018-08-02T10:16:58.27795818Z"
}
...
You can put these timestamps in bash variables with single commands like this:
START=$(docker inspect --format='{{.State.StartedAt}}' test)
STOP=$(docker inspect --format='{{.State.FinishedAt}}' test)
Then you can convert them to UNIX epoch timestamps (seconds since Jan 01 1970. (UTC))
START_TIMESTAMP=$(date --date=$START +%s)
STOP_TIMESTAMP=$(date --date=$STOP +%s)
and if you subtract these two, you get the duration in seconds...
echo $(($STOP_TIMESTAMP-$START_TIMESTAMP)) seconds
9 seconds
You have to consider a couple of things:
docker exec
specifying PID=1 because container time running = entrypoint running.Combining two things you get docker container time execution with:
docker exec -ti <container_id> ps -o etime= -p "1"
It gives you more accuracy than column STATUS of docker ps
command.
If you're executing several processes in container and you need execution time for any of them, just replace "1" by its PID inside container.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With