Docker buid issue

This commit is contained in:
2025-10-17 11:04:16 +05:30
parent a3d3fa4435
commit 739b88e92c
3 changed files with 44 additions and 32 deletions

View File

@ -1,36 +1,24 @@
# Use official Python base image (faster & smaller than Ubuntu + manual Python)
FROM python:3.10-slim
# Set working directory
WORKDIR /app
# Prevent Python from buffering stdout/stderr
ENV PYTHONUNBUFFERED=1
# Install mysql-client for database connectivity checks
RUN apt-get update && \
apt-get install -y default-mysql-client curl && \
rm -rf /var/lib/apt/lists/*
# Copy requirements first for better caching
COPY requirements.txt .
# Use PyPI mirror + increased timeout to avoid network errors
# Install Python packages with timeout
RUN pip install --no-cache-dir -r requirements.txt \
-i https://pypi.org/simple \
--timeout=100
--timeout=100 || pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY . .
# Environment variables
ENV FLASK_ENV=production
ENV FLASK_APP=run.py
ENV FLASK_RUN_HOST=0.0.0.0
ENV FLASK_RUN_PORT=5000
# Expose the Flask port
EXPOSE 5000
# Use Gunicorn for production
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "run:app"]

View File

@ -0,0 +1,38 @@
import time
import sys
import os
from sqlalchemy import create_engine
from sqlalchemy.exc import OperationalError
def wait_for_db(max_retries=30, retry_delay=2):
"""Wait for database to be ready"""
mysql_host = os.getenv('MYSQL_HOST', 'db')
mysql_user = os.getenv('MYSQL_USER', 'vendinguser')
mysql_password = os.getenv('MYSQL_PASSWORD', 'vendingpass')
mysql_db = os.getenv('MYSQL_DATABASE', 'vending')
db_uri = f'mysql+pymysql://{mysql_user}:{mysql_password}@{mysql_host}:3306/{mysql_db}'
print(f"⏳ Waiting for MySQL at {mysql_host}:3306...")
for attempt in range(1, max_retries + 1):
try:
engine = create_engine(db_uri)
connection = engine.connect()
connection.close()
print("✓ MySQL is ready!")
return True
except OperationalError as e:
if attempt < max_retries:
print(f"Waiting for MySQL... (attempt {attempt}/{max_retries})")
time.sleep(retry_delay)
else:
print(f"❌ Failed to connect to MySQL after {max_retries} attempts")
print(f"Error: {e}")
sys.exit(1)
return False
if __name__ == '__main__':
wait_for_db()