How to Deploy a Dockerized Flask Application on AWS
In today’s tech landscape, deploying applications using containerization technologies like Docker has become increasingly popular. One of the most common frameworks for building web applications in Python is Flask. When combined with cloud services like Amazon Web Services (AWS), you can create scalable, efficient, and robust applications. In this article, we’ll take you through the step-by-step process of deploying a Dockerized Flask application on AWS.
What is Docker and Why Use It?
Docker is a platform that allows developers to automate the deployment of applications inside lightweight containers. These containers package your application and all its dependencies, ensuring that it runs uniformly across different computing environments.
Benefits of Using Docker:
- Consistency: "It works on my machine" becomes a thing of the past.
- Isolation: Each application runs in its own environment, preventing conflicts.
- Scalability: Easily scale applications up or down based on demand.
- Simplified CI/CD: Streamlines Continuous Integration and Continuous Deployment processes.
What is Flask?
Flask is a micro web framework for Python designed for building web applications quickly and with minimal code. It’s lightweight and easy to extend, making it a favorite among developers for creating small to medium-sized applications.
Use Cases for Flask:
- RESTful APIs
- Prototyping applications
- Simple web applications
- Microservices architecture
Prerequisites
Before we start deploying our Dockerized Flask application on AWS, ensure you have the following: - An AWS account - Docker installed on your local machine - Basic understanding of Flask and Python - AWS Command Line Interface (CLI) installed and configured
Step 1: Create a Simple Flask Application
Let's start by creating a simple Flask application. Create a new directory for your project and navigate into it:
mkdir flask-docker-app
cd flask-docker-app
Now, create a new file called app.py
with the following content:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, Dockerized Flask on AWS!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Next, create a requirements.txt
file to specify the dependencies:
Flask==2.0.3
Step 2: Dockerize the Flask Application
Now, let’s create a Dockerfile to containerize our Flask application. Create a file named Dockerfile
in the same directory:
# Use an official Python runtime as a parent image
FROM python:3.8-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . .
# Install the required packages
RUN pip install --no-cache-dir -r requirements.txt
# Make port 5000 available to the world outside this container
EXPOSE 5000
# Define environment variable
ENV NAME World
# Run the application
CMD ["python", "app.py"]
Build the Docker Image
Run the following command to build your Docker image:
docker build -t flask-docker-app .
Run the Docker Container
You can run the container locally to verify everything is working:
docker run -p 5000:5000 flask-docker-app
Visit http://localhost:5000
in your browser; you should see "Hello, Dockerized Flask on AWS!".
Step 3: Push Docker Image to Amazon ECR
To deploy our application on AWS, we need to push our Docker image to Amazon Elastic Container Registry (ECR).
Create an ECR Repository
- Go to the AWS Management Console.
- Navigate to the ECR service.
- Click on "Create repository".
- Enter a name for your repository (e.g.,
flask-docker-app
). - Click "Create repository".
Authenticate Docker to Your ECR Registry
Run the following command to authenticate Docker with your AWS ECR:
aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <your-aws-account-id>.dkr.ecr.<your-region>.amazonaws.com
Tag and Push Your Docker Image
Tag your Docker image and push it to ECR:
docker tag flask-docker-app:latest <your-aws-account-id>.dkr.ecr.<your-region>.amazonaws.com/flask-docker-app:latest
docker push <your-aws-account-id>.dkr.ecr.<your-region>.amazonaws.com/flask-docker-app:latest
Step 4: Deploy on AWS Elastic Beanstalk
AWS Elastic Beanstalk makes it easy to deploy and manage applications in the cloud.
Create a New Elastic Beanstalk Environment
- Go to AWS Management Console and navigate to Elastic Beanstalk.
- Click on "Create Application".
- Enter an application name and choose "Docker" as the platform.
- Click "Next" and select your ECR image.
- Configure the environment settings (instance type, scaling, etc.).
- Click "Create Environment".
Access Your Application
Once the environment is created, it may take a few minutes to launch. After that, you will receive a public URL to access your Flask application.
Troubleshooting Common Issues
- Container Fails to Start: Check the logs in Elastic Beanstalk for error messages that may indicate what went wrong.
- Application Not Accessible: Ensure that the security group associated with your Elastic Beanstalk environment allows inbound traffic on port 5000.
Conclusion
Deploying a Dockerized Flask application on AWS is a powerful way to ensure your application is scalable and robust. By following the steps outlined in this article, you can leverage the power of Docker and AWS to streamline your development and deployment processes. Whether you're building a simple web app or a complex microservices architecture, using Docker with AWS can significantly enhance your workflow and application performance. Happy coding!