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
- One-time setup: Create a Google Artifact Registry repository
- Upload the Docker container to the Artifact Registry
- One-time setup: Create a Google Run service
- Manage the service
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:
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
- Go to https://console.cloud.google.com/artifacts for your project.
- Click Create Repository.
- Specify a name for the repository.
- In the Format option, select Docker.
- In Location type, select Region, and specify the region you chose for your Google Cloud SQL instance .
- 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.
- 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.
- Build a local version of the Docker image, following the procedure in Build a local image.
-
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.
-
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. -
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.
-
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:
- Go to https://console.cloud.google.com/artifacts for your project.
- 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.
- Go to https://console.cloud.google.com/run/ for your project.
- Click Create service.
- Keep the default Deploy one revision from an existing container image enabled.
- In the Container image URL field, click Select to open the Select container image window.
- In the list of images that appears, navigate to container image you pushed in the previous step, highlight it and click Select.
- In the Service name field, enter a name for your service.
- In the Region field, select your location.
- Under Authentication, select the relevant option depending on whether your site will be public or not. If it is public, enable Allow unauthenticated invocations.
- Set the following options:
- CPU allocation and pricing: CPU is always allocated
- Service autoscaling > Minimum number of instances: 1
- Expand Container, Volumes, Connections, Security > Container > Settings, and set the following options:
- Resources > Memory: 8 GiB
- Resources > CPU: 2
- Expand the Variables and secrets tab.
- Click the Variables and Secrets tab.
- Click Add variable.
- Add the same environment variables and secrets, with the same names and values as you did when you created the data management run job You can omit the
INPUT_DIR
variable. Add a variable or reference a secret forMAPS_API_KEY
. -
When you are finished, click Done.
- Under Execution environment > Autoscaling, set the following options:
- Minimum number of instances: 1 - Maximum number of instances: 1
- Disable Startup CPU boost.
-
Under Cloud SQL connections click Add connection and select your Cloud SQL instance from the menu.
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.
- Cloud Console
- gcloud CLI
- Go to the https://console.cloud.google.com/run/ page, click on the service you created above, and click Edit & Deploy Revision .
- Select a new container image and click Deploy.
- From any local directory, run the following command:
gcloud run deploy SERVICE_NAME --image CONTAINER_IMAGE_URL
Page last updated: November 21, 2024 • Send feedback about this page