I defined a super simple healthcheck endpoint in my Django app (urls.py) to be used in a docker compose environment, like:
services: django: build: context: . dockerfile: ./compose/production/django/Dockerfile depends_on: db: condition: service_healthy env_file: - ./.envs/.production/.django command: /start healthcheck: test: ["CMD", "curl", "-f", "http://localhost:5000/healthcheck/"] start_period: 20s interval: 15s timeout: 10s retries: 5 start.sh
#!/bin/bash set -o errexit set -o pipefail set -o nounset python /app/manage.py collectstatic --noinput exec /usr/local/bin/gunicorn config.asgi --bind 0.0.0.0:5000 --chdir=/app -k uvicorn.workers.UvicornWorker # ... various imports def healthcheck(): return HttpResponse() urlpatterns = [ # Django Admin, use {% url 'admin:index' %} path(settings.ADMIN_URL, admin.site.urls), # User management path("accounts/", include("allauth.urls")), # Hijack path("hijack/", include("hijack.urls")), # Healthcheck path("healthcheck/", healthcheck, name="healthcheck"), # Media files *static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT), ] if settings.DEBUG: # Static file serving when using Gunicorn + Uvicorn for local web socket development urlpatterns += staticfiles_urlpatterns() In a local environment there are no issues since localhost is inside the ALLOWED_HOSTS configuration variable. But if I'm in a production environment and adding "localhost" to the allowed hosts list doesn't seem like a good practice.
Since the healthcheck endpoint is only used internally is there a way to resolve this? On the top of my head I'm thinking of something to skip this check just for that endpoint but I don't know how or if it is even possible. Maybe there are even other better solutions?
Hostheader to the suitable value when using curl. See the linked duplicate which demonstrates how to set headers while using curl.