0
0
GCPcloud~5 mins

Deploying container images in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Deploying container images means running your app inside a container on the cloud. This helps you share your app easily and run it anywhere without setup problems.
When you want to run your web app on Google Cloud without managing servers.
When you have built a container image locally and want to make it available on Google Cloud.
When you want to update your app by deploying a new container image version.
When you want to run multiple copies of your app for better availability.
When you want to use Google Cloud Run to run containers without managing infrastructure.
Config File - service.yaml
service.yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: example-service
  namespace: default
spec:
  template:
    spec:
      containers:
      - image: gcr.io/google-samples/hello-app:1.0
        ports:
        - containerPort: 8080

This file defines a Cloud Run service using a container image.

apiVersion: The API version for Cloud Run.

kind: The resource type, here a Service.

metadata: Names the service and sets the namespace.

spec: Describes the container image to run and the port it listens on.

Commands
This command logs you into your Google Cloud account so you can run commands and deploy resources.
Terminal
gcloud auth login
Expected OutputExpected
Your browser has been opened to visit: https://accounts.google.com/o/oauth2/auth?... You are now logged in as [user@example.com]. To set the active project, run: $ gcloud config set project PROJECT_ID
Sets the active Google Cloud project where your container will be deployed.
Terminal
gcloud config set project example-project
Expected OutputExpected
Updated property [core/project].
Builds your container image from the current folder and uploads it to Google Container Registry with the given tag.
Terminal
gcloud builds submit --tag gcr.io/example-project/hello-app:v1 .
Expected OutputExpected
[BUILD OUTPUT] Creating temporary tarball archive of 10 files... Uploading tarball of [.] to [gs://example-project_cloudbuild/source/...]... Starting build... Step 1/3 : FROM python:3.9-slim ---> abcdef123456 Step 2/3 : COPY . /app ---> Using cache Step 3/3 : CMD ["python", "app.py"] ---> Using cache Successfully built abcdef123456 Successfully tagged gcr.io/example-project/hello-app:v1 Pushed gcr.io/example-project/hello-app:v1
--tag - Specifies the name and tag of the container image to create.
Deploys the container image to Cloud Run as a managed service, making it available on the internet.
Terminal
gcloud run deploy example-service --image gcr.io/example-project/hello-app:v1 --platform managed --region us-central1 --allow-unauthenticated
Expected OutputExpected
Deploying container to Cloud Run service [example-service] in project [example-project] region [us-central1] Allow unauthenticated invocations to [example-service] (y/N)? y Creating Revision... Routing traffic... Service [example-service] revision [example-service-00001-abc] has been deployed and is serving 100 percent of traffic. Service URL: https://example-service-abc123.run.app
--image - Specifies the container image to deploy.
--platform - Specifies the Cloud Run platform type.
--allow-unauthenticated - Allows public access to the service.
Shows details about the deployed Cloud Run service to verify it is running.
Terminal
gcloud run services describe example-service --platform managed --region us-central1
Expected OutputExpected
apiVersion: serving.knative.dev/v1 kind: Service metadata: name: example-service namespace: default spec: template: spec: containers: - image: gcr.io/example-project/hello-app:v1 status: url: https://example-service-abc123.run.app conditions: - type: Ready status: True
--platform - Specifies the Cloud Run platform type.
Key Concept

If you remember nothing else from this pattern, remember: build your container image, upload it to Google Container Registry, then deploy it to Cloud Run to run your app on Google Cloud.

Common Mistakes
Not setting the correct Google Cloud project before deploying.
The deployment will go to the wrong project or fail if the project is not set.
Always run 'gcloud config set project your-project-id' before deploying.
Forgetting to allow unauthenticated access when deploying Cloud Run service.
Your service will not be accessible publicly and will return permission errors.
Use the '--allow-unauthenticated' flag during deployment if you want public access.
Using an incorrect or missing container image tag in the deploy command.
Cloud Run will fail to find the image and the deployment will fail.
Make sure the image tag matches the one pushed to Container Registry.
Summary
Use 'gcloud builds submit' to build and upload your container image to Google Container Registry.
Deploy the container image to Cloud Run with 'gcloud run deploy' to run your app on Google Cloud.
Verify your deployment with 'gcloud run services describe' to check the service status and URL.