3-how-to-deploy-a-flask-application-using-docker-and-aws.html

How to Deploy a Flask Application Using Docker and AWS

Deploying web applications can seem daunting, especially if you’re new to the concepts of cloud computing and containerization. However, combining Flask—a lightweight and powerful web framework for Python—with Docker and AWS can streamline the deployment process significantly. This article will guide you through the steps to deploy a Flask application using Docker and AWS, providing clear code examples and actionable insights along the way.

What is Flask?

Flask is a popular micro web framework for Python, designed to make getting started quick and easy while allowing for flexibility and scalability as your application grows. It’s known for its simplicity and ease of use, making it a favorite among developers for building web applications and RESTful APIs.

Why Use Docker?

Docker is a platform that uses containerization to package applications and their dependencies into a standardized unit called a container. The benefits of using Docker include:

  • Consistency: Ensure your application runs the same way on different environments.
  • Isolation: Run multiple applications on the same host without conflicts.
  • Scalability: Easily scale applications by deploying additional containers.

Why Deploy on AWS?

Amazon Web Services (AWS) is a leading cloud service provider that offers a wide range of services for hosting applications. Benefits of deploying on AWS include:

  • Scalability: Easily handle increased traffic.
  • Reliability: A robust infrastructure with high availability.
  • Variety of Services: Access to a vast array of additional services like databases, storage, and machine learning.

Prerequisites

Before we begin, ensure you have the following:

  • A basic understanding of Python and Flask.
  • Docker installed on your machine.
  • An AWS account.
  • AWS CLI installed and configured.

Step 1: Create a Simple Flask Application

Let’s first create a simple Flask application. Create a new directory for your project and add the following files:

app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Flask on Docker and AWS!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

requirements.txt

Flask==2.0.1

Dockerfile

Create a Dockerfile in the same directory with the following content:

# Use the official Python image from the Docker Hub
FROM python:3.9-slim

# Set the working directory
WORKDIR /app

# Copy the requirements file
COPY requirements.txt .

# Install the required packages
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code
COPY app.py .

# Expose the application port
EXPOSE 5000

# Command to run the application
CMD ["python", "app.py"]

Step 2: Build the Docker Image

In your terminal, navigate to your project directory and build the Docker image:

docker build -t flask-docker-app .

This command will create a Docker image named flask-docker-app.

Step 3: Run the Docker Container Locally

To test your application locally, run the following command:

docker run -p 5000:5000 flask-docker-app

Visit http://localhost:5000 in your web browser, and you should see "Hello, Flask on Docker and AWS!"

Step 4: Push the Docker Image to AWS ECR

To deploy your application on AWS, you need to push your Docker image to Amazon Elastic Container Registry (ECR).

Create an ECR Repository

  1. Go to the AWS Management Console and navigate to ECR.
  2. Click on "Create Repository."
  3. Give your repository a name (e.g., flask-docker-app) and click "Create."

Authenticate Docker to ECR

Run the following command to authenticate your Docker client to your ECR registry:

aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <your-account-id>.dkr.ecr.<your-region>.amazonaws.com

Tag and Push the Docker Image

Tag your Docker image with the ECR repository URI:

docker tag flask-docker-app:latest <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/flask-docker-app:latest

Now push the image to ECR:

docker push <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/flask-docker-app:latest

Step 5: Deploy the Application Using AWS ECS

Now that your Docker image is in ECR, the next step is to deploy it using Amazon Elastic Container Service (ECS).

Create an ECS Cluster

  1. Go to the ECS console in AWS.
  2. Click on "Clusters" and then "Create Cluster."
  3. Choose "Networking only" and give your cluster a name.

Create a Task Definition

  1. In the ECS console, click on "Task Definitions" and then "Create new Task Definition."
  2. Select "Fargate" for the launch type.
  3. Define the task settings:
  4. Task Name: flask-docker-task
  5. Container Definitions: Click on "Add container."

    • Container Name: flask-docker-app
    • Image: <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/flask-docker-app:latest
    • Memory Limits: 512 MiB
    • Port Mappings: 5000
  6. Click "Create" to finish the task definition.

Run the Task

  1. Go to your ECS cluster and click on "Tasks."
  2. Click "Run new Task."
  3. Choose your task definition and set the number of tasks you want to run.
  4. Click "Run Task."

Step 6: Access Your Application

After a few moments, your task will start running. To access your application:

  1. Go to the "Tasks" tab in your ECS cluster.
  2. Click on your task to see the details.
  3. Note the public IP address assigned to your task.
  4. Visit http://<public-ip>:5000 in your browser, and you should see your Flask application running!

Conclusion

Deploying a Flask application using Docker and AWS can greatly simplify your workflow and provide a scalable solution for production environments. With the steps provided, you now have the foundational knowledge to create, containerize, and deploy your application. Experiment further by adding more features, exploring different AWS services, and optimizing your Docker configuration. Happy coding!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.