Deploy services to Google Cloud

This page shows you how to build a custom services Docker container as a GCP artifact, upload it to the Artifact Registry, and create a Google Cloud Run service. This is step 5 of the recommended workflow.

System overview

When you are ready to host your custom Data Commons site in production, you create a Google Cloud Run service for the site. This is the production setup:

services setup

You push a locally built Docker image to the Google Cloud Artifact Registry, and then deploy the image as a Cloud Run service.

One-time setup: Create a Google Artifact Registry repository

  1. Go to https://console.cloud.google.com/artifacts for your project.
  2. Click Create Repository.
  3. Specify a name for the repository.
  4. In the Format option, select Docker.
  5. In Location type, select Region, and specify the region you chose for your Google Cloud SQL instance .
  6. Enable or disable Immutable image tags according to the workflow you prefer; that is, if you want to be able to reuse the same Docker tag for new images, keep this option disabled.
  7. Click Create.

Upload the Docker container to the Artifact Registry

This procedure creates a “dev” Docker package that you upload to the Google Cloud Artifact Registry. Any time you rebuild the image and want to deploy it to the cloud, you need to rerun this procedure.

  1. Build a local version of the Docker image, following the procedure in Build a local image.
  2. Authenticate to gcloud:

    gcloud auth login
    

    This opens a browser window that prompts you to enter credentials, sign in to Google Cloud SDK and allow Google Cloud SDK to access your account. Accept the prompts.

  3. Generate credentials for the Docker package you will build in the next step. Docker package names must be in the format LOCATION-docker-pkg.dev, where the LOCATION is the region you have selected in the repository creation step previously; for example, us-central1.

      
     gcloud auth configure-docker LOCATION-docker.pkg.dev
    

    When prompted to confirm creating the credentials file, click Y to accept.

  4. Create a package from the source image created in step 1:

     
    docker tag SOURCE_IMAGE_NAME:SOURCE_IMAGE_TAG \  
    LOCATION-docker.pkg.dev/PROJECT_ID/ARTIFACT_REPO/TARGET_IMAGE_NAME:TARGET_IMAGE_TAG  
    
    • The artifact repo must be an Artifact Registry repository you have created previously.
    • The target image name and tag can be the same as the source or different.
  5. Push the image to the registry:

    docker push LOCATION-docker.pkg.dev/PROJECT_ID/ARTIFACT_REPO/TARGET_IMAGE_NAME:TARGET_IMAGE_TAG  
    

This will take several minutes to upload.

When it completes, verify that the container has been uploaded in the Cloud Console:

  1. Go to https://console.cloud.google.com/artifacts for your project.
  2. In the list of repositories, click on the one you created above. Under Repository Details, you should see the Docker image listed.

One-time setup: Create a Google Run service

This procedure shows you how to create a service and set environment variables using the Cloud Console.

See also Deploying to Cloud Run for more information on all the options you may set on your service.

  1. Go to https://console.cloud.google.com/run/ for your project.
  2. Click Create service.
  3. Keep the default Deploy one revision from an existing container image enabled.
  4. In the Container image URL field, click Select to open the Select container image window.
  5. In the list of images that appears, navigate to container image you pushed in the previous step, highlight it and click Select.
  6. In the Service name field, enter a name for your service.
  7. In the Region field, select your location.
  8. Under Authentication, select the relevant option depending on whether your site will be public or not. If it is public, enable Allow unauthenticated invocations.
  9. Set the following options:
    • CPU allocation and pricing: CPU is always allocated
    • Service autoscaling > Minimum number of instances: 1

    Cloud Run service

  10. Expand Container, Volumes, Connections, Security > Container > Settings, and set the following options:
    • Resources > Memory: 8 GiB
    • Resources > CPU: 2

    Cloud Run service

  11. Expand the Variables and secrets tab.
  12. Click the Variables and Secrets tab.
  13. Click Add variable.
  14. Add the same environment variables, with the same names and values as you did when you created the data management run job You can omit the INPUT_DIR variable.
  15. Add a variable for the MAPS_API_KEY and set it to your Maps API key.
  16. When you are finished, click Done.

    Cloud Run service

  17. Under Execution environment > Autoscaling, set the following options:
    • Minimum number of instances: 1 - Maximum number of instances: 1
  18. Disable Startup CPU boost.
  19. Under Cloud SQL connections click Add connection and select your Cloud SQL instance from the menu.

    Cloud Run service

Click Create to kick off the deployment. Click the Logs tab to see the status details of the operation. Once it completes, a link to the deployed image URL is listed at the top of the page. Click on the link to see the running instance.

Manage the service

Every time you make changes to the code and release a new Docker artifact, or rerun the data management job, you need to restart the service as well. To do so:

  1. Go to the https://console.cloud.google.com/run/ page, click on the service you created above, and click Edit & Deploy Revision.
  2. Select a new container and click Deploy.