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.