0
0
Fork 0
mirror of https://github.com/healthchecks/healthchecks.git synced 2025-04-08 14:40:05 +00:00

Fix the Docker healthcheck script to supply correct Host header

Commit 8fed685f12 added a HEALTHCHECK
instruction in the Dockerfile. The healthcheck script calls http://localhost:8000/api/v3/status/, which fails if localhost is not in ALLOWED_HOSTS.

With this change, the healthcheck script is now a Django management
command. It reads Django's ALLOWED_HOSTS setting, grabs the first
element, and uses it in the "Host:" HTTP header when making a HTTP
request.

cc: 
This commit is contained in:
Pēteris Caune 2024-08-21 15:52:19 +03:00
parent 027fcc1097
commit 320a7c7733
No known key found for this signature in database
GPG key ID: E28D7679E9A9EDE2
4 changed files with 27 additions and 2 deletions

View file

@ -1,6 +1,11 @@
# Changelog
All notable changes to this project will be documented in this file.
## v3.5.2 - 2024-08-21
### Bug Fixes
- Fix the Docker healthcheck script to supply correct Host header (#1051)
## v3.5.1 - 2024-08-20
### Bug Fixes

View file

@ -33,6 +33,7 @@ RUN --mount=type=bind,target=/wheels,source=/wheels,from=builder \
pip install --no-cache /wheels/*
COPY --from=builder /opt/healthchecks/ /opt/healthchecks/
COPY docker/fetchstatus.py /opt/healthchecks/hc/api/management/commands/
RUN \
rm -f /opt/healthchecks/hc/local_settings.py && \
@ -44,5 +45,5 @@ RUN mkdir /data && chown hc /data
USER hc
ENV USE_GZIP_MIDDLEWARE=True
HEALTHCHECK --interval=10s --start-period=10s --retries=1 CMD /opt/healthchecks/docker/fetchstatus.sh
HEALTHCHECK --interval=10s --start-period=10s --retries=1 CMD ./manage.py fetchstatus
CMD [ "uwsgi", "/opt/healthchecks/docker/uwsgi.ini"]

20
docker/fetchstatus.py Normal file
View file

@ -0,0 +1,20 @@
from __future__ import annotations
from typing import Any
from urllib.request import Request, urlopen
from django.conf import settings
from django.core.management.base import BaseCommand
class Command(BaseCommand):
def handle(self, **options: Any) -> str:
host = settings.ALLOWED_HOSTS[0]
if host == "*":
host = "localhost"
req = Request("http://localhost:8000/api/v3/status/", headers={"Host": host})
with urlopen(req) as response:
assert response.status == 200
return "Status OK"

View file

@ -1 +0,0 @@
python -c "from urllib.request import urlopen; urlopen('http://localhost:8000/api/v3/status/')"