docker execution speed

We are going to use Docker, but there is a "discussion" as too how efficient this will be... and as I have no clue when the correct answer will be - it is time to develop a test.

Python Prime Numbers

This is the test - check the number of prime numbers.

And this is the code

python -m timeit -n 10 -s "from pyprimes import prime_count" "prime_count(1500000)"

In order to run this we need

  • python3
  • a package called pyprimes

Docker Image

Using the official Python:3 Docker image (called Python:3). We can auto add modules by copying a requirements file into a specific direcory (info on the Docker python:3 web page at in case you think I am making this up.


FROM python:3

WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
add /usr/local/bin/
run chmod +x /usr/local/bin/

cmd ["/bin/bash","/usr/local/bin/"]

python -m timeit -n 10 -s "from pyprimes import prime_count" "prime_count(1500000)"



Building Container

Straight forward Container build command

docker build -t py3_prime:1.0 ./py_prime

The output looks like

Sending build context to Docker daemon  4.096kB
Step 1/7 : FROM python:3
 ---> a5b7afcfdcc8
Step 2/7 : WORKDIR /usr/src/app
Removing intermediate container ef3af0706d52
 ---> f0640f92db31
Step 3/7 : COPY requirements.txt ./
 ---> 60fa2c98eb9c
Step 4/7 : RUN pip install --no-cache-dir -r requirements.txt
 ---> Running in edb0f6715b98
Collecting pyprimes (from -r requirements.txt (line 1))
Installing collected packages: pyprimes
  Running install for pyprimes: started
    Running install for pyprimes: finished with status 'done'
Successfully installed pyprimes-0.1
Removing intermediate container edb0f6715b98
 ---> 74e9611089bc
Step 5/7 : add /usr/local/bin/
 ---> 6c08eeb1832e
Step 6/7 : run chmod +x /usr/local/bin/
 ---> Running in 7f380b0d2312
Removing intermediate container 7f380b0d2312
 ---> e444105f221f
Step 7/7 : cmd ["/bin/bash","/usr/local/bin/"]
 ---> Running in 27bc3f67fc1a
Removing intermediate container 27bc3f67fc1a
 ---> 85e34c23c430
Successfully built 85e34c23c430
Successfully tagged py_prime:1.0

And that's it

Test it on Baremetal

Again a normal run command, we have the script to auto-execute so no need to shell into the container

docker run -t py_prime:1.0

And I get the result

10 loops, best of 3: 293 msec per loop

Bare Metal Parallel 5

To be a little more difficult I than run 5 parallel tasks

for a in 1 2 3 4 5 
   nohup ./

The average time with 5 parallel proceses running was 10 loops, best of 3: 838 msec per loop.

Container Parallel 5

To be a little more difficult I than run 5 parallel tasks

for a in 1 2 3 4 5 
   nohup docker run -t py_prime:1.0&

The average time with 5 parallel proceses running was 110 loops, best of 3: 713 msec per loop.

I did not expect that to be quicker.

Export the Container

I could repeat these steps on the virtual Machine, instead I will export and Import the container.

Virtual Machine

I build a new VirtualMachine

  • Ubuntu Serer 16.04.03
  • BARE Install

I then used this script to add directly from Docker, and not from the Ubuntu Repos

curl -fsSL | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"
sudo apt-get install apt-transport-https -y
sudo apt-get update
apt-cache policy docker-ce
sudo apt-get install -y docker-ce

I then added the Group Docker to my username

moduser -aG docker tim

Logged out - and then carried on.


Using the Save option

docker save py_prime > py_prime.tar


Using the Load option

Note: This I had to sudo to make to work.

docker load -i <path to image tar file>

VM Container Tests

I did the same as on the baremetal machine

1 Thread - same time as the Baremetal 5 Threads - Half the speed of the bare-metal solution.