mirror of
https://github.com/narrrl/holy-flavor-rater.git
synced 2026-11-03 16:41:27 +01:00
improved docker startup: frontend now waits for healthy backend
This commit is contained in:
1 parent
2774516051
commit
42e6905db4
5 files changed
+42
-2
No files matched your search
@@ -10,6 +10,7 @@ WORKDIR /app
|
||||
|
||||
# Install dependencies
|
||||
COPY requirements.txt .
|
||||
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
# Copy the rest of the backend code
|
||||
|
||||
@@ -6,12 +6,19 @@ from django.conf import settings
|
||||
from django.conf.urls.static import static
|
||||
from django.views.static import serve
|
||||
from django.views.generic.base import TemplateView
|
||||
from django.http import HttpResponse
|
||||
import os
|
||||
|
||||
def health_check(request):
|
||||
return HttpResponse("OK", content_type="text/plain")
|
||||
|
||||
# Clean the admin path
|
||||
admin_url = os.environ.get('ADMIN_URL', 'admin').strip('/')
|
||||
|
||||
urlpatterns = [
|
||||
# Health check
|
||||
path('health/', health_check, name='health_check'),
|
||||
|
||||
# Robots.txt
|
||||
path('robots.txt', TemplateView.as_view(template_name="robots.txt", content_type="text/plain")),
|
||||
|
||||
|
||||
+8
-1
@@ -23,6 +23,12 @@ services:
|
||||
- "traefik.http.routers.holy-energy-backend.entrypoints=websecure"
|
||||
- "traefik.http.routers.holy-energy-backend.tls.certresolver=https_resolver"
|
||||
- "traefik.http.services.holy-energy-backend.loadbalancer.server.port=8000"
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://localhost:8000/health/"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
start_period: 30s
|
||||
|
||||
frontend:
|
||||
build:
|
||||
@@ -41,7 +47,8 @@ services:
|
||||
env_file:
|
||||
- .env.narl.io
|
||||
depends_on:
|
||||
- backend
|
||||
backend:
|
||||
condition: service_healthy
|
||||
labels:
|
||||
- "traefik.enable=true"
|
||||
- "traefik.docker.network=narl"
|
||||
|
||||
+5
-1
@@ -27,6 +27,10 @@ FROM nginx:stable-alpine
|
||||
# Copy built files
|
||||
COPY --from=build-stage /app/dist /usr/share/nginx/html
|
||||
|
||||
# Copy entrypoint script
|
||||
COPY entrypoint.sh /entrypoint.sh
|
||||
RUN chmod +x /entrypoint.sh
|
||||
|
||||
# Custom Nginx config to handle React Router (SPA)
|
||||
RUN printf 'server {\n\
|
||||
listen 80;\n\
|
||||
@@ -38,4 +42,4 @@ RUN printf 'server {\n\
|
||||
}' > /etc/nginx/conf.d/default.conf
|
||||
|
||||
EXPOSE 80
|
||||
CMD ["nginx", "-g", "daemon off;"]
|
||||
ENTRYPOINT ["/entrypoint.sh"]
|
||||
@@ -0,0 +1,21 @@
|
||||
#!/bin/sh
|
||||
|
||||
# The backend hostname is 'holy-backend' based on docker-compose.yml
|
||||
# We wait for port 8000 to be open
|
||||
echo "Waiting for backend (holy-backend:8000) to be available..."
|
||||
|
||||
MAX_RETRIES=30
|
||||
COUNT=0
|
||||
|
||||
while ! nc -z holy-backend 8000; do
|
||||
COUNT=$((COUNT + 1))
|
||||
if [ $COUNT -ge $MAX_RETRIES ]; then
|
||||
echo "Backend timed out after $MAX_RETRIES attempts - starting Nginx anyway"
|
||||
break
|
||||
fi
|
||||
echo "Backend is unavailable (attempt $COUNT/$MAX_RETRIES) - sleeping"
|
||||
sleep 2
|
||||
done
|
||||
|
||||
echo "Starting Nginx..."
|
||||
exec nginx -g "daemon off;"
|
||||
Reference in new issue
Block a user