Generate a diagram with the dot tool from the graphviz package

Use a customized Dockerfile to configure system packages whose command-line utilities are used as part of serving HTTP requests.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample


// createDiagram generates a diagram image from the provided io.Reader written to the io.Writer.
func createDiagram(w io.Writer, r io.Reader) error {
	stderr := new(bytes.Buffer)
	args := []string{
		"-Glabel=Made on Cloud Run",
	cmd := exec.Command("/usr/bin/dot", args...)
	cmd.Stdin = r
	cmd.Stdout = w
	cmd.Stderr = stderr

	if err := cmd.Run(); err != nil {
		return fmt.Errorf("exec(%s) failed (%v): %s", cmd.Path, err, stderr.String())

	return nil


// Generate a diagram based on a graphviz DOT diagram description.
public static InputStream createDiagram(String dot) {
  if (dot == null || dot.isEmpty()) {
    throw new NullPointerException("syntax: no graphviz definition provided");
  // Adds a watermark to the dot graphic.
  List<String> args = new ArrayList<String>();
  args.add("-Glabel=\"Made on Cloud Run\"");

  StringBuilder output = new StringBuilder();
  InputStream stdout = null;
  try {
    ProcessBuilder pb = new ProcessBuilder(args);
    Process process = pb.start();
    OutputStream stdin = process.getOutputStream();
    stdout = process.getInputStream();
    // The Graphviz dot program reads from stdin.
    Writer writer = new OutputStreamWriter(stdin, "UTF-8");
  } catch (Exception e) {
  return stdout;


// Generate a diagram based on a graphviz DOT diagram description.
const createDiagram = dot => {
  if (!dot) {
    throw new Error('syntax: no graphviz definition provided');

  // Adds a watermark to the dot graphic.
  const dotFlags = [
    '-Glabel="Made on Cloud Run"',
  ].join(' ');

  const image = execSync(`/usr/bin/dot ${dotFlags} -Tpng`, {
    input: dot,
  return image;


def create_diagram(dot):
    # Generates a diagram based on a graphviz DOT diagram description.
    if not dot:
        raise Exception("syntax: no graphviz definition provided")

    dot_args = [  # These args add a watermark to the dot graphic.
        "-Glabel=Made on Cloud Run",

    # Uses local `dot` binary from Graphviz:
    image =
        ["dot"] + dot_args, input=dot.encode("utf-8"), stdout=subprocess.PIPE

    if not image:
        raise Exception("syntax: bad graphviz definition provided")
    return image

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.