Day 2 of 5
⏱ ~60 minutes
Google Cloud in 5 Days — Day 2

Compute: Cloud Run and Compute Engine

Deploy a containerized app to Cloud Run (serverless) and a traditional VM to Compute Engine. Understand when each is the right choice.

Cloud Run: Serverless Containers

Cloud Run runs Docker containers without managing servers. It scales to zero (no traffic = no cost) and handles scaling automatically.

Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"]
Deploy to Cloud Run
# Build and push to Artifact Registry
gcloud run deploy my-service \
  --source . \
  --platform managed \
  --region us-central1 \
  --allow-unauthenticated

# Or build/push manually
gcloud builds submit --tag gcr.io/PROJECT_ID/my-app
gcloud run deploy my-app --image gcr.io/PROJECT_ID/my-app
Compute Engine VM
# Create a VM
gcloud compute instances create my-vm \
  --machine-type e2-micro \
  --zone us-central1-a \
  --image-family debian-12 \
  --image-project debian-cloud

# SSH into it
gcloud compute ssh my-vm --zone us-central1-a

# Stop when done to avoid charges
gcloud compute instances stop my-vm --zone us-central1-a
ℹ️
Cloud Run is the right choice for stateless HTTP services — APIs, web apps, microservices. Use Compute Engine when you need persistent disk, specific OS configuration, or long-running non-HTTP workloads.
📝 Day 2 Exercise
Deploy an App to Cloud Run
  1. B
  2. u
  3. i
  4. l
  5. d
  6. a
  7. s
  8. i
  9. m
  10. p
  11. l
  12. e
  13. P
  14. y
  15. t
  16. h
  17. o
  18. n
  19. F
  20. l
  21. a
  22. s
  23. k
  24. o
  25. r
  26. N
  27. o
  28. d
  29. e
  30. E
  31. x
  32. p
  33. r
  34. e
  35. s
  36. s
  37. a
  38. p
  39. p
  40. .
  41. C
  42. o
  43. n
  44. t
  45. a
  46. i
  47. n
  48. e
  49. r
  50. i
  51. z
  52. e
  53. i
  54. t
  55. w
  56. i
  57. t
  58. h
  59. D
  60. o
  61. c
  62. k
  63. e
  64. r
  65. .
  66. D
  67. e
  68. p
  69. l
  70. o
  71. y
  72. t
  73. o
  74. C
  75. l
  76. o
  77. u
  78. d
  79. R
  80. u
  81. n
  82. .
  83. V
  84. e
  85. r
  86. i
  87. f
  88. y
  89. t
  90. h
  91. e
  92. p
  93. u
  94. b
  95. l
  96. i
  97. c
  98. U
  99. R
  100. L
  101. w
  102. o
  103. r
  104. k
  105. s
  106. .
  107. C
  108. h
  109. e
  110. c
  111. k
  112. t
  113. h
  114. e
  115. l
  116. o
  117. g
  118. s
  119. i
  120. n
  121. t
  122. h
  123. e
  124. c
  125. o
  126. n
  127. s
  128. o
  129. l
  130. e
  131. .

Day 2 Summary

  • Cloud Run: serverless, scales to zero, charges only for requests. Perfect for APIs.
  • Compute Engine: full VMs, persistent disk, full OS control. More expensive, more flexible.
  • gcloud run deploy --source . auto-builds and deploys from source code.
  • Always stop VMs when not in use. Compute Engine charges by the second even when idle.
Finished this lesson?