This commit is contained in:
MuratovAS 2024-01-13 21:31:01 +05:00
parent bb23b21806
commit 739a7a1527
No known key found for this signature in database
GPG key ID: CE09BE03367711AD
9 changed files with 53 additions and 54 deletions

View file

@ -1,3 +1,4 @@
.* .*
test/ test/
example.png example.png
docker-compose.yml

View file

@ -1,12 +0,0 @@
version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
ignore:
- dependency-name: "*"
update-types: ["version-update:semver-patch"]
- dependency-name: "*"
update-types: ["version-update:semver-minor"]

View file

@ -1,5 +1,6 @@
Copyright (c) 2018 Eugene Agafonov Copyright (c) 2018 Eugene Agafonov
Copyright (c) 2023 e-dant (github.com/e-dant) Copyright (c) 2023 e-dant (github.com/e-dant)
Copyright (c) 2024 MuratovAS
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View file

@ -3,20 +3,36 @@
Visualize the relationship between Docker networks and containers Visualize the relationship between Docker networks and containers
as a neat graphviz graph. as a neat graphviz graph.
This repository fork [e-dant/docker-network-graph](https://github.com/e-dant/docker-network-graph)
Changes:
- Improved design
- Added the ability to generate url
- Added display of connections with the host
## Example ## Example
![example graph](https://raw.githubusercontent.com/e-dant/docker-network-graph/release/example.png) ![example graph](./example.png)
## Usage ## Usage
usage: docker-network-graph.py [-h] [-v] [-o OUT] usage: docker-network-graph.py [-h] [-v] [-o OUT] [-u]
Visualize docker networks. Visualize docker networks.
optional arguments: optional arguments:
-h, --help show this help message and exit -h, --help Show this help message and exit
-v, --verbose Verbose output -v, --verbose Verbose output
-o OUT, --out OUT Write output to file -o OUT, --out OUT Write output to file [not supported by container]
-u, --url Generate link for GraphvizOnline
## Running inside docker
If you want to generate a graph for a remote system you can also easily
run this script inside a pre-built docker container:
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock e-dant/docker-network-graph -u
For more advanced use cases you can append arguments to the `docker run`
command as if you were running it in a local shell.
## Running local
In most cases what you want to run are the following couple commands: In most cases what you want to run are the following couple commands:
git clone https://github.com/e-dant/docker-network-graph.git git clone https://github.com/e-dant/docker-network-graph.git
@ -26,12 +42,6 @@ In most cases what you want to run are the following couple commands:
This will generate an .svg file containing the graph. This will generate an .svg file containing the graph.
## Running inside docker
If you want to generate a graph for a remote system you can also easily
run this script inside a pre-built docker container:
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock e-dant/docker-network-graph
This will just generate and output the graph in the [DOT Language][dot]. This will just generate and output the graph in the [DOT Language][dot].
You can then paste that code into [GraphvizOnline][gvonline] You can then paste that code into [GraphvizOnline][gvonline]
to render it. The recommended rendering engine is `fdp`. to render it. The recommended rendering engine is `fdp`.
@ -41,15 +51,12 @@ Alternatively, if you prefer to render locally, you can run
paste the previous output there, press enter and finally CTRL+C to paste the previous output there, press enter and finally CTRL+C to
generate the file. generate the file.
For more advanced use cases you can append arguments to the `docker run`
command as if you were running it in a local shell.
[dot]: https://www.graphviz.org/doc/info/lang.html
[gvonline]: https://dreampuf.github.io/GraphvizOnline/
## Development ## Development
If you'd like to contribute to this project, there is a sample docker-compose file If you'd like to contribute to this project, there is a sample docker-compose file
using dummy containers in `test`. using dummy containers in `test`.
You can deploy it using `docker-compose -f test/docker-compose.yml up -d`. You can deploy it using `docker-compose -f docker-compose.yml up -d`.
## Credit
[dot]: https://www.graphviz.org/doc/info/lang.html
[gvonline]: https://dreampuf.github.io/GraphvizOnline/

View file

@ -5,6 +5,7 @@ import argparse
import random import random
import docker import docker
import typing import typing
import urllib.parse
from dataclasses import dataclass from dataclasses import dataclass
from graphviz import Graph from graphviz import Graph
from graphviz.parameters.formats import FORMATS from graphviz.parameters.formats import FORMATS
@ -152,7 +153,8 @@ def get_containers(
def draw_network(g: Graph, net: Network): def draw_network(g: Graph, net: Network):
label = f"{{<gw_iface> {net.gateway} | {net.name}" # <gw_iface> {net.gateway} |
label = f"{{{net.name}"
if net.internal: if net.internal:
label += " | Internal" label += " | Internal"
if net.isolated: if net.isolated:
@ -163,8 +165,8 @@ def draw_network(g: Graph, net: Network):
f"network_{net.name}", f"network_{net.name}",
shape="record", shape="record",
label=label, label=label,
fillcolor=net.color, color=net.color + "60",
style="filled", style="filled,rounded",
) )
@ -186,7 +188,7 @@ def draw_container(g: Graph, c: Container):
f"container_{c.container_id}", f"container_{c.container_id}",
shape="record", shape="record",
label=label, label=label,
fillcolor="#ff9999", fillcolor="#cdcdcd",
style="filled", style="filled",
) )
@ -199,7 +201,7 @@ def draw_link(g: Graph, networks: typing.Dict[str, Network], link: Link):
) )
def generate_graph(verbose: bool, file: str): def generate_graph(verbose: bool, file: str, url: str):
docker_client = docker.from_env() docker_client = docker.from_env()
networks = get_networks(docker_client, verbose) networks = get_networks(docker_client, verbose)
@ -230,10 +232,23 @@ def generate_graph(verbose: bool, file: str):
if link.network_name != "none": if link.network_name != "none":
draw_link(g, networks, link) draw_link(g, networks, link)
for _, network in networks.items():
if network.internal != True:
if network.name != "host":
g.edge(
f"network_{network.name}",
f"network_host",
color="#808080",
style="dotted",
)
if file: if file:
g.render(base) g.render(base)
else: else:
print(g.source) if url:
print("https://dreampuf.github.io/GraphvizOnline/#" + urllib.parse.quote(g.source))
else:
print(g.source)
def graphviz_output_file(filename: str): def graphviz_output_file(filename: str):
@ -242,13 +257,12 @@ def graphviz_output_file(filename: str):
raise argparse.ArgumentTypeError("Must be valid graphviz output format") raise argparse.ArgumentTypeError("Must be valid graphviz output format")
return filename return filename
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Visualize docker networks.") parser = argparse.ArgumentParser(description="Visualize docker networks.")
parser.add_argument("-v", "--verbose", help="Verbose output", action="store_true") parser.add_argument("-v", "--verbose", help="verbose output", action="store_true")
parser.add_argument( parser.add_argument("-o", "--out", help="write output to file", type=graphviz_output_file)
"-o", "--out", help="Write output to file", type=graphviz_output_file parser.add_argument("-u", "--url", help="generate link for GraphvizOnline", action="store_true")
)
args = parser.parse_args() args = parser.parse_args()
generate_graph(args.verbose, args.out) generate_graph(args.verbose, args.out, args.url)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 111 KiB

After

Width:  |  Height:  |  Size: 85 KiB

View file

@ -1,6 +0,0 @@
#! /usr/bin/env bash
(
cd "$(dirname "$0")/.." \
&& pipenv install
)

View file

@ -1,6 +0,0 @@
#! /usr/bin/env bash
(
cd "$(dirname "$0")/.." \
&& pipenv run python docker-network-graph.py -o output.svg
)