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.


#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

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

  • 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”: [
“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…

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

    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()"Time took to create the container: %s seconds." % (end - start))

start = time.time()


end = time.time()"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 
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=[
    ], ports=[80],
        port_bindings={80: 8080},

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 
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 \

# 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