diff --git a/.dockerignore b/.dockerignore index ab932c6..3799ed0 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,4 @@ .* test/ example.png +docker-compose.yml \ No newline at end of file diff --git a/.github/dependabot.yml b/.github/dependabot.yml deleted file mode 100644 index 659205c..0000000 --- a/.github/dependabot.yml +++ /dev/null @@ -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"] diff --git a/LICENSE b/LICENSE index 686f034..05166e4 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,6 @@ Copyright (c) 2018 Eugene Agafonov 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 of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 3e6a036..b04e992 100644 --- a/README.md +++ b/README.md @@ -3,20 +3,36 @@ Visualize the relationship between Docker networks and containers 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 graph](https://raw.githubusercontent.com/e-dant/docker-network-graph/release/example.png) +![example graph](./example.png) ## Usage - usage: docker-network-graph.py [-h] [-v] [-o OUT] + usage: docker-network-graph.py [-h] [-v] [-o OUT] [-u] Visualize docker networks. optional arguments: - -h, --help show this help message and exit + -h, --help Show this help message and exit -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: 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. -## 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]. You can then paste that code into [GraphvizOnline][gvonline] 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 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 If you'd like to contribute to this project, there is a sample docker-compose file 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/ diff --git a/test/docker-compose.yml b/docker-compose.yml similarity index 100% rename from test/docker-compose.yml rename to docker-compose.yml diff --git a/docker-network-graph.py b/docker-network-graph.py index 96ac1f1..9e4f530 100755 --- a/docker-network-graph.py +++ b/docker-network-graph.py @@ -5,6 +5,7 @@ import argparse import random import docker import typing +import urllib.parse from dataclasses import dataclass from graphviz import Graph from graphviz.parameters.formats import FORMATS @@ -152,7 +153,8 @@ def get_containers( def draw_network(g: Graph, net: Network): - label = f"{{ {net.gateway} | {net.name}" + # {net.gateway} | + label = f"{{{net.name}" if net.internal: label += " | Internal" if net.isolated: @@ -163,8 +165,8 @@ def draw_network(g: Graph, net: Network): f"network_{net.name}", shape="record", label=label, - fillcolor=net.color, - style="filled", + color=net.color + "60", + style="filled,rounded", ) @@ -186,7 +188,7 @@ def draw_container(g: Graph, c: Container): f"container_{c.container_id}", shape="record", label=label, - fillcolor="#ff9999", + fillcolor="#cdcdcd", 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() networks = get_networks(docker_client, verbose) @@ -230,10 +232,23 @@ def generate_graph(verbose: bool, file: str): if link.network_name != "none": 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: g.render(base) 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): @@ -242,13 +257,12 @@ def graphviz_output_file(filename: str): raise argparse.ArgumentTypeError("Must be valid graphviz output format") return filename - + if __name__ == "__main__": parser = argparse.ArgumentParser(description="Visualize docker networks.") - parser.add_argument("-v", "--verbose", help="Verbose output", action="store_true") - parser.add_argument( - "-o", "--out", help="Write output to file", type=graphviz_output_file - ) + parser.add_argument("-v", "--verbose", help="verbose output", action="store_true") + parser.add_argument("-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() - generate_graph(args.verbose, args.out) + generate_graph(args.verbose, args.out, args.url) \ No newline at end of file diff --git a/example.png b/example.png index 9cc05bf..6f4bf39 100644 Binary files a/example.png and b/example.png differ diff --git a/tool/build b/tool/build deleted file mode 100755 index 631dd8f..0000000 --- a/tool/build +++ /dev/null @@ -1,6 +0,0 @@ -#! /usr/bin/env bash - -( - cd "$(dirname "$0")/.." \ - && pipenv install -) diff --git a/tool/run b/tool/run deleted file mode 100755 index b1af30f..0000000 --- a/tool/run +++ /dev/null @@ -1,6 +0,0 @@ -#! /usr/bin/env bash - -( - cd "$(dirname "$0")/.." \ - && pipenv run python docker-network-graph.py -o output.svg -)