Diagrams as code using docker + diagram.py

So a colleague showed this python tool to me today and I really liked how easy it is.

Also it has nice icons.


In the documentation for diagrams.py the main way to use it is by installing pip and graphviz on my system and the usual python stuff, meaning that either I will have a bloated python system or I have to take care of so many different virtual envs.

These days I try to usually put any tools I want to reuse often into a docker container and run from there.

So for this python tool, I will just make a Dockerfile and build it and then run the python in docker.

Every time I want to run it, it will run from the custom built docker container and that is going to be it, no bloated python setups.

Quite easy with a simple Dockerfile:

FROM python:3
RUN pip install diagrams
RUN apt-get update && apt-get install -y \
    graphviz \
 && rm -rf /var/lib/apt/lists/*

And then just mount the directory the python script is in, and have it run nice and easy like so:

docker run --rm -it -v "${PWD}":/diagram -w /diagram diagrams python diagram.py

The docker command removes the finished container so it is not polluting your docker container cache (the docker image that was built in the first step will remain).

I pushed the Dockerfile and an example here: https://github.com/ledakis/infra-diagrams-python-docker

© Copyright 2019-2021 by Theocharis Ledakis.