Debug a Cloud Run service in Cloud Code for VS Code

Cloud Code for VS Code lets you easily debug an application deployed to Cloud Run. You don't need to complete manual setup like setting up port forwarding or injecting language-specific debug arguments. You simply need a Cloud Code-ready Cloud Run application that includes a skaffold.yaml configuration file and a launch.json file of type cloudcode.cloudrun.

Debugging your Cloud Run service

To debug your service, follow these steps:

  1. To run your service and attach a debugger session to it, open the Command Palette (click Manage icon Manage > Command Palette or press Ctrl/Cmd+Shift+P) and and then run the Debug on Cloud Run Emulator command.

  2. In the Run/Debug on Cloud Run Emulator dialog, set the specifications for your configuration:

    • Choose Docker or Buildpacks as your builder and then specify your Dockerfile or Buildpack.
    • If you want to specify a Dockerfile target or build arguments, click Show Advanced Build Settings and then specify the target or arguments.
    • If you want to specify a custom service name, container image URL, container port, identity for the service to use, number of dedicated CPUs, or amount of memory to allocate to each container instance, click Show Advanced Service Settings and then specify the values.
    • If you want to specify any environment variables, click Show Advanced Service Settings, click Environment Variables, and then specify the values.
    • If you want to specify Cloud SQL connections, click Connections and then specify one connection per line.
    • If you want VS Code to reestablish a debugging session after you save a change to your code, make sure that the Automatically re-build and re-run on changes checkbox is checked.

    If you want to further customize how your service is run, you can edit your launch.json file directly.

  3. To begin debugging your service, click Debug.

  4. For each debuggable container in your service, confirm or enter the directory in the remote container where the program you'd like to debug is found.

    Alternatively, you can press ESC to skip debugging the container.

    Remote Root prompt

    Cloud Code attaches a debug session for each debuggable container in the service.

    By default, when you save a change to your application, Cloud Code redeploys your application and sets up a new debug session. You can toggle this feature with the watch flag in your project's launch configuration.

  5. To add a breakpoint, click in the editor margin of the file you're debugging.

    Red filled circles signify active breakpoints and gray hollow circles signify disabled breakpoints.

    For finer breakpoint control, you can use the Breakpoints section in VS Code's Debug view.

    Breakpoints section in the left hand panel of Debug View that allows adding, removing, and disabling breakpoints

    When you send a new request to your service, it pauses at the line you specified.

  6. After your session completes, you can use the following contextual menu commands:

    • Open Deployment Logs: Open the application logs of a specific deployment with the Cloud Code Logs Explorer
    • Open Service URL: Open the application service URL of a specific service in a web browser
  7. If you've turned off watch mode in your launch configuration and you want to make changes to your application and rebuild and redeploy the application, in the Development sessions pane, pause on the run action and then click Rebuild and redeploy icon Rebuild and redeploy the application.

  8. If you want to inspect variables and stack info, use the Debug Sidebar. To interact with the debugging session, use the Debug Console in the bottom pane debugger.

  9. To stop your deployment, click the Stop button in the action bar for your current deployment.

    Action bar for Cloud Run deployment

Getting Support

To send feedback, report issues on GitHub, or ask a question on Stack Overflow.