From 739b88e92ca9bcec630fe4a541d7567190ef87cc Mon Sep 17 00:00:00 2001 From: mukeshs Date: Fri, 17 Oct 2025 11:04:16 +0530 Subject: [PATCH] Docker buid issue --- Machine-Backend/Dockerfile | 16 ++------------ Machine-Backend/wait_for_db.py | 38 ++++++++++++++++++++++++++++++++++ docker-compose.yml | 22 ++++---------------- 3 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 Machine-Backend/wait_for_db.py diff --git a/Machine-Backend/Dockerfile b/Machine-Backend/Dockerfile index 2c25b1f..de49064 100644 --- a/Machine-Backend/Dockerfile +++ b/Machine-Backend/Dockerfile @@ -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"] \ No newline at end of file diff --git a/Machine-Backend/wait_for_db.py b/Machine-Backend/wait_for_db.py new file mode 100644 index 0000000..28618ea --- /dev/null +++ b/Machine-Backend/wait_for_db.py @@ -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() \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index ea4ffef..025d42e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -40,26 +40,12 @@ services: db: condition: service_healthy restart: always - healthcheck: - test: ["CMD", "curl", "-f", "http://localhost:5000/health"] - interval: 30s - timeout: 10s - retries: 3 - start_period: 40s command: > sh -c " - echo '⏳ Waiting for MySQL to be ready for connections...' - for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do - if mysql -h db -u vendinguser -pvendingpass -e 'SELECT 1' >/dev/null 2>&1; then - echo '✓ MySQL is ready!' - break - fi - echo 'Waiting for MySQL... (attempt' $$i'/15)' - sleep 3 - done - echo '🌱 Running database seed script...' - python seed_user.py - echo '🚀 Starting Gunicorn server...' + python wait_for_db.py && + echo '🌱 Running database seed script...' && + python seed_user.py && + echo '🚀 Starting Gunicorn server...' && gunicorn --bind 0.0.0.0:5000 --workers 2 --timeout 120 run:app "