DEV Community

Alex M. Schapelle for Otomato

Posted on

When Python Runs Your Containers (part 2)

Welcome gentle reader. My name is Silent-Mobius, also known as Alex M. Schapelle. In the past we've introduced you to a small project named docker-agent which was created for remote management of docker containers, which I renamed into mazin-docker. The project was conceived as an idea while I was tinkering with various tools for automation and also found alike project of other developer named @andreagrandi

To Address The Known Wisdom

We have created flask RestAPI application that connects with docker sdk to Docker-Engine and does basic commands with docker containers, for docker containers. You can read all about it here. Once we made mazin-docker work, but the price was too high:

  • Application structure was dumped in one folder
  • No tests were available
  • Extending application as python library was not an option

To Plan Or Not To Plan

Building application while tinkering, usually produces working software, yet it is less usable when extending with additional features. In wisdom of flask developer council, we need to create applications with Large Applications as Packages. That is why I've invested in planning the new application structure, that also supports tests in addition to continuous integration.
The revamped structure on Devel branch of the project
and looks like this:

├── nginx.conf
├── pyproject.toml
├── src
│   └── mazin_docker
│       ├──
│       ├──
│       ├── docker-compose.yml
│       ├── hooks
│       │   ├──
│       │   ├──
│       │   └──
│       ├──
│       ├── libs
│       │   ├──
│       │   ├──
│       │   ├──
│       │   └──
│       └── requirements.txt
└── tests
Enter fullscreen mode Exit fullscreen mode
  • pyproject.toml: Python project package configuration file
  • nginx.conf: Reverse proxy configuration file for deployment
  • src : General name for source code of the project. Mainly suggested to be used for tests cover
  • tests: Folder for test configuration and test scripts
  • : Python constructor used to initializing the object’s state. The task of constructors is to initialize and also enable export .py files as loadable libraries
  • : Main logic and routing of the application
  • : Application configuration file
  • libs : Folder for keeping external functions and classes of data structures
  • docker-compose.yml: Initial docker compose file that is used to manage container services
  • hooks : folder that holds custom python scripts that are executed when RestAPI event is invoked
  • requirements.txt : requirement file that lists python libraries needed to be included for running or developing the project.

In What Manner Shall It Come To Life ?

The question that is most likely to be asked by you, dear reader, would be: how do I make it work?
Through the iterations of the project, we have changed the structure to be used as a library, thus can be executed with simple python execution:

git clone
git checkout Devel # Devel is our main branch
Enter fullscreen mode Exit fullscreen mode

This shall start the the application for local use and development.
In case of production use case, it would be suggested to use one of the popular wsgi application servers, such as gunicorn, cherrypy or uWSGI.

cd mazin-docker # validate that you are in project folder
pip3 install -r src/requirements.txt # gunicorn is part of the requirements.txt file
gunicorn --reload --worker 5 --bind
Enter fullscreen mode Exit fullscreen mode

From here on, we can install nginx to reverse proxy the service of the application, or apache2 to connect the application directly. In case you are adventures, you can run it as it is, in systemd service mode, which I've shown in previous article.


At the moment the project is still in development adding features, thus making it Work In Progress, wIP for short.
In case you'd like to request feature, don't hesitate to reach out or open issue on gitlab
So the die is cast, all is left for you to embrace the future and to remember: Do Try To Have Some Fun

Top comments (0)