Containers in the cloud
Containers (from the Docker course) package an app with everything it needs, so it runs the same on your laptop and in production. Every cloud offers several ways to run them, from "just give me a URL" to full Kubernetes clusters. This lesson helps you choose, and walks through the usual path: build an image → push it to a registry → run it on a managed service.
The options, from simplest to most control
| Service | AWS | Google Cloud | Azure | You manage |
|---|---|---|---|---|
| Serverless containers (scale to zero, URL included) | App Runner, ECS on Fargate | Cloud Run | Container Apps | Only the image |
| Container orchestration, no servers | ECS on Fargate | GKE Autopilot | Container Apps | Tasks, services, networking |
| Managed Kubernetes | EKS | GKE | AKS | Kubernetes workloads (and often node pools) |
| Containers on your own VMs | ECS on EC2 | GKE Standard | AKS node pools | Also the VMs |
A good rule: start at the top, and move down only when you need what the lower levels offer. Most web apps and APIs run happily on Cloud Run, Container Apps or ECS Fargate. Kubernetes pays off for large platforms with many services and teams (and has its own course on this site).
Step 1: a container registry
Images are stored in a private registry next to where they run: ECR (AWS), Artifact Registry (Google Cloud), ACR (Azure).
repo=$(aws ecr create-repository --repository-name shop-api --image-scanning-configuration scanOnPush=true \
--image-tag-mutability IMMUTABLE --query "repository.repositoryUri" --output text)
echo "$repo" | sed -E 's/^[0-9]+\.dkr\.ecr\.[a-z0-9-]+\.amazonaws\.com/123456789012.dkr.ecr.ap-south-1.amazonaws.com/'
aws ecr describe-repositories --query "repositories[].[repositoryName, imageTagMutability, imageScanningConfiguration.scanOnPush]" --output text123456789012.dkr.ecr.ap-south-1.amazonaws.com/shop-api shop-api IMMUTABLE True
- Scan on push checks images for known vulnerabilities.
- Immutable tags stop someone overwriting
v1.4.2with different code. Tag images with the Git commit or version, not justlatest.
Pushing from your machine or CI:
aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-south-1.amazonaws.com
docker build -t shop-api:1.4.2 .
docker tag shop-api:1.4.2 123456789012.dkr.ecr.ap-south-1.amazonaws.com/shop-api:1.4.2
docker push 123456789012.dkr.ecr.ap-south-1.amazonaws.com/shop-api:1.4.2💡 Build images for the CPU you'll run on:
docker buildx build --platform linux/arm64for Graviton/Axion/Cobalt (cheaper),linux/amd64for Intel/AMD.
Step 2a: the easiest path, Cloud Run
On Google Cloud, one command builds, pushes and deploys a container, and returns an HTTPS URL. It scales to zero when idle:
gcloud run deploy shop-api --source . --region asia-south1 --allow-unauthenticated \
--memory 512Mi --min-instances 0 --max-instances 20 --set-env-vars "APP_ENV=prod"Example output:
Building using Dockerfile and deploying container to Cloud Run service [shop-api] in project [learn-cloud-2026] region [asia-south1]
✓ Building and deploying... Done.
✓ Uploading sources...
✓ Building Container...
✓ Creating Revision...
✓ Routing traffic...
Done.
Service [shop-api] revision [shop-api-00001-xyz] has been deployed and is serving 100 percent of traffic.
Service URL: https://shop-api-abc123xyz-el.a.run.appAzure Container Apps is similar: az containerapp up --name shop-api --source . --ingress external --target-port 8080.
The contract is simple: your container listens on the port in the PORT environment variable, is stateless (files and sessions go to storage and databases), and starts quickly.
Step 2b: ECS on Fargate (AWS)
ECS describes your app with three objects:
| Object | Is |
|---|---|
| Cluster | A logical group for your services |
| Task definition | A recipe: image, CPU, memory, port, environment, IAM role, logging |
| Service | Keeps N copies (tasks) of a task definition running, replaces failed ones, and registers them with a load balancer |
{
"family": "shop-api",
"requiresCompatibilities": ["FARGATE"],
"networkMode": "awsvpc",
"cpu": "512",
"memory": "1024",
"runtimePlatform": { "cpuArchitecture": "ARM64", "operatingSystemFamily": "LINUX" },
"executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"taskRoleArn": "arn:aws:iam::123456789012:role/shop-api-task",
"containerDefinitions": [
{
"name": "api",
"image": "123456789012.dkr.ecr.ap-south-1.amazonaws.com/shop-api:1.4.2",
"portMappings": [{ "containerPort": 8080 }],
"environment": [{ "name": "APP_ENV", "value": "prod" }],
"secrets": [{ "name": "DATABASE_URL", "valueFrom": "arn:aws:secretsmanager:ap-south-1:123456789012:secret:shop/db-url" }],
"healthCheck": { "command": ["CMD-SHELL", "wget -qO- localhost:8080/health || exit 1"], "interval": 15 },
"logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "/ecs/shop-api", "awslogs-region": "ap-south-1", "awslogs-stream-prefix": "api" } }
}
]
}# the private subnets and app security group from the Networking lesson:
vpc=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --query Vpc.VpcId --output text)
sub_a=$(aws ec2 create-subnet --vpc-id "$vpc" --cidr-block 10.0.11.0/24 --availability-zone ap-south-1a --query Subnet.SubnetId --output text)
sub_b=$(aws ec2 create-subnet --vpc-id "$vpc" --cidr-block 10.0.12.0/24 --availability-zone ap-south-1b --query Subnet.SubnetId --output text)
sg=$(aws ec2 create-security-group --vpc-id "$vpc" --group-name app-sg --description "App" --query GroupId --output text)
aws ecs create-cluster --cluster-name shop --query "cluster.[clusterName, status]" --output text
aws ecs register-task-definition --cli-input-json file://taskdef.json \
--query "taskDefinition.[family, revision, cpu, memory, runtimePlatform.cpuArchitecture]" --output text
aws ecs create-service --cluster shop --service-name shop-api --task-definition shop-api:1 --desired-count 2 \
--launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets=[$sub_a,$sub_b],securityGroups=[$sg],assignPublicIp=DISABLED}" \
--query "service.[serviceName, desiredCount, launchType]" --output text
aws ecs list-services --cluster shop --query "serviceArns" --output text | sed -E 's/[0-9]{12}/123456789012/'shop ACTIVE shop-api 1 512 1024 ARM64 shop-api 2 FARGATE arn:aws:ecs:ap-south-1:123456789012:service/shop/shop-api
Notice the two roles: the execution role lets ECS pull the image and read secrets; the task role is what your code can do (read S3, write to DynamoDB…). And secrets injects the database URL from Secrets Manager at start-up, so it's never in the image or the task definition.
Deploying a new version means registering a new task definition revision with the new image tag, then aws ecs update-service --task-definition shop-api:2. ECS starts new tasks, waits for them to pass health checks, then stops the old ones (a rolling deployment), and can roll back automatically if the new tasks fail.
Managed Kubernetes, briefly
When you need Kubernetes, the clouds run the control plane for you:
gcloud container clusters create-auto shop --region asia-south1 # GKE Autopilot: no nodes to manage
az aks create --resource-group demo-rg --name shop --node-count 2 --generate-ssh-keys
eksctl create cluster --name shop --region ap-south-1 --nodes 2 # EKS with the eksctl toolThen you deploy with standard Kubernetes manifests and kubectl, which work the same on all three. The Kubernetes course covers that in depth.
Production checklist
- Images: small base images, non-root user, scanned, tagged by version (see the Docker course).
- Configuration in environment variables; secrets from the cloud's secret manager.
- Health checks so the platform restarts or replaces broken containers.
- Logs to stdout/stderr: the platform collects them (CloudWatch, Cloud Logging, Azure Monitor).
- Stateless containers: anything that must survive goes to a database or object storage.
- Autoscaling on CPU, memory or requests, with sensible minimum and maximum counts.
- Least-privilege task role / service account for the container.
Clean up
aws ecs update-service --cluster shop --service shop-api --desired-count 0 > /dev/null
aws ecs delete-service --cluster shop --service shop-api --query "service.status" --output text
aws ecs delete-cluster --cluster shop --query "cluster.status" --output text
aws ecr delete-repository --repository-name shop-api --force --query "repository.repositoryName" --output textINACTIVE INACTIVE shop-api
Practice
- Containerise a small web app (or use one from the Docker course) and run it locally with the
PORTvariable. - Push it to a registry on the cloud you use.
- Deploy it to Cloud Run, Container Apps or App Runner and open the URL.
- Explain the difference between an ECS task's execution role and its task role.
1.Which is usually the simplest way to run a containerised web app?
2.In ECS, what keeps a fixed number of copies of your container running?
3.Why use immutable image tags?
4.Where should a container get its database password?