Step-by-Step Guide to Fix Graphviz Issues on Google Cloud Run

Punita GoelPunita Goel
2 min read

Deploying a Python app on Google Cloud Run with Graphviz visualizations can trigger:

graphviz.backend.execute.ExecutableNotFound: failed to execute PosixPath('dot'), make sure the Graphviz executables are on your systems' PATH

This happens because Cloud Run’s default build misses the dot executable, even with a Dockerfile. Here’s a quick fix.

Step 1: Set Up Dockerfile

Create a Dockerfile to install Graphviz:

FROM python:3.11-slim
WORKDIR /app
RUN apt-get update && apt-get install -y graphviz
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["bash", "-c", "dot -V && python app.py"]
  • Uses Python 3.11 and installs dot.

  • Add graphviz to requirements.txt manually.

  • The CMD checks dot at runtime.

Step 2: Configure cloudbuild.yaml

Since the autogenerated Cloud Build doesn’t read the Dockerfile by default, update cloudbuild.yaml with debug and build steps:

steps:
- name: 'python:3.11'
  args:
    - '-c'
    - >
      apt-get update -y && \
      apt-get install -y graphviz && \
      pip install --no-cache-dir graphviz && \
      python -c 'import graphviz; from graphviz import Digraph; print("Graphviz Digraph imported:", Digraph)'
  dir: .
  id: install-and-debug
  entrypoint: bash
- name: 'gcr.io/cloud-builders/docker'
  args:
    - '-c'
    - |
      ls -l Dockerfile || echo "Dockerfile not found!"
      cat Dockerfile || echo "Failed to read Dockerfile!"
  id: debug-dockerfile
  entrypoint: bash
- name: 'gcr.io/cloud-builders/docker'
  args:
    - build
    - '-f'
    - Dockerfile
    - '-t'
    - >-
$_AR_HOSTNAME/$_AR_PROJECT_ID/$_AR_REPOSITORY/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA
    - .
  dir: .
  id: build-image
  • install-and-debug: Installs Graphviz and tests the Python import.

  • debug-dockerfile: Checks if the Dockerfile exists and reads it.

  • build-image: Builds the image using the Dockerfile, tagging it with your project details.

Step 3: Deploy and Fix Runtime

Deploy with:

gcloud run deploy SERVICE_NAME --source . --platform managed --region REGION --no-cache

If the error persists, add to app.py:

import os
os.environ["PATH"] = "/usr/bin:" + os.environ.get("PATH", "")

Check logs (gcloud run logs read) to confirm dot works.

Why It Works

The cloudbuild.yaml ensures Graphviz is installed, the Dockerfile sets it up for runtime, and the PATH fix resolves any execution issues.

For Your Project

This fix enables your project’s visualizations on Google Cloud.

Quick and done—test it now!

0
Subscribe to my newsletter

Read articles from Punita Goel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Punita Goel
Punita Goel