The user makes an HTTP request to the Cloud Run service which executes a Graphviz utility to transform the request into an image. That image is delivered to the user as the HTTP response.
Dockerfile
Your Dockerfile
is specific to the language and base operating environment, such as Ubuntu, that your service will use.
The Build and Deploy Quickstart shows various Dockerfiles
that can be used as a starting point to build a Dockerfile
for other services.
This service requires one or more additional system packages not available by default.
Open the Dockerfile
in an editor.
Look for a Dockerfile
RUN
statement. This statement allows running arbitrary shell commands to modify the environment. If the Dockerfile
has multiple stages, identified by finding multiple FROM
statements, it will be found in the last stage.
The specific packages required and the mechanism to install them varies by the operating system declared inside the container.
To get instructions for your operating system or base image, click the appropriate tab.
To determine the operating system of your container image, check the name in the FROM
statement or a README associated with your base image. For example, if you extend from node
, you can find documentation and the parent Dockerfile
on Docker Hub.
Test your customization by building the image, using docker build
locally or Cloud Build.
The sample service uses parameters from the incoming HTTP request to invoke a system call that executes the appropriate dot
utility command.
In the HTTP handler below, a graph description input parameter is extracted from the dot
querystring variable.
Graph descriptions can include characters which must be URL encoded for use in a querystring.
You'll need to differentiate between internal server errors and invalid user input. This sample service returns an Internal Server Error for all dot command-line errors unless the error message contains the string syntax
, which indicates a user input problem.
The core logic of diagram generation uses the dot command-line tool to process the graph description input parameter into a diagram in the PNG image format.
Any vulnerabilities in the dot
tool are potential vulnerabilities of the web service. You can mitigate this by using up-to-date versions of the graphviz
package through re-building the container image on a regular basis.
If you extend the current sample to accept user input as command-line parameters, you should protect against command-injection attacks. Some of the ways to prevent injection attacks include:
You can further mitigate potential vulnerabilities by deploying the service with a service account that has not been granted any permissions to use Google Cloud services, rather than using the default account, which has commonly used permissions. For that reason, the steps in this tutorial create and use a new service account.
To ship your code, you build with Cloud Build, and upload to Artifact Registry, and deploy to Cloud Run:
Create an Artifact Registry:
gcloudartifactsrepositoriescreateREPOSITORY\--repository-formatdocker\--locationREGION
Replace:
Run the following command to build your container and publish on Artifact Registry.
gcloudbuildssubmit--tagREGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/graphviz
Where PROJECT_ID is your Google Cloud project ID, and graphviz
is the name you want to give your service.
Upon success, you will see a SUCCESS message containing the ID, creation time, and image name. The image is stored in Artifact Registry and can be re-used if desired.
gcloudbuildssubmit--tagREGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/graphviz
Where PROJECT_ID is your Google Cloud project ID, and graphviz
is the name you want to give your service.
Upon success, you will see a SUCCESS message containing the ID, creation time, and image name. The image is stored in Artifact Registry and can be reused if desired.
gcloudbuildssubmit--tagREGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/graphviz
Where PROJECT_ID is your Google Cloud project ID, and graphviz
is the name you want to give your service.
Upon success, you will see a SUCCESS message containing the ID, creation time, and image name. The image is stored in Artifact Registry and can be reused if desired.
Using the Dockerfile, configure and build a base image with the system packages installed to override Jib's default base image:
gcloudbuildssubmit--tagREGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/graphviz-base
Where PROJECT_ID is your Google Cloud project ID.
Use the gcloud credential helper to authorize Docker to push to your Artifact Registry.
gcloudauthconfigure-docker
Build your final container with Jib and publish on Artifact Registry:
mvncompilejib:build\-Dimage=REGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/graphviz\-Djib.from.image=REGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/graphviz-base
Where PROJECT_ID is your Google Cloud project ID.
Deploy using the following:
gcloudiamservice-accountscreateSA_NAME
gcloudrundeploygraphviz-web--service-accountSA_NAME@PROJECT_ID.iam.gserviceaccount.com--imageREGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/graphviz
graphviz
is the name of the container from above and graphviz-web
is the name of the service. Respond Y
to the "allow unauthenticated" prompt. See Managing access for more details on IAM-based authentication.To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.
The following Terraform code creates a Cloud Run service.
Replace IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest
. If you use Artifact Registry, the repositoryREPO_NAME must already be created. The URL has the shape LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
.
The following Terraform code makes your Cloud Run service public.
If you want to deploy a code update to the service, repeat the previous steps. Each deployment to a service creates a new revision and automatically starts serving traffic when ready.
Try out your service by sending HTTP POST
requests with DOT syntax descriptions in the request payload.
Send an HTTP request to your service.
Copy the URL into your browser URL bar and update [SERVICE_DOMAIN]
:
https://SERVICE_DOMAIN/diagram.png?dot=digraphRun{rankdir=LRCode->Build->Deploy->Run}
You can embed the diagram in a web page:
<imgsrc="https://SERVICE_DOMAIN/diagram.png?dot=digraph Run { rankdir=LR Code -> Build -> Deploy -> Run }"/>
Open the resulting diagram.png
file in any application that supports PNG
files, such as Chrome.
It should look like this:
You can explore a small collection of ready-made diagram descriptions.
.dot
fileSend an HTTP request to your service.
Copy the URL into your browser URL bar
https://SERVICE_DOMAIN/diagram.png?dot=SELECTEDDOTFILECONTENTS
If you created a new project for this tutorial, delete the project. If you used an existing project and wish to keep it without the changes added in this tutorial, delete resources created for the tutorial.
The easiest way to eliminate billing is to delete the project that you created for the tutorial.
To delete the project:
Delete the Cloud Run service you deployed in this tutorial:
gcloudrunservicesdeleteSERVICE-NAME
Where SERVICE-NAME is your chosen service name.
You can also delete Cloud Run services from the Google Cloud console.
Remove the gcloud default region configuration you added during tutorial setup:
gcloudconfigunsetrun/region
Remove the project configuration:
gcloud config unset project
Delete other Google Cloud resources created in this tutorial:
Delete the container image named REGION-docker.pkg.dev/PROJECT_ID/REPOSITORY/graphviz
from Artifact Registry.
Delete the service account SA_NAME.
gcloudiamservice-accountsdeleteSA_NAME@PROJECT_ID.iam.gserviceaccount.com
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-04-17 UTC.