Deploying a Containerised Flask App to AWS EC2
Overview
This guide covers pushing a Docker image to AWS ECR and running it on an EC2 instance.
Prerequisites
- Docker installed and running locally
- AWS CLI installed (
aws --version) - An AWS account with ECR and EC2 access
Step 1: Configure the AWS CLI
aws configure
Enter your:
- AWS Access Key ID
- AWS Secret Access Key
- Default region:
eu-west-2 - Default output format:
json
Verify you are logged in:
aws sts get-caller-identity
Step 2: Create an ECR Repository
aws ecr create-repository --repository-name contain-my-flask --region eu-west-2
Note the repositoryUri in the response — you will need it for tagging and pushing.
Step 3: Build the Docker Image for Linux/amd64
If you are on Apple Silicon (M1/M2), you must build for the correct platform:
docker buildx build --platform linux/amd64 -t contain-my-flask:1.0.0 .
Step 4: Authenticate Docker to ECR
aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin 664047078509.dkr.ecr.eu-west-2.amazonaws.com
Step 5: Tag and Push the Image to ECR
Tag the image with the ECR URI:
docker tag contain-my-flask:1.0.0 664047078509.dkr.ecr.eu-west-2.amazonaws.com/contain-my-flask:1.0.0
Push the image:
docker push 664047078509.dkr.ecr.eu-west-2.amazonaws.com/contain-my-flask:1.0.0
Step 6: Launch an EC2 Instance
In the AWS Console:
- Go to EC2 → Launch instance
- Name:
Flask Server - AMI: Amazon Linux 2023 (free tier)
- Instance type: t2.micro (free tier)
- Create or select a key pair — download the
.pemfile - Security group inbound rules:
- SSH (port 22) — My IP
- HTTP (port 80) — Anywhere (0.0.0.0/0)
- Click Launch instance
Step 7: Attach an IAM Role to the EC2 Instance
So the instance can pull from ECR without manual credentials:
- Go to IAM → Roles → Create role
- Select AWS service → EC2 → Next
- Attach policy:
AmazonEC2ContainerRegistryReadOnly - Name the role
ec2-ecr-role→ Create role - Go to EC2 → select your instance → Actions → Security → Modify IAM role
- Select
ec2-ecr-role→ Update IAM role
Step 8: SSH into the EC2 Instance
Fix the key file permissions first:
chmod 400 ~/Desktop/your-key.pem
Connect to the instance:
ssh -i ~/Desktop/your-key.pem ec2-user@<public-ip>
Step 9: Install and Start Docker on the Instance
sudo yum install -y docker
sudo service docker start
Step 10: Authenticate Docker to ECR (on the Instance)
aws ecr get-login-password --region eu-west-2 | sudo docker login --username AWS --password-stdin 664047078509.dkr.ecr.eu-west-2.amazonaws.com
Step 11: Pull the Image from ECR
sudo docker pull 664047078509.dkr.ecr.eu-west-2.amazonaws.com/contain-my-flask:1.0.0
Step 12: Run the Container
sudo docker run -d -p 80:3030 664047078509.dkr.ecr.eu-west-2.amazonaws.com/contain-my-flask:1.0.0
-druns the container in the background-p 80:3030maps port 80 on the EC2 instance to port 3030 inside the container
Verify the container is running:
sudo docker ps
Step 13: Access the App
Open your browser and visit:
http://<your-ec2-public-ip>
Use http:// not https://.
Troubleshooting
SSH times out — your IP may have changed. Update the SSH inbound rule in the security group to My IP.
No matching manifest for linux/amd64 — you built the image on Apple Silicon. Rebuild with --platform linux/amd64 and push again.
Unable to locate credentials on EC2 — the IAM role is not attached. Follow Step 7.