Docker and Cloud Run for AI apps
Shipping AI Apps · 10 questions
Short notes
Key idea
Docker packages your app with its exact Python version and dependencies into an image that runs the same everywhere. Cloud Run runs that container on Google Cloud, scaling with traffic and down to zero when idle.
Syntax
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["sh", "-c", "uvicorn main:app --host 0.0.0.0 --port ${PORT:-8080}"]
docker build -t shop-api . && docker run -p 8080:8080 shop-api
gcloud run deploy shop-api --source . --region asia-south1 \
--set-secrets GEMINI_API_KEY=gemini-key:latest --max-instances 3
Example
gcloud run deploy --source . builds the image, pushes it and gives you an HTTPS URL. With --max-instances 3, a traffic spike can't scale the bill without limit.
Traps
- Listen on
$PORTand0.0.0.0, or Cloud Run's health check fails. - Copy
requirements.txtbefore the code so Docker caches the install layer. - Secrets baked into the image leak to anyone who can pull it; use Secret Manager.
- Cold starts: the first request after idle is slow; set min instances if that matters (it costs money).
- Add a
.dockerignorefor.venv,.envand data files.
Want the full lesson with step-by-step examples, a project and a certificate? Shipping AI Apps course, ₹180 →
- Question 1easy
What problem do containers mainly solve?
- Question 2easy
What's the difference between an image and a container?
- Question 3medium
Which order makes code edits rebuild fastest?
- Question 4medium
What does this print?
lines = ["FROM python:3.12-slim", "COPY requirements.txt .", "ENV GEMINI_API_KEY=AIza-real", "CMD [\"uvicorn\"]"] print(any(l.startswith("ENV") and "KEY" in l for l in lines)) - Question 5medium
How should the Gemini key reach a container on Cloud Run?
Docker and Cloud Run for AI apps
Docker and Cloud Run for AI apps
Docker and Cloud Run for AI apps
Docker and Cloud Run for AI apps
Docker and Cloud Run for AI apps
5 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.