Bird
Raised Fist0
GCPcloud~5 mins

Deploying container images in GCP - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of deploying a container image on Google Cloud Run?
easy
A. To run applications without managing servers
B. To create virtual machines manually
C. To store data in a database
D. To write code directly in the cloud console

Solution

  1. Step 1: Understand what Cloud Run does

    Cloud Run runs containerized apps without needing you to manage servers.
  2. Step 2: Compare options to Cloud Run's purpose

    Options B, C, and D describe other cloud tasks, not Cloud Run's main use.
  3. Final Answer:

    To run applications without managing servers -> Option A
  4. Quick Check:

    Cloud Run = serverless app running [OK]
Hint: Cloud Run runs apps without servers [OK]
Common Mistakes:
  • Confusing Cloud Run with VM creation
  • Thinking Cloud Run stores data
  • Assuming Cloud Run is a coding environment
2. Which command correctly deploys a container image to Cloud Run in the us-central1 region?
easy
A. gcloud deploy run myapp --image=gcr.io/myproject/myimage --zone=us-central1
B. gcloud run deploy myapp --image=gcr.io/myproject/myimage --region=us-central1
C. gcloud run create myapp --image=gcr.io/myproject/myimage --zone=us-central1
D. gcloud run deploy --image=myimage gcr.io/myproject --region=us-central1

Solution

  1. Step 1: Identify correct gcloud command syntax

    The correct command is 'gcloud run deploy' followed by service name, image, and region.
  2. Step 2: Check each option for syntax errors

    gcloud run deploy myapp --image=gcr.io/myproject/myimage --region=us-central1 matches correct syntax. Options B, C, and D have wrong commands or argument order.
  3. Final Answer:

    gcloud run deploy myapp --image=gcr.io/myproject/myimage --region=us-central1 -> Option B
  4. Quick Check:

    Correct deploy syntax = gcloud run deploy myapp --image=gcr.io/myproject/myimage --region=us-central1 [OK]
Hint: Use 'gcloud run deploy' with --image and --region [OK]
Common Mistakes:
  • Using 'gcloud deploy run' instead of 'gcloud run deploy'
  • Confusing --zone with --region
  • Incorrect argument order
3. Given this command:
gcloud run deploy myservice --image=gcr.io/myproj/app:v1 --region=us-east1
What happens after running it?
medium
A. The container image is deployed as a Cloud Run service named 'myservice' in us-east1 region
B. A virtual machine named 'myservice' is created in us-east1
C. The image is uploaded to Google Container Registry only
D. The command fails because region must be us-central1

Solution

  1. Step 1: Understand the deploy command effect

    The command deploys the container image as a Cloud Run service with the given name and region.
  2. Step 2: Evaluate other options

    B is wrong because no VM is created. C is wrong because deployment runs the image, not just upload. A is wrong because us-east1 is a valid region.
  3. Final Answer:

    The container image is deployed as a Cloud Run service named 'myservice' in us-east1 region -> Option A
  4. Quick Check:

    Deploy command = service deployment [OK]
Hint: Deploy runs container as service, not VM or just upload [OK]
Common Mistakes:
  • Thinking deploy creates VM
  • Confusing upload with deploy
  • Assuming region must be us-central1
4. You run:
gcloud run deploy --image=gcr.io/myproj/app:v2 --region=us-west1
But get an error: "Service name is required." What is the fix?
medium
A. Remove the --region flag
B. Change --image to --container-image
C. Add the service name after 'deploy', e.g., 'gcloud run deploy myservice ...'
D. Run the command without 'gcloud run'

Solution

  1. Step 1: Identify missing required argument

    The error says service name is missing; the deploy command needs a service name after 'deploy'.
  2. Step 2: Fix the command by adding service name

    Add the service name, e.g., 'myservice', after 'deploy' to fix the error.
  3. Final Answer:

    Add the service name after 'deploy', e.g., 'gcloud run deploy myservice ...' -> Option C
  4. Quick Check:

    Deploy needs service name [OK]
Hint: Always specify service name after 'deploy' [OK]
Common Mistakes:
  • Omitting service name
  • Changing unrelated flags
  • Removing required command parts
5. You want to deploy a new version of your app container image to Cloud Run and keep the same service URL. Which command should you use?
hard
A. gcloud container deploy myapp --image=gcr.io/myproj/app:v2
B. gcloud run create myapp --image=gcr.io/myproj/app:v2 --region=us-central1
C. gcloud run deploy --image=gcr.io/myproj/app:v2 --region=us-central1
D. gcloud run deploy myapp --image=gcr.io/myproj/app:v2 --region=us-central1

Solution

  1. Step 1: Understand updating a Cloud Run service

    To update an existing service and keep its URL, use 'gcloud run deploy' with the same service name.
  2. Step 2: Check options for correct update command

    gcloud run deploy myapp --image=gcr.io/myproj/app:v2 --region=us-central1 uses 'deploy' with service name and new image, which updates the service. Options A, B, and D are incorrect commands or missing service name.
  3. Final Answer:

    gcloud run deploy myapp --image=gcr.io/myproj/app:v2 --region=us-central1 -> Option D
  4. Quick Check:

    Deploy with same service name updates app [OK]
Hint: Use 'deploy' with same service name to update [OK]
Common Mistakes:
  • Using 'create' instead of 'deploy' to update
  • Omitting service name
  • Using wrong gcloud commands
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard