Python + Docker + SQLite

iljitsch

Ars Tribunus Angusticlavius
9,637
Subscriptor++
A few years ago, I wrote a book about the BGP routing protocol, and I decided to create a way to run a "mini lab" so readers could try examples from the book for themselves. This is powered by virtual routers in Docker and managed with shell scripts, bash on the Mac/Linux side and PowerShell on the Windows side. Go here to take a look.

I now want to extend this tool to make it more flexible. I think a good way to do that is to load virtual network configurations into an SQLite database and then run the virtual machines based on the contents of the database. Looks like SQLite support in Python 3 is pretty straightforward, so that part shouldn't be too much trouble.

For another project I already rewrote some of those shell scripts in Python, and then I encountered numerous ways for Python to interact with Docker. I went for the simplest one: simply executing stuff on the command line. Does it make sense to use one of the more complex ways to talk to Docker from Python?

Right now the Docker containers running the virtual routers get some path mapped to their file system where they find their config files. I'm going to put these in the database, so I need some code to extract those files and update them in the database if they're changed. But a really great alternative would be if I could have Docker mount some part of the database in the container's file system. I have found several projects that let a Linux machine mount an SQLite database as a file system, so that would be a decent alternative, but only under Linux*. Can Docker itself do something like this? Or are some kind of plugins possible to add this functionality?

I sometimes need this system to interact with the web. I currently do that through PHP, but I found it impossible for the Apache demon to interact with Docker due to users/permissions. So I run a PHP script on the command line and have the web PHP and CLI PHP scripts talk to each other through a System V message queue. Is that something that Python can do without too much trouble?

* It's going to be fun trying to get all of this Python stuff to work under Windows!
 

Andrey Petrov

Smack-Fu Master, in training
2
On the Python/Docker question: the Docker SDK (pip install docker) starts paying off once you need more than fire-and-forget. It talks to the Engine API directly, so you get container objects, exit codes, exceptions and log streams instead of parsing CLI output. On Windows it connects to Docker Desktop's named pipe by itself (it pulls in pywin32 there), which also gets you out of PowerShell quoting. The one gap is Compose. There is no official Python API for it, so if you use docker compose you'll still shell out for that part.

I wouldn't try to mount the database for the config files. Docker has no built-in way to expose database rows as files, and the FUSE projects you found only help on Linux. The SDK has a portable alternative. container.put_archive(path, data) unpacks a tar archive into a container, and container.get_archive(path) hands one back. You can build the tar in memory with tarfile and io.BytesIO straight from the blobs in SQLite, so nothing gets written to the host.

It also works on a container that has been created but not started yet. So the flow becomes create, copy the configs in, start. When a lab ends, pull the config directory with get_archive, compare each file with what's in the database (a hash per file is enough) and write back only what changed.

The Apache problem is the socket: /var/run/docker.sock is owned by root and the docker group. Adding the Apache user to that group would make it work, but it effectively gives the web server root on the box. Your current split, a privileged helper plus an unprivileged web front end, is the right shape. Python can do SysV message queues with the third-party sysv_ipc module, but that module doesn't exist on Windows. A small HTTP service bound to 127.0.0.1 is portable: run it as a user in the docker group, let it expose only the few actions the web side needs, like starting or stopping a lab, and have PHP call it with curl.