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