gneya commited on
Commit
c7a353e
·
verified ·
1 Parent(s): 73923c3

Upload 6 files

Browse files
Files changed (6) hide show
  1. app.py +31 -0
  2. docker-compose.yml +33 -0
  3. dockerfile +84 -0
  4. init.sql +3 -0
  5. pg_hba.conf +2 -0
  6. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask
2
+ import psycopg2
3
+
4
+ app = Flask(__name__)
5
+
6
+ # Database connection details
7
+ DB_USER = 'user'
8
+ DB_PASSWORD = 'password'
9
+ DB_NAME = 'mydatabase'
10
+ DB_HOST = 'db'
11
+ DB_PORT = '5432'
12
+
13
+ @app.route('/')
14
+ def index():
15
+ # Connect to PostgreSQL database
16
+ conn = psycopg2.connect(
17
+ database=DB_NAME,
18
+ user=DB_USER,
19
+ password=DB_PASSWORD,
20
+ host=DB_HOST,
21
+ port=DB_PORT
22
+ )
23
+
24
+ cursor = conn.cursor()
25
+ cursor.execute("SELECT version();")
26
+ db_version = cursor.fetchone()
27
+
28
+ return f"PostgreSQL database version: {db_version}"
29
+
30
+ if __name__ == '__main__':
31
+ app.run(host='0.0.0.0', port=8000)
docker-compose.yml ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: '3.8'
2
+
3
+ services:
4
+ # Flask application service
5
+ app:
6
+ image: my-flask-app # Use the image built for your Flask app
7
+ build:
8
+ context: . # Build from Dockerfile in current directory
9
+ ports:
10
+ - "8000:8000" # Map host port 7860 to container port 7860
11
+ depends_on:
12
+ - db # Ensure the db service is started before the app
13
+ environment:
14
+ - FLASK_ENV=development
15
+ - DATABASE_URL=postgresql://user:password@db:5432/mydatabase # Connection URL for PostgreSQL
16
+ networks:
17
+ - my_network # Connect to the custom network
18
+
19
+ # PostgreSQL database service
20
+ db:
21
+ image: postgres:13 # Use the official PostgreSQL image
22
+ environment:
23
+ POSTGRES_USER: user
24
+ POSTGRES_PASSWORD: password
25
+ POSTGRES_DB: mydatabase
26
+ ports:
27
+ - "5432:5432" # Map host port 5432 to container port 5432
28
+ networks:
29
+ - my_network # Connect to the custom network
30
+
31
+ networks:
32
+ my_network:
33
+ driver: bridge
dockerfile ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # FROM postgres:15
2
+ # # Update package list and install Python 3, Pip, and any required packages
3
+ # FROM ubuntu:20.04
4
+
5
+ # # Update package list and install Python 3, Pip, and required packages
6
+ # RUN apt-get update && apt-get install -y python3 python3-pip python3-venv
7
+
8
+ # # Install dependencies for PostgreSQL or Python packages
9
+ # RUN apt-get install -y libpq-dev gcc
10
+
11
+ # # Create and activate a virtual environment
12
+ # WORKDIR /app
13
+ # RUN python3 -m venv venv
14
+ # ENV PATH="/app/venv/bin:$PATH"
15
+
16
+ # # Copy application files
17
+ # COPY . /app
18
+ # ENV DEBIAN_FRONTEND=noninteractive
19
+
20
+
21
+ # # Install Python dependencies inside the virtual environment
22
+ # RUN pip install --upgrade pip
23
+ # RUN pip install -r requirements.txt
24
+ # # Install required packages for PostgreSQL
25
+ # RUN apt-get update && apt-get install -y \
26
+ # postgresql \
27
+ # postgresql-contrib \
28
+ # && rm -rf /var/lib/apt/lists/*
29
+
30
+ # # Set environment variables for PostgreSQL
31
+ # ENV POSTGRES_USER="admin"
32
+ # ENV POSTGRES_PASSWORD="admin"
33
+ # ENV POSTGRES_DB="admin"
34
+
35
+ # # Expose the PostgreSQL port
36
+ # EXPOSE 5432
37
+
38
+ # # Install any necessary dependencies
39
+ # # RUN pip install -r requirements.txt
40
+ # # Initialize PostgreSQL database and start the service
41
+ # # Initialize PostgreSQL database and start the service
42
+
43
+
44
+ # # Set environment variables for database name, username, and password
45
+
46
+
47
+ # # Optional: If you want to initialize the database with a script
48
+ # COPY init.sql /docker-entrypoint-initdb.d/
49
+
50
+ # # Expose PostgreSQL default port
51
+ # EXPOSE 5432
52
+ # EXPOSE 7860
53
+
54
+
55
+
56
+ # # Command to run the application along with PostgreSQL
57
+ # CMD service postgresql start
58
+ # ENTRYPOINT ["python3", "app.py"]
59
+
60
+
61
+ # Use the official Python image as a base image
62
+ FROM python:3.8-slim
63
+
64
+ # Set environment variables
65
+ ENV FLASK_APP=app.py
66
+ ENV FLASK_RUN_HOST=0.0.0.0
67
+
68
+ # Set the working directory in the container
69
+ WORKDIR /app
70
+
71
+ # Copy the requirements file into the container
72
+ COPY requirements.txt .
73
+
74
+ # Install Python dependencies
75
+ RUN pip install --no-cache-dir -r requirements.txt
76
+
77
+ # Copy the rest of the application code into the container
78
+ COPY . .
79
+
80
+ # Expose the port the app runs on
81
+ EXPOSE 7860
82
+
83
+ # Start the Flask application
84
+ CMD ["flask", "run"]
init.sql ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ CREATE DATABASE mydatabase;
2
+ CREATE USER myuser WITH ENCRYPTED PASSWORD 'admin';
3
+ GRANT ALL PRIVILEGES ON DATABASE "admin" TO "admin";
pg_hba.conf ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ # TYPE DATABASE USER ADDRESS METHOD
2
+ host all all 0.0.0.0/0 md5
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Flask # Or any other framework you are using
2
+ psycopg2-binary # PostgreSQL adapter for Python