Create a Flask Docker Image and have AWS host the Docker Container for us in the cloud.
- cd to project directory
.\venv\Scripts\activate
to start virtual envpip install --upgrade pip
pip install -r .\requirements.txt
to install dependenciespython .\app.py
to start the webserver on port 5000- Go to
http://localhost:5000/
to confirm you are seeing 'Hello World!' Ctrl + C
to stop web serverdeactivate
to stop the venv
- cd to project directory
- Run cmd
docker image build -t flask-demo-vs .
docker run -p 5000:5000 -d flask-demo-vs
to run the image (which is now called a Container). Note we are binding port 5000 (host) to port 5000 on the container.[host_port]:[container_port]
- Go to http://localhost:5000/ on local machine to make sure container is running.
- Note the long string of chars, like
d1eaa386d55db5ca1298e5931906bb967c1dbff87d800dc8bb841f424ea50f1f
- this is the container id. - Run command:
docker stop d1eaa386d55db5ca1298e5931906bb967c1dbff87d800dc8bb841f424ea50f1f
to stop the container. - Run command:
docker system prune
to clean up resources after stopping container. - Run command:
docker image ls
to see all images on the machine.
- https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
- Go into AWS Console > IAM dashboard
- Users > Add users > name it
- Select programmatic access to get an access key id + secret
- Click Attach existing policies directly
- Choose ECS_FullAccess and AmazonEC2ContainerRegistryFullAccess
- Tag: name == ecr-and-ecs-full-access
- Create user and copy the access key ID + secret access key (like AKIA4HNKMWVR2UBBDVPF and MIb7JUYmKM5ZVfYSgZsSoWPJgB9bp5vkNzGtlo7R)
- Open terminal/powershell after installing the AWS CLI
- Run cmd:
aws configure
, then enter the access key + secret access key when prompted - us-east-1 is a common default region for US
- AWS Console > ECR (Elastic Container Registry)
- Create a repo
- Name the repo, like
flask-demo-vs
(it's nice to be consistent with your Docker Image name) - Click on the repo name after it's created
- Click View Push Commands
- Run the commands from the project dir
- Confirm you can see the docker image in ECR repo after upload.
- Copy the Image URI from ECR, it will look like
840560325987.dkr.ecr.us-east-1.amazonaws.com/flask-demo-vs:latest
- AWS Console > ECS > Create Cluster
- EC2 Linux + Networking
- Name cluster: flask-cluster
- t2.micro is for free-tier
- Choose the VPC that already exists for your AWS account
- Choose the Subnet
- Auto Assign public IP == enabled
- Create a new security group
- Click Create
- It will take some time for the resources to get made. Wait for it to finish.
- Click View Cluster after it's done
- Click Tasks tab
- Create new Task Definition, choose EC2
- Name: EcsFlaskDemo
- Task size: 512 MiB and 1 VCPU
- Add Container > Container Name: FlaskContainerTest
- Image: paste in the URI from ECR (
840560325987.dkr.ecr.us-east-1.amazonaws.com/flask-demo-vs:latest
) - Add Port mappings: Can do 5000:5000
- Click Add
- Click Create
- Go back to view tasks > run the task you just made (Choose EC2)
- Go to EC2 dashboard
- Click on the running EC2 instance
- Copy the public IPv4 address, like 35.171.26.253
- Go to Security > Click on Security Group
- Edit Inbound Rules
- Allow all traffic IPv4 and IPv6 on port 5000
- Save rule
- Now try to reach your web server by hitting port 5000, like this: 35.171.26.253:5000
If you get the error: "Hardware assisted virtualization and data execution protection must be enabled in the BIOS" - try running this cmd and restarting Windows: dism.exe /Online /Enable-Feature:Microsoft-Hyper-V /All
. If that doesn't work, go into BIOS and make sure Virtualization or SVM (Secure Virtual Machine) option is enabled.