From 871d604ef4b2b1fba4403ebe269d4c4f3a1831ba Mon Sep 17 00:00:00 2001 From: Shihaam Abdul Rahman Date: Sat, 8 Feb 2025 17:25:44 +0500 Subject: [PATCH] builds --- .build/prod/api.Dockerfile | 38 ++++++++++++++++++++++++++++++++++++ .build/prod/compose.yml | 13 ++++++++++++ .build/prod/entrypoint.sh | 14 +++++++++++++ .build/prod/nginx.Dockerfile | 11 +++++++++++ .build/prod/nginx.conf | 22 +++++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 .build/prod/api.Dockerfile create mode 100644 .build/prod/compose.yml create mode 100755 .build/prod/entrypoint.sh create mode 100644 .build/prod/nginx.Dockerfile create mode 100644 .build/prod/nginx.conf diff --git a/.build/prod/api.Dockerfile b/.build/prod/api.Dockerfile new file mode 100644 index 0000000..5b3cf04 --- /dev/null +++ b/.build/prod/api.Dockerfile @@ -0,0 +1,38 @@ +FROM python:3.11.4-slim-buster AS builder + +WORKDIR /var/www/html/ + +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +RUN apt-get update && \ + apt-get install -y --no-install-recommends gcc + +RUN pip install --upgrade pip +COPY . /var/www/html/ +COPY ./requirements.txt . +RUN pip wheel --no-cache-dir --no-deps --wheel-dir /var/www/html/wheels -r requirements.txt + +FROM python:3.11.4-slim-buster + +WORKDIR /var/www/html/ + +RUN mkdir /var/www/html/staticfiles -p && chmod -R 777 /var/www/html/staticfiles + +RUN apt-get update && apt-get install -y --no-install-recommends netcat +COPY --from=builder /var/www/html/wheels /wheels +COPY --from=builder /var/www/html/requirements.txt . +RUN pip install --upgrade pip +RUN pip install --no-cache /wheels/* + +COPY . /var/www/html/ +# copy entrypoint.prod.sh +COPY .build/prod/entrypoint.sh /entrypoint.sh + +#RUN python manage.py collectstatic + +ENTRYPOINT ["/entrypoint.sh"] +VOLUME /var/www/html/staticfiles + + +CMD gunicorn apibase.wsgi:application --bind 0.0.0.0:5000 --workers=2 diff --git a/.build/prod/compose.yml b/.build/prod/compose.yml new file mode 100644 index 0000000..2cbb482 --- /dev/null +++ b/.build/prod/compose.yml @@ -0,0 +1,13 @@ +services: + api: + build: + context: ../../ + dockerfile: .build/prod/api.Dockerfile + hostname: sarlink-portal-api + image: git.shihaam.dev/sarlink/sarlink-portal-api/api + nginx: + build: + context: . + dockerfile: ./nginx.Dockerfile + hostname: sarlink-portal-api-nginx + image: git.shihaam.dev/sarlink/sarlink-portal-api/nginx diff --git a/.build/prod/entrypoint.sh b/.build/prod/entrypoint.sh new file mode 100755 index 0000000..278a322 --- /dev/null +++ b/.build/prod/entrypoint.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +if [ "$DATABASE" = "postgres" ] +then + echo "Waiting for postgres..." + + while ! nc -z $POSTGRES_HOST $POSTGRES_PORT; do + sleep 0.1 + done + + echo "PostgreSQL started" +fi + +exec "$@" \ No newline at end of file diff --git a/.build/prod/nginx.Dockerfile b/.build/prod/nginx.Dockerfile new file mode 100644 index 0000000..52e6e98 --- /dev/null +++ b/.build/prod/nginx.Dockerfile @@ -0,0 +1,11 @@ +FROM nginx + +# Install basic tools +RUN apt update \ + && apt install curl nano iputils-ping zip unzip -y --no-install-recommends \ + && apt auto-remove -y \ + && apt clean -y + +COPY nginx.conf /etc/nginx/conf.d/default.conf + +WORKDIR /etc/nginx diff --git a/.build/prod/nginx.conf b/.build/prod/nginx.conf new file mode 100644 index 0000000..b322c6c --- /dev/null +++ b/.build/prod/nginx.conf @@ -0,0 +1,22 @@ +server { + listen 80; + + server_name _; + access_log /dev/stdout; + error_log /dev/stdout info; + + # Serve static files + location /static/ { + alias /var/www/html/staticfiles/; + } + + # Forward requests to Gunicorn + location / { + proxy_pass http://api:5000; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + client_max_body_size 6M; + } +}