> [!WARNING]
>
> **Preview
> --- Cloud Run sandboxes**
>
>
> This feature is
>
> subject to the "Pre-GA Offerings Terms" in the General Service Terms section of the
> [Service Specific
> Terms](https://docs.cloud.google.com/terms/service-terms#1).
>
> Pre-GA features are available "as is" and might have limited support.
>
> For more information, see the
> [launch stage descriptions](https://cloud.google.com/products/#product-launch-stages).

Cloud Run sandboxes provide a fast, secure, and isolated environment to
execute untrusted code or run tools, such as AI agents, within your existing
service in the second-generation environment. Sandboxes are highly optimized for
latency and run within the same instance as your container, sharing its allocated
CPU and memory.

This page describes how to configure sandboxes on your container.
For details about writing code to interact with the sandbox using the CLI, see
[Code execution in Cloud Run](https://docs.cloud.google.com/run/docs/code-execution.md.txt).

## Before you begin

1. [Install and initialize](https://docs.cloud.google.com/sdk/docs.md.txt) the gcloud CLI.
2. Deploy a Cloud Run [service](https://docs.cloud.google.com/run/docs/deploying.md.txt) in the [second generation
   execution environment](https://docs.cloud.google.com/run/docs/configuring/execution-environments).

## Required roles


To get the permissions that
you need to configure and deploy Cloud Run services,

ask your administrator to grant you the
following IAM roles:

- [Cloud Run Developer](https://docs.cloud.google.com/iam/docs/roles-permissions/run.md.txt#run.developer) (`roles/run.developer`) on the Cloud Run service
- [Service Account User](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.serviceAccountUser) (`roles/iam.serviceAccountUser`) on the service identity


If you are deploying a [service](https://docs.cloud.google.com/run/docs/deploying-source-code.md.txt#required_roles)
or [function](https://docs.cloud.google.com/run/docs/deploy-functions.md.txt#required-roles) from source code, you
must also have additional roles granted to you on your project and
Cloud Build service account.

For a list of IAM roles and permissions that are associated with
Cloud Run, see
[Cloud Run IAM roles](https://docs.cloud.google.com/run/docs/reference/iam/roles.md.txt)
and [Cloud Run IAM permissions](https://docs.cloud.google.com/run/docs/reference/iam/permissions.md.txt).
If your Cloud Run service interfaces with
Google Cloud APIs, such as Cloud Client Libraries, see the
[service identity configuration guide](https://docs.cloud.google.com/run/docs/configuring/services/service-identity.md.txt).
For more information about granting roles, see
[deployment permissions](https://docs.cloud.google.com/run/docs/reference/iam/roles.md.txt#additional-configuration)
and [manage access](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).

## Enable sandboxes

Any configuration change leads to the
creation of a new revision. Subsequent revisions will also automatically get
this configuration setting unless you make explicit updates to change it.

When you enable sandboxes, your Cloud Run service deploys in the second-generation execution environment. To enable sandboxes in Cloud Run services, use the Google Cloud CLI or a YAML configuration:

### gcloud

To deploy or update the service, specify the `--sandbox-launcher` flag:

- To deploy a new service, run the following command:

  ```bash
  gcloud beta run deploy SERVICE --image IMAGE_URL --sandbox-launcher
  ```

  Replace the following:
  - <var translate="no">SERVICE</var>: the name of your Cloud Run service.
  - `IMAGE_URL`: a reference to the container image, for example, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry, the [repository](https://docs.cloud.google.com/artifact-registry/docs/repositories/create-repos.md.txt#docker) <var translate="no">REPO_NAME</var> must already be created. The URL follows the format of `LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG`
- To update an existing service, run the following command:

  ```bash
  gcloud beta run services update SERVICE --sandbox-launcher
  ```

### YAML

1. If you are creating a new service, skip this step.
   If you are updating an existing service, download its [YAML configuration](https://docs.cloud.google.com/run/docs/reference/yaml/v1.md.txt):

   ```bash
   gcloud run services describe SERVICE --format export > service.yaml
   ```
2. Update your YAML file to include the `sandboxLauncher` attribute set to `true` inside your container configuration:

   ```yaml
   apiVersion: serving.knative.dev/v1
   kind: Service
   metadata:
     name: SERVICE
     annotations:
       run.googleapis.com/launch-stage: BETA
   spec:
     template:
       spec:
         containers:
         - name: CONTAINER
           image: IMAGE_URL
           sandboxLauncher: true
           port: 8080
   ```

   Replace the following:
   - <var translate="no">SERVICE</var>: the name of your Cloud Run service.
   - <var translate="no">CONTAINER</var>: the name of your container.
   - `IMAGE_URL`: a reference to the container image, for example, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry, the [repository](https://docs.cloud.google.com/artifact-registry/docs/repositories/create-repos.md.txt#docker) <var translate="no">REPO_NAME</var> must already be created. The URL follows the format of `LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG`
3. Create or update the service using the following command:

   ```bash
   gcloud run services replace service.yaml
   ```

   The `gcloud run services replace` command defaults to using `service.yaml` file if present.

Sandboxes share the CPU and memory allocated to the host container. Make
sure your main container's CPU and memory limits can accommodate
both your application and any active sandboxes that you run at the same time.

## Disable sandboxes

To disable the ability to launch a sandbox in your service, use the Google Cloud CLI or a YAML configuration:

### gcloud

Update the service using the `--no-sandbox-launcher` flag by running the following command:

```bash
gcloud beta run services update SERVICE --no-sandbox-launcher
```

Replace <var translate="no">SERVICE</var> with the name of your service.

### YAML

1. If you are creating a new service, skip this step.
   If you are updating an existing service, download its [YAML configuration](https://docs.cloud.google.com/run/docs/reference/yaml/v1.md.txt):

   ```bash
   gcloud run services describe SERVICE --format export > service.yaml
   ```
2. Update your YAML file to remove the `sandboxLauncher` attribute inside your container configuration:

   ```yaml
   apiVersion: serving.knative.dev/v1
   kind: Service
   metadata:
     name: SERVICE
   spec:
     template:
       spec:
         containers:
         - name: CONTAINER
           image: IMAGE_URL
           port: 8080
   ```

   Replace the following:
   - <var translate="no">SERVICE</var>: the name of your Cloud Run service.
   - <var translate="no">CONTAINER</var>: the name of your container.
   - `IMAGE_URL`: a reference to the container image, for example, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry, the [repository](https://docs.cloud.google.com/artifact-registry/docs/repositories/create-repos.md.txt#docker) <var translate="no">REPO_NAME</var> must already be created. The URL follows the format of `LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG`
3. Create or update the service using the following command:

   ```bash
   gcloud run services replace service.yaml
   ```

   The `gcloud run services replace` command defaults to using `service.yaml` file if present.

## View sandbox settings

To view the current sandbox settings for your Cloud Run service:

### Console

1. In the Google Cloud console, go to the Cloud Run **Services** page:

   [Go to Cloud Run](https://console.cloud.google.com/run/services)
2. Click the service to open its **Service details** page.

3. Click the **Revision history** tab.

4. In the **Containers** tab, locate the **Sandbox launcher** setting to view whether sandboxed containers are enabled or disabled.

### gcloud

1. Use the following command:

   ```bash
   gcloud beta run services describe SERVICE
   ```

   Replace <var translate="no">SERVICE</var> with the name of your service.
2. To verify that sandboxed containers are enabled, locate the `sandboxLauncher: true` attribute in the returned configuration.

To run untrusted code in a sandbox from your service, see [Code execution in Cloud Run](https://docs.cloud.google.com/run/docs/code-execution.md.txt).