Dockerize Flask Application-AWS ECS+Flask
Have you ever built an APIs with Python?
Yess…..The first framework that strikes our mind is a Flask. Ok, I think almost the maximum number of developers know about flask but as for some percent who don’t know about the Flask……!
Flask is an open-source, beginner-friendly web framework built on the Python programming language. Flask is suitable when you want to develop an application with a light codebase rapidly.
But there is a small problem with the flask Applications was dependencies. So to overcome that we came up with a solution Dockers. That is how docker came into the picture.
Docker is an open-source tool that enables you to containerize your applications. It aids in building, testing, deploying, and managing your applications within an isolated environment. Dockerizing is the process of packing, deploying, and running applications using Docker containers.
We are not going to use Docker containers in our case. Wait what there will be a question raised in your mind without Dockers how…..? Don’t worry whenever we have some tech problem we have our man world’s most comprehensive and broadly adopted cloud platform AWS (AMAZON WEB SERVICES) Came up with an agile solution Elastic Container services.
How it works
Amazon ECS is a fully managed container orchestration service that makes it easy for you to deploy, manage, and scale containerized applications. It deeply integrates with the rest of the AWS platform to provide a secure and easy-to-use solution for running container workloads in the cloud and now on your infrastructure with Amazon ECS Anywhere

let us start our article there will be interesting keywords and services coming in the middle of our journey in the article. I will explain how ECS works, and what are problems faced by me. Let’s crack this.
My name is Sivasai Gangala, I am a full-stack developer from DhruthzuciTechsolutions, who loves to develop and build solutions using AWS services.
Step 1 — Prerequisites
- An AWS Account.
- The AWS CLI is Installed and Configured (with enough permissions to take all the actions we need to take).
- Docker (Docker commands may require sudo, or you can add your current user to the docker group).

AWS Container Services
Here we are creating a flask API, containerizing it in ECS, and serving through Load Balancer
ECS — Elastic Container Service is an AWS-managed service that helps us deploy and run our container applications in the cloud environment. Check out here for more details.
ECR — Elastic Container Registry is a fully-managed Docker container registry that makes it easier for developers to store, manage, and deploy Docker container images. Check out here for more details.
Build Simple python application
- Make a directory for the application and create an app.py file.
$vim app.py
2. Copy the following code in app.py.
from flask import Flask
from flask_restful import Resource, Apiapp = Flask(__name__)
api = Api(app)class MyClass(Resource):
def get(self):
return {'Hello': 'This is an flask rest API deployed in ECS'}api.add_resource(MyClass, '/hello')if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
3. Create requirments.txt file
$vim requirements.txt
4. Copy the following code in the requirments.txt file.
flask
flask_restful
5. Create Dockerfile
$ vim Dockerfile
6. Copy the following code.
FROM python:3.7
COPY. /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD python app.py
7. Build docker image
$ docker build -t my_docker_app: latest.
8. Run docker image
$ docker run -d -p 5000:5000 my_docker_app:latest
9. Test your app locally
$curl http://localhost:5000/hello
yayy.we build our API successfully in localhost……………………….!
Now come to the next step. we have our API running locally, now we have to deploy it on Server. For that, we are choosing AWS Elastic container services. let us move on to our next steps…..
Now we will upload our sample docker image to ECR
- Create ECR Repository
- Go to Amazon ECR and create a repository in AWS ECR and follow push commands to upload docker image to ECR as shown in the below gif.

- We get the following push commands for our image as shown below.

Note: There may be a chance of getting the following error listed down While running the first command “get login credentials” if you get the following error, then you need to check if you are using AWS CLI v1 or v2. This error usually occurs if you are using AWS CLI v1. To solve this, you need to first uninstall v1, log out and log in again and then install AWS CLI v2, and then you should be good to go.
Error: Cannot perform an interactive login from a non TTY device
After pushing our image to ECR, we have to create a cluster to perform our tasks.
Create ECS Cluster
- Go to Amazon ECS → Clusters → click on “Create Cluster”.

- Then select “EC2 Linux + Networking” as shown below.
In this article, we will use EC2 instance to deploy our containers. In next article, we will see how to use AWS Fargate to make our application serverless.

- Configure cluster as shown below.

- Configure networking as shown below.

- Then click on “Create”.
Create Task Definition
- Go to Amazon ECS → Task Definitions → click Create new task definition and fill in the below details as shown below.

- Add containers, fill in details as shown below and click on “Create”.

Create Service
- Go Amazon ECS → Clusters → select your cluster and click on “Create Service” and follow the steps as shown below and click on “Next Step”.

- In the “load balancing” section, select Application Load Balancer and select your load balancer. You need to create an application load balancer in advance.
- In the “Container to load balance” section, create a new listener port 80 and create a new target group. Set health check path to our API path i.e. /hello. Then click on the next step and skip auto-scaling for now and finally click on “Create Service.”

Test your application.
Go to EC2 management console, click on Load Balancers, select the load balancer, we used for creating ECS service and grab its DNS name.
Go to your browser and access your API using the following URL.
“HTTP://<LOAD_BALANCER_DNS>/hello”.
successfully we deployed our flask API in AWS ECS……………………….!
Conclusion
In this article, we learned how to
- Create a simple REST API using flask,
- Containerize it using docker,
- Upload Docker image to ECR repository
- Deploy the application in AWS Elastic Container Service.
- Serving API through the load balancer.
Next time I will come up with a new concept on how to connect this API to the RDS MySQL instance bringing new one more new AWS services called RDS into the picture.
If you find this article useful or if you see any improvements are needed, please do let me know below in the comment section. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend.
Credits:Google