Recursive Fibonacci with Docker

I tweeted this a couple of days ago but I had so much fun and so huge of a feedback that I decided to blog about it.

Here you will find a Docker image that is able to compute Fibonnaci numbers using a trivial recursive algorithm that spawns Docker containers inside Docker containers.

All you have to do is run this command line:

docker run --rm --privileged -ti -v /var/lib/docker:/var/lib/docker dgageot/fiboid 0
docker run --rm --privileged -ti -v /var/lib/docker:/var/lib/docker dgageot/fiboid 1
docker run --rm --privileged -ti -v /var/lib/docker:/var/lib/docker dgageot/fiboid 2
docker run --rm --privileged -ti -v /var/lib/docker:/var/lib/docker dgageot/fiboid 4

I’ve run this container with the value 15. It takes 30 minutes on my machine to compute the result. It launches hundreds of Docker containers to do so. The longest depth is 14. Which means that it launches a Docker#14 inside a Docker#13 inside a Docker#12 … inside a Docker#1.

What is it useful for? you might ask

Well, first it’s fun. Then it taught me a lot about Docker. I had to understand what this Dind image from Jérôme Petazzoni does and how it works.

You also wonder why it takes so much time when people say that Docker is really fast with almost no CPU overhead.

Because I spawn Docker containers inside Docker containers, I have to start a fresh Docker daemon almost each time. For each two containers, I spawn one Docker daemon. This is where the time is spent, not in the execution of the containers.

2 thoughts on “Recursive Fibonacci with Docker”

Comments are closed.