Running the Flask App Locally
-
Install Python
- Ensure you have Python 3.9+ installed. You can check using:
python --version
- Ensure you have Python 3.9+ installed. You can check using:
-
Create a Virtual Environment
- Navigate to the project directory and run:
python -m venv venv source venv/bin/activate # On Windows use: venv\Scripts\activate
- Navigate to the project directory and run:
-
Install Dependencies
- Install required packages using:
pip install -r requirements.txt
- Install required packages using:
-
Set Up Environment Variables
- Ensure you have a
.envfile in the project root with:MY_ENV_VAR=HelloWorld
- Ensure you have a
-
Run the Flask App
- Start the application:
python app.py
- The app should now be running at
http://127.0.0.1:5000/.
- Start the application:
-
Test API Endpoints Using Postman
- GET request:
- URL:
http://127.0.0.1:5000/get - Response should contain
messageandenv_value.
- URL:
- POST request:
- URL:
http://127.0.0.1:5000/post - Body (JSON):
{ "key": "value" } - Response should contain
received_data.
- URL:
- GET request:
Running the App Using Docker for CI/CD
-
Build the Docker Image
docker build -t flask-app . -
Run the Container
docker run -p 5000:5000 --env-file .env flask-app
-
Test API Endpoints
- Use Postman with the same URLs as above (
http://127.0.0.1:5000/getandhttp://127.0.0.1:5000/post).
- Use Postman with the same URLs as above (
-
Pushing to a Container Registry (Optional)
- Tag and push the image to a registry (e.g., Docker Hub, AWS ECR):
docker tag flask-app username/flask-app:latest docker push username/flask-app:latest
- Replace
usernamewith your actual Docker Hub or registry username.
- Tag and push the image to a registry (e.g., Docker Hub, AWS ECR):
-
Deployment in CI/CD
- Modify your CI/CD pipeline to pull the image and deploy it to a server or container orchestration platform like Kubernetes or AWS ECS.
This guide provides the necessary steps to run and test the application locally and explains how to prepare it for deployment using Docker.