Skip to main content

Docker - Diagrams as code

Generate stunning infrastructure and application diagrams directly from Python code using the diagrams library and a simple Docker command. Visualize your complex systems as code effortlessly.

· 5 min read
View raw
Christophe
Markdown, WSL and Docker lover ~ Insatiable curious.
Cet article existe aussi en français — lire la version française.

This article is over a year old but was reviewed on July 30, 2026 — the content is still accurate.

Docker - Diagrams as code

⚡
TL;DR

This article shows how to render diagrams-as-code with the Python diagrams library via a ready-to-use Docker image (gtramontina/diagrams), piping a .py file into docker run to produce architecture diagrams (e.g. AWS/Azure/GCP icons) with zero local install. It closes with a broad roundup of other diagram-as-code tools: Mermaid, PlantUML, Graphviz, Kroki, DBML-renderer, Structurizr, and more.

But what a joy it is to be able to draw diagrams by just writing text. Some tools are better known than others, e.g. Mermaid — which I've since automated in Documentation as Code - Transform Your Infrastructure into Beautiful Diagrams with Python and Mermaid, and whose Graphviz cousin is covered in Python - Generate flows using pydot.

Did you know https://diagrams.mingrammer.com/? Let's explore it using, of course, a Docker ready-to-use image.

What comes out​

Here is a diagram produced by this article, and nothing but text went in:

Team

The command that produced it is a single line; the .py file is piped into a container and the .png lands in your current folder:

>user@machine: ~/yourproject

Why it works​

  • The .py file is not executed on your machine: it's piped into the container, which contains Python, the diagrams library, Graphviz and the whole AWS/Azure/GCP/K8S icon sets.
  • -v $(pwd):/out is what makes the generated image come back to your folder instead of dying with the container.
  • -u 1000:1000 runs the container as you, so the produced file belongs to your user and not to root.

Windows notation

If you're working on Windows, replace $(pwd) with %CD%. And replace cat by type.

The source​

As always, for the demo, please start a Linux shell and run mkdir -p /tmp/docker-diagrams && cd $_ to create a folder called docker-diagrams in your Linux temporary folder and jump in it.

Please create a new file called team.py with the content that produced the diagram above:

from diagrams import Diagram
from diagrams.aws.compute import EC2
from diagrams.aws.database import RDS
from diagrams.aws.network import ELB
with Diagram("My Team", show=False, direction="TB"):
(
ELB("Olivier")
>> [EC2("Christophe"), EC2("Jason"), EC2("Niki"), EC2("Rudy"), EC2("Stijn")]
>> RDS("Work together")
)

This code creates a diagram showing a team structure using AWS architecture icons. It visualizes how an entity named 'Olivier' (represented as a load balancer) connects to five team members (represented as EC2 instances) who all feed into a shared database called 'Work together'. The diagram is created but not displayed on screen.

Then run the conversion command shown here above.

0.23.3 is the latest version available when writing this document. See https://hub.docker.com/r/gtramontina/diagrams/tags to retrieve the latest one.

Easy, right?

A real architecture​

Twenty lines of Python for a team chart is nice; here is the same exercise on an actual infrastructure:

from diagrams import Cluster, Diagram
from diagrams.k8s.compute import Pod, StatefulSet
from diagrams.k8s.network import Service
from diagrams.k8s.storage import PV, PVC, StorageClass
with Diagram("Stateful Architecture", show=False):
with Cluster("Apps"):
svc = Service("svc")
sts = StatefulSet("sts")
apps = []
for _ in range(3):
pod = Pod("pod")
pvc = PVC("pvc")
pod - sts - pvc
apps.append(svc >> pod >> pvc)
apps << PV("pv") << StorageClass("sc")

This script generates a diagram of a Kubernetes stateful application architecture. It creates a visual representation showing a service routing to three pods, each with its own persistent volume claim, all backed by a shared persistent volume and storage class. The diagram is built using the diagrams library and saved to a file rather than displayed immediately.

And the resulting image:

Stateful Architecture

Crazy, right? And all of this without installing anything!

The Docker image code base is here: https://github.com/gtramontina/docker-diagrams, and you'll find plenty of other examples at https://github.com/mingrammer/diagrams.

Icons and other tools (reference, skip this for now)​

A tremendous list of icons/nodes is available on multiple pages at https://diagrams.mingrammer.com/docs/nodes/onprem. See OnPrem, AWS, Azure, GCP, IBM, K8S and also how to create our own (using local .png images): Custom.

And, since diagrams is far from being the only text-to-picture tool around, here is the list I keep coming back to:

Conclusion​

Architecture diagrams that live in your repository, that you can diff and review like any other file, and that never require Python or Graphviz on your machine. The next time the infrastructure changes, you edit a line of text instead of dragging a box.

If you'd rather stay with a syntax you already know, Documentation as Code - Transform Your Infrastructure into Beautiful Diagrams with Python and Mermaid does the same trick with Mermaid.


Was this article helpful?
Did you try the steps in this article?