Skip to main content

Create & Dockerize a Flask app with Redis

 Create & Dockerize a Flask app with Redis






For this project we would create everything from scratch.

Section 1 : We would create the Flask app
Section 2 : We would deploy the app as service, along with redis using docker and docker-compose.

Section 1

Creating a flask app that returns the count number of times the page was visited.

Create a requirements.txt file, and paste the following dependency name
Flask
redis


Import dependencies using
pip install --no-cache-dir -r requirements.txt


Create a app.py file 
from flask import Flask
from redis import Redis
app = Flask(__name__)
redis = Redis(host='db_service', port=6379)
@app.route('/')
def hello():
    visit_count = redis.incr('visit_count')
    return f"Hello, this page has been visited {visit_count} times."
if __name__ == '__main__':
    app.run(host='0.0.0.0')


We can test the app locally for this we need to:

Install virtualenv if it's not already installed:
sudo apt-get update
sudo apt-get install python3-venv

Create a new virtual environment:
python3 -m venv myenv

Activate the virtual environment:
source myenv/bin/activate

Install Flask and Redis within the virtual environment:
pip install Flask redis

Run the Flask application within the virtual environment:
python3 app.py


Section 2

For Section 2, Let's visit one previous project Build Flask Image using docker & docker-compose


You can also follow the github project DockerizeFlaskVisitCounter



---THANK YOU---

Comments

Popular posts from this blog

Kubernetes2

  Challenge 2 ConfigMaps: Step-1: To create a ConfigMap named 'fresco-config' with the key-value pair SERVER_URL= https://www.fresco.me , use the following command: kubectl create configmap fresco-config --from-literal=SERVER_URL=https://www.fresco.me To verify if the ConfigMap is created, use the following command: kubectl get configmap fresco-config Step-2: To create an nginx pod with the environmental variable SERVER_URL_ENV using the ConfigMap created earlier, use the following YAML template: apiVersion: v1 kind: Pod metadata: name: fresco-nginx-pod spec: containers: - name: fresco-nginx-container image: nginx env: - name: SERVER_URL_ENV valueFrom: configMapKeyRef: name: fresco-config key: SERVER_URL Deploy the above file you can name it deployment.yaml and use the command: kubectl apply -f deployment.yaml To test your configuration by executing the following command: ...

Mini-Projects for Docker, Docker Swarm

 Mini-Projects for Docker Challenge 1 Welcome to the Docker challenge, your task is to follow the below steps and complete them successfully. Step 1: Pull latest nginx image Step 2: Create a new bridge 'bridge_sample' Step 3: Run a couple of images (Cont1 and Cont2) and connect these to the new bridge created. Now try to ping from cont1 to cont2 to verify connectivity. Step 4: Stop containers Step 5: Remove network, containers, and images using docker commands Note: Execute "history -w" in the terminal before submitting the scenario. Before moving to Answers try to solve yourself... Let's get started... Step 1: Pull latest nginx image docker pull nginx:latest Step 2: Create a new bridge 'bridge_sample' docker network create bridge_sample Step 3: Run containers and connect them to the new bridge docker run -d --name Cont1 --network bridge_sample nginx:latest docker run -d --name Cont2 --netw...

Springboot e-commerce

 Springboot e-commerce git:  https://github.com/HazraSoham/WingsT4Springboot User.java - renamed to EcomUser.java Below is the code snippet how EcomUser.java should be, add constructor and getters and setters also. Have many-to-many with Role Have one-to-one with Cart Have one-to-many with Product @Entity public class EcomUser {           @Id @GeneratedValue (strategy = GenerationType. IDENTITY ) private Integer ecomUserId ; private String username ; private String password ; @ManyToMany (fetch = FetchType. EAGER , cascade = CascadeType. ALL ) @JoinTable (name = "UserRole" , joinColumns = @JoinColumn (name = "ecomUserId" ), inverseJoinColumns = @JoinColumn (name = "roleId" )) private Set<Role> roles ; Role.java - shown below the class, add getter and setters also Have many-to-many with User @Entity public class Role { @Id @GeneratedValue (strategy = GenerationType. IDENTITY ) private Integer roleId ; private S...