Preview

This product or feature is subject to the "Pre-GA Offerings Terms" in the General Service Terms section of theService Specific Terms. Pre-GA products and features are available "as is" and might have limited support. For more information, see thelaunch stage descriptions.

Create a startup script

This page describes how to create a startup script for the web service and then validate the script.

To ensure that bringing up the web service does not require manual intervention, you must create a startup script.The startup script does the following tasks:

  • Reads the virtual machine (VM) metadata and sets the environment variable for metadata with theCONNECTOR_ENV prefix. Any data required by consumers is taken during VM creation from Marketplace and is set as environment variables in docker. These environment variables can then be read and processed accordingly in the application.
  • Starts the docker container containing the web service with the appropriate environment variables.

The following code is a sample startup script:

#!/bin/bash# 1. Fetch Metadata Keysmetadata_keys_url="http://metadata.google.internal/computeMetadata/v1/instance/attributes/"metadata_keys=$(curl-H"Metadata-Flavor: Google""$metadata_keys_url")# 2. Set Environment Variables for CONNECTOR_ENV Keys (with error handling)forkeyin$metadata_keys;doif[[$key==CONNECTOR_ENV_*]];thenmetadata_value_url="http://metadata.google.internal/computeMetadata/v1/instance/attributes/$key"# Fetch value with error handlingvalue=$(curl-H"Metadata-Flavor: Google""$metadata_value_url"2>/dev/null)if[[-z"$value"]];thenecho"Warning: No value found for key '$key'. Skipping.">&2# Log the warning to stderrcontinue# Skip to the next iterationfiexport"$key=$value"fidone# 3. Run Docker with Environment Variablessudodockerstopconnector-service||truesudodockerrun\--nameconnector-service\$(env|grepCONNECTOR_ENV_|sed's/=/="/;s/$/"/'|sed's/^/-e /')\-d-p$CONNECTOR_ENV_PORT:$CONNECTOR_ENV_PORT\--restart=unless-stopped\connector-container

Validate the startup script

  1. In the VM instance, add the metadata for the port and all other parameters which are required during VM creation.
    gcloudcomputeinstancesadd-metadataVM_NAME\--zone=VM_ZONE\--project=PROJECT_NAME\--metadata=CONNECTOR_ENV_PORT=8081
  2. Edit the VM from UI and add the startup script mentioned in the automation section.

    You can also use the following gcloud command:

    gcloudcomputeinstancesadd-metadataVM_NAME\--zone=VM_ZONE\--project=PROJECT_NAME\--metadata-from-filestartup-script=gcp-start.sh
  3. After adding the startup script, ssh to the VM and run the following command:
    sudogoogle_metadata_script_runnerstartup
  4. Run the following command to ensure that the docker container is running on the mentioned port.
    sudodockerps
  5. Run the following command to stop and remove the docker service. After testing, the service must not be running on the VM.
    sudodockerrm-fconnector-service
  6. What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-19 UTC.