A Fantastic Way to Programmatically Create Diagrams for Different Cloud Architectures
By: Bruce H. Cottman
Tags: Python, Architecture, Docker, Programming, Cloud

Docker Solution for Graphviz, Diagram, and Cluster

I have posted several articles on how to create development and test Docker images [see references 4, 5, and 6 below]. I assume you know of Docker and have read them.

Docker is used for encapsulating an individual image of your application.

Docker-Compose is used to manage several images at the same time for the same application. This tool offers the same features as Docker but allows you to have more complex applications.

Figure 2.Docker-Compose. Illustration by Rachel Cottman

Diagrams and Graphviz for a Docker image: steps required

Based on the instructions in the articles mentioned above, Graphviz is put in the Docker image build by modifying the end of the dockerfile :

# Step 1

.

.

.

USER root

RUN apt-get update

RUN apt-get -y install python-pydot

RUN apt-get -y install python-pydot-ng

RUN apt-get -y install graphviz

RUN rm -rf /var/lib/apt/lists/*

I put the modifications at the end of the dockerfile because the USER root command changes the permissions and I was too impatient to place it back to the default user.

Note: You may want to change the USER value back.

The requirements.txt file is:

# Step 2

numpy==1.19.2

numba==0.51.2

matplotlib==3.3.2

diagrams==0.17.0

dev and test are subdirectories of the docker subdirectory to the clouds project directory:

# Step 3

.

.

.

|--clouds

|-- docker

|-- dev

|--- Dockerfile

|--- docker-compose.yml

|--- README.md

|--- requirements.txt

|-- test

|--- Dockerfile

|--- docker-compose.yml

|--- README.md

|--- requirements.txt

|-- src

|-- test

|--- requirements.txt

|--- README.md

.

.

Testing Graphviz in a Jupyter notebook

Diagrams depend on the Graphviz runtime. The previous section shows step-by-step how to create a Docker image with Diagrams and Graphviz.

Note: Change the dockerfile to include any Jupyter extensions or delete any extensions you may want.

I run Jupyter notebook inside the dev_1 Docker container. All the Jupyter notebook extensions I want are in the dev_1 Docker container.

(base) Macie:~ brucecottman$ updev [1] 38060

(base) Macie:~ brucecottman$ Creating network "dev_default" with the default driver Creating dev_dev_1 ... done Attaching to dev_dev_1

.

.

dev_1 | Or copy and paste one of these URLs: dev_1 | or http://127.0.0.1:8888/?token=4a9e25ae109156cc1ab8d5f363b990f65e306f93305e62a4

You must actually use port 8889 instead of port 8888 when you copy and paste the URL into a browser:

dev_1 | or http://127.0.0.1:8889/?token=4a9e25ae109156cc1ab8d5f363b990f65e306f93305e62a4

Now when I run the Python code in a Jupyter notebook:



from graphviz import Digraph dot = Digraph(comment='The Round Table') print(dot)

dot.node('A', 'King Arthur')

dot.node('B', 'Sir Bedevere the Wise')

dot.node('L', 'Sir Lancelot the Brave') dot.edges(['AB', 'AL'])

dot.edge('B', 'L', constraint='false')

print(dot.source)

dot.render('test-output/round-table.jpg', view=True)

dot

I get:

Figure 3. Show successful test run of Graphviz

Looks good so far!