Upload 2 files
Browse files- Dockerfile +25 -8
- docker_entrypoint.py +16 -39
Dockerfile
CHANGED
@@ -1,11 +1,28 @@
|
|
1 |
-
FROM python:3.
|
|
|
|
|
|
|
|
|
2 |
WORKDIR /app
|
3 |
-
|
4 |
-
|
5 |
-
COPY . .
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
ENV PYTHONUNBUFFERED=1
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
11 |
ENTRYPOINT ["python", "docker_entrypoint.py"]
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
# Create non-root user
|
4 |
+
RUN useradd -m -u 1000 user
|
5 |
+
|
6 |
WORKDIR /app
|
7 |
+
|
8 |
+
# Copy requirements first for better caching
|
9 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
10 |
+
|
11 |
+
# Install dependencies
|
12 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
13 |
+
|
14 |
+
# Copy application files
|
15 |
+
COPY --chown=user . /app
|
16 |
+
|
17 |
+
# Switch to non-root user
|
18 |
+
USER user
|
19 |
+
|
20 |
+
# Set environment variables
|
21 |
ENV PYTHONUNBUFFERED=1
|
22 |
+
ENV PYSCOUT_MODE=api
|
23 |
+
ENV HOST=0.0.0.0
|
24 |
+
ENV PORT=7860
|
25 |
+
|
26 |
+
# Run the application
|
27 |
ENTRYPOINT ["python", "docker_entrypoint.py"]
|
28 |
+
|
docker_entrypoint.py
CHANGED
@@ -1,7 +1,4 @@
|
|
1 |
#!/usr/bin/env python3
|
2 |
-
"""
|
3 |
-
Docker entrypoint script that decides which component to run based on environment variables.
|
4 |
-
"""
|
5 |
import os
|
6 |
import sys
|
7 |
import subprocess
|
@@ -12,7 +9,6 @@ def run_command(cmd):
|
|
12 |
print(f"Running command: {' '.join(cmd)}")
|
13 |
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
14 |
|
15 |
-
# Stream the output
|
16 |
for line in process.stdout:
|
17 |
sys.stdout.write(line)
|
18 |
sys.stdout.flush()
|
@@ -21,7 +17,6 @@ def run_command(cmd):
|
|
21 |
return process.returncode
|
22 |
|
23 |
def check_required_files():
|
24 |
-
"""Check if all required files exist"""
|
25 |
required_files = [
|
26 |
"pyscout_api.py",
|
27 |
"deepinfra_client.py",
|
@@ -38,7 +33,6 @@ def check_required_files():
|
|
38 |
return True
|
39 |
|
40 |
def wait_for_mongodb():
|
41 |
-
"""Wait for MongoDB to be available"""
|
42 |
import time
|
43 |
import pymongo
|
44 |
|
@@ -51,7 +45,7 @@ def wait_for_mongodb():
|
|
51 |
for attempt in range(max_attempts):
|
52 |
try:
|
53 |
client = pymongo.MongoClient(mongo_uri, serverSelectionTimeoutMS=5000)
|
54 |
-
client.admin.command('ping')
|
55 |
print(f"MongoDB connection successful after {attempt+1} attempts")
|
56 |
return True
|
57 |
except Exception as e:
|
@@ -62,41 +56,24 @@ def wait_for_mongodb():
|
|
62 |
return False
|
63 |
|
64 |
def main():
|
65 |
-
"""Main entry point for the Docker container"""
|
66 |
if not check_required_files():
|
67 |
sys.exit(1)
|
68 |
|
69 |
-
#
|
70 |
-
|
|
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
elif mode == "all":
|
84 |
-
print("Starting both API server and UI")
|
85 |
-
# Start API server in background
|
86 |
-
api_process = subprocess.Popen(["python", "pyscout_api.py"])
|
87 |
-
time.sleep(5) # Wait for API to start
|
88 |
-
|
89 |
-
# Start UI in foreground
|
90 |
-
ui_cmd = ["python", "app.py"]
|
91 |
-
ui_code = run_command(ui_cmd)
|
92 |
-
|
93 |
-
# Kill API process when UI exits
|
94 |
-
api_process.terminate()
|
95 |
-
return ui_code
|
96 |
-
|
97 |
-
else:
|
98 |
-
print(f"ERROR: Unknown mode '{mode}'. Valid options: api, ui, all")
|
99 |
-
return 1
|
100 |
|
101 |
if __name__ == "__main__":
|
102 |
-
sys.exit(main())
|
|
|
1 |
#!/usr/bin/env python3
|
|
|
|
|
|
|
2 |
import os
|
3 |
import sys
|
4 |
import subprocess
|
|
|
9 |
print(f"Running command: {' '.join(cmd)}")
|
10 |
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
11 |
|
|
|
12 |
for line in process.stdout:
|
13 |
sys.stdout.write(line)
|
14 |
sys.stdout.flush()
|
|
|
17 |
return process.returncode
|
18 |
|
19 |
def check_required_files():
|
|
|
20 |
required_files = [
|
21 |
"pyscout_api.py",
|
22 |
"deepinfra_client.py",
|
|
|
33 |
return True
|
34 |
|
35 |
def wait_for_mongodb():
|
|
|
36 |
import time
|
37 |
import pymongo
|
38 |
|
|
|
45 |
for attempt in range(max_attempts):
|
46 |
try:
|
47 |
client = pymongo.MongoClient(mongo_uri, serverSelectionTimeoutMS=5000)
|
48 |
+
client.admin.command('ping')
|
49 |
print(f"MongoDB connection successful after {attempt+1} attempts")
|
50 |
return True
|
51 |
except Exception as e:
|
|
|
56 |
return False
|
57 |
|
58 |
def main():
|
|
|
59 |
if not check_required_files():
|
60 |
sys.exit(1)
|
61 |
|
62 |
+
# Always run in API mode with fixed host and port
|
63 |
+
print("Starting PyScoutAI API server")
|
64 |
+
wait_for_mongodb()
|
65 |
|
66 |
+
host = os.environ.get("HOST", "0.0.0.0")
|
67 |
+
port = int(os.environ.get("PORT", "7860"))
|
68 |
+
|
69 |
+
cmd = [
|
70 |
+
"uvicorn",
|
71 |
+
"pyscout_api:app",
|
72 |
+
"--host", host,
|
73 |
+
"--port", str(port)
|
74 |
+
]
|
75 |
+
|
76 |
+
return run_command(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
if __name__ == "__main__":
|
79 |
+
sys.exit(main())
|