Issue
I have a backend project where the final result is stored in fraud_detection.db. I am experimenting with flask and docker and don't seem to be able to get everything running.
This is my file tree
DE-Project/
│
├── .dockerignore
├── docker-compose.yaml
├── Dockerfile.train_model
├── Dockerfile.producer
├── Dockerfile.consumer
├── Dockerfile.frontend
│
├── shared/
│ └── requirements.txt
│ └── kaggle.json
│
├── train_model/
│ ├── train_model.py
│ └── functions.py
│
└── make_predictions/
│ └── producer.py
│ └── consumer.py
│ └── functions.py
│ └── fraud_detection.db
└── frontend/
├── app.py
├── test.py
└── templates/
└── index.html
This is the flask app.py
from flask import Flask, render_template
import sqlite3
import os
app = Flask(__name__)
# Set the path to the templates folder explicitly
template_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
app.template_folder = template_path
# Get the absolute path to the directory containing this script
script_dir = os.path.dirname(os.path.abspath(__file__))
# Construct the absolute path to the database file
db_file_path = os.path.join(script_dir, 'make_predictions/fraud_detection.db')
@app.route('/')
def index():
# Connect to the SQLite database using the absolute path
conn = sqlite3.connect(db_file_path)
# Create a cursor object to execute SQL queries
cur = conn.cursor()
# Execute a query to fetch all entries (assuming your table is named 'potential_fraud')
sqlite_select_Query = "SELECT * FROM potential_fraud LIMIT 10;"
# Execute the query
cur.execute(sqlite_select_Query)
# Fetch all the entries
record = cur.fetchall()
# Close the database connection
conn.close()
# Render the HTML template with the entries
return render_template('index.html', entries=record)
if __name__ == '__main__':
# Run the Flask app on port 5000
This is my Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3.8
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Expose port 5000 to the outside world
EXPOSE 5000
# Run app.py when the container launches
CMD ["python", "app.py"]
When I run it in docker I get this error when I access it via localhost:
File "app.py", line 20, in index
conn = sqlite3.connect(db_file_path)
sqlite3.OperationalError: unable to open database file
I understand that this error usually happens when something is wrong with the file path. I tested the connection with test.py which worked fine:
import sqlite3
conn = sqlite3.connect('make_predictions/fraud_detection.db')
cur = conn.cursor()
sqlite_select_Query = "SELECT * FROM potential_fraud LIMIT 1;"
cur.execute(sqlite_select_Query)
record = cur.fetchall()
print(record)
cur.close()
conn.close()
print("The SQLite connection is closed")
Is something in my app.py wrong? Maybe my Dockerfile or both?
Solution
I managed to solve my problem. The trick, or my solution, was to put the database into a shared drive. I specified it in the docker-compose, consumer.py and the frontend. This worked. I guess an even better solution would to have a real database setup in a completely standalone container. But this works for now :)
Answered By - Stefan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.