Hands-On: Build & Push an Image to ECR
Containerize a tiny Python web app and upload it to your private registry.
1 min read
Here's a minimal Python app (app.py) and its Dockerfile:
Python
from http.server import BaseHTTPRequestHandler, HTTPServer
class Hello(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b"Hello from a container on Fargate!")
HTTPServer(("0.0.0.0", 8080), Hello).serve_forever()Dockerfile
FROM python:3.13-slim
COPY app.py .
EXPOSE 8080
CMD ["python", "app.py"]Now create a repository, log Docker in to ECR, then build and push:
Try it
Terminal
REGION=us-east-1
ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
REPO=$ACCOUNT.dkr.ecr.$REGION.amazonaws.com/hello-app
aws ecr create-repository --repository-name hello-app
# Log Docker in to your private registry (the token is valid for 12 hours)
aws ecr get-login-password --region $REGION \
| docker login --username AWS --password-stdin $ACCOUNT.dkr.ecr.$REGION.amazonaws.com
docker build --platform linux/amd64 -t $REPO:v1 .
docker push $REPO:v1Tip
Tag images with a version or Git commit (v1, 3f2a9c1) instead of only latest. Then you always know
exactly what's running, and rolling back is just deploying the previous tag.
Note
On an Apple Silicon Mac, --platform linux/amd64 builds an image for standard x86 Fargate tasks.
Alternatively, build for ARM and set the task's CPU architecture to ARM64.
Key takeaways
- Write a Dockerfile, build the image, then push it to an ECR repository.
- `aws ecr get-login-password | docker login` authenticates Docker to ECR.
- Use meaningful image tags so deployments and rollbacks are predictable.
Check your understanding
Pick an answer to see the explanation.
1. Why avoid deploying only the 'latest' tag?