Docker Community Forums

Share and learn in the Docker community.

How to measure container start time

I have a simple c based “Hello world” application that I dockerized into different base images. I want to measure boot time for the container. I tried running time docker run my-app but I am not sure if this is the correct way to get boot time. Any pointers will be helpful.

Application:

#include <stdio.h>

int main() {
  printf("Hello world");
  return 0;
}

One of the Dockerfile:

FROM scratch
COPY hello /
CMD ["/hello"]

May I ask what boot time you refer to? A container does not boot: - no kernel is started, no os is booted, no os services are started, just whatever you declared as ENTRYPOINT/CMD will be executed. The creation time will vary depending on the parameters: no network will speed things up, a few published ports will slow it down, many published ports will slow it down by a lot…

Thus said: what exactly are you looking for?!

Do you mean the container creation process that does things like creating the filesystem, network, volumes and more depending of the parametrization of your docker create or docker run` command?

1 Like

Thank you for clarity.

I mean the container creation process and how much time spent on creating the filesystem, network, volumes, etc. Is it possible to see a breakdown of different namespace creation?

I have no idea :slight_smile:

If i’d need that kind of information, I would check if enabling debug for the server component enriches the logs with the required information, if it doesn’t then I would try to hook in into the event stream of the docker engine and get the point in time where things happen.

Thank you very much, Metin.

Based on discussion, I got more clarity. I will look for debugging functionality. If I find something, I will update here. Thanks Again :smiley:

1 Like

1st approach: 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.

2nd approach: container information
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”
}

Duration calculation example (with bash):
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

As you probably know, when we execute something like docker run something, there are essentially two things happening behind the scenes.

1. The container is created, i.e. everything is set up in the correct order, things are verified etc. The equivalent command is docker container create.

2. The container is started. Equivalent command is docker container start.

Most of the time we just use the docker run command. To investigate how much time it takes to create a container, and start a container, we can take the human friendly docker client out of the way. The following Python3 source code will help you understand this better.

from docker import APIClient
import logging as log
import time

log.basicConfig(
    level=log.INFO, filename="/dev/stdout",
    format="%(levelname)s: %(message)s"
)
cli = APIClient(base_url="unix:///run/docker.sock")

# The container creation process starts here
start = time.time()

container = cli.create_container(image="ubuntu:focal")

end = time.time()

log.info("Time took to create the container: %s seconds." % (end - start))

start = time.time()

cli.start(container.get("Id"))

end = time.time()

log.info("Time took to start the container: %s seconds." % (end - start))

It simply creates a container, calculates the time, then starts the container, and calculates the time again. I used the ubuntu:focal image here ( if you’re planning to use this, pull the image first docker pull ubuntu:focal). Here are my results -

debdut@localhost:~/forum$ python3 0-constartup.py 
INFO: Time took to create the container: 0.03260612487792969 seconds.
INFO: Time took to start the container: 0.3692433834075928 seconds.

Do note that this container instance was very barebone. In the following I tried something slightly harder.

container = cli.create_container(
    image="nginx", volumes=[
        "/etc/nginx/conf.d",
        "/usr/share/nginx"
    ], ports=[80],
    host_config=cli.create_host_config(
        port_bindings={80: 8080},
        binds=[
            "vol0:/etc/nginx/conf.d",
            "vol1:/usr/share/nginx"
        ]
    )
)

I’m only changing the container creation part, adding some port binds and volumes. Create the volumes first if you’re going to try this yourself -

docker volume create vol0
docker volume create vol1

I got the following estimate after making the container creation somewhat more difficult than it was before -

debdut@localhost:~/forum$ python3 0-constartup.py 
INFO: Time took to create the container: 0.04808855056762695 seconds.
INFO: Time took to start the container: 0.4077751636505127 seconds.

If you’d more like to use normal shell commands, here’s the first part in curl and jq -

time id=$(curl -XPOST \
    --unix-socket /run/docker.sock \
    -H "Content-Type: application/json" -d '{ "Image": "ubuntu:focal" }' \
    http://localhost/containers/create -s \
    | jq -r ".Id"
)

# Output . . 

time curl -XPOST \
    --unix-socket /run/docker.sock \
    http://localhost/containers/$id/start

# Output . .

Hope this helps. At the end, do remember to run docker rm $(docker ps -qf status=exited), otherwise you’ll be left with quite so many unnecessary containers.

1 Like