From 8fbc080ef9be6d813eb123ab40832199dc4ed101 Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Sun, 15 Sep 2024 23:24:19 -0600 Subject: [PATCH] feat: containerize the project --- CHANGELOG.md | 4 ++++ Dockerfile | 27 ++++++++++++++++++++++++--- docker-compose-prod.yml | 8 ++------ docker-compose.override.yml | 8 ++++++-- docker-compose.yml | 11 +++++------ justfile | 2 +- nginx.conf | 13 ------------- test/integration/test_deployment.py | 2 +- uwsgi.ini | 14 +++++++------- 9 files changed, 50 insertions(+), 39 deletions(-) delete mode 100644 nginx.conf diff --git a/CHANGELOG.md b/CHANGELOG.md index bae3869..3d52a9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## Next Major Release + +Containerizes the project + ## v1.1.0 (2024-07-18) - Adds support for Python 3.12 diff --git a/Dockerfile b/Dockerfile index 06c8373..1d4fa8d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,25 @@ -FROM nginx:1.25-alpine +FROM python:3.12 -RUN rm /etc/nginx/conf.d/default.conf -COPY nginx.conf /etc/nginx/conf.d +# Helps with Docker logging +ENV PYTHONUNBUFFERED=1 + +WORKDIR /app + +RUN apt-get update && \ + apt-get install -y \ + ca-certificates \ + curl \ + gnupg \ + lsb-release && \ + curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add - && \ + echo "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list && \ + apt-get update && \ + apt-get install -y docker-ce-cli + +COPY . . + +RUN pip install -e . + +EXPOSE 5000 + +ENTRYPOINT [ "uwsgi", "--ini", "uwsgi.ini" ] diff --git a/docker-compose-prod.yml b/docker-compose-prod.yml index f04d69f..c8f9f88 100644 --- a/docker-compose-prod.yml +++ b/docker-compose-prod.yml @@ -1,8 +1,8 @@ services: - harvey-nginx: + harvey: deploy: replicas: 2 - mem_limit: '512m' + mem_limit: '1024m' healthcheck: test: ['CMD-SHELL', 'if [ ! -e /var/run/nginx.pid ]; then exit 1; else exit 0; fi'] start_period: 10s @@ -10,7 +10,3 @@ services: interval: 30s timeout: 10s retries: 3 - labels: - - 'traefik.http.routers.harveyapi.rule=Host(`harveyapi.justinpaulhammond.com`) || Host(`www.harveyapi.justinpaulhammond.com`)' - - 'traefik.http.routers.harveyapi.tls=true' - - 'traefik.http.routers.harveyapi.tls.certresolver=letsencrypt' diff --git a/docker-compose.override.yml b/docker-compose.override.yml index e8d3b03..4549c12 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -1,4 +1,8 @@ services: - harvey-nginx: + harvey: labels: - - "traefik.http.routers.harveyapi.rule=Host(`harveyapi.localhost`)" + - 'traefik.enable=true' + - 'traefik.http.routers.harveyapi.rule=Host(`harveyapi.localhost`)' + - 'traefik.http.services.harveyapi.loadbalancer.server.port=5000' + volumes: + - /Users/jhammond/harvey:/lala diff --git a/docker-compose.yml b/docker-compose.yml index 61a7417..4eb69ac 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,14 +1,13 @@ services: - harvey-nginx: + harvey: build: . restart: always networks: - traefik - labels: - - traefik.enable=true - - 'traefik.docker.network=traefik' - extra_hosts: - - 'host.docker.internal:host-gateway' + volumes: + - /var/run/docker.sock:/var/run/docker.sock:ro + - /Users/jhammond/harvey:/lala + - ./:/app networks: traefik: diff --git a/justfile b/justfile index ec9e112..d6bf564 100644 --- a/justfile +++ b/justfile @@ -47,7 +47,7 @@ install: # Test the project end-to-end integration: - venv/bin/python {{TEST_DIR}}/integration/test_deployment.py + python {{TEST_DIR}}/integration/test_deployment.py # Sorts imports throughout the project isort: diff --git a/nginx.conf b/nginx.conf deleted file mode 100644 index 9527a4b..0000000 --- a/nginx.conf +++ /dev/null @@ -1,13 +0,0 @@ -server { - listen 80; - error_log /var/log/nginx/error.log; - access_log /var/log/nginx/access.log; - - location / { - include uwsgi_params; - # This uwsgi pass only works for Docker Desktop - uwsgi_pass host.docker.internal:5000; - uwsgi_read_timeout 30s; - uwsgi_send_timeout 30s; - } -} diff --git a/test/integration/test_deployment.py b/test/integration/test_deployment.py index d1a2809..0640ad5 100644 --- a/test/integration/test_deployment.py +++ b/test/integration/test_deployment.py @@ -12,7 +12,7 @@ data = json.load(file) request = requests.post( - 'http://127.0.0.1:5000/deploy', + 'http://harvey.localhost/deploy', json=data, headers={ 'Content-Type': 'application/json', diff --git a/uwsgi.ini b/uwsgi.ini index bb20c07..15adc66 100644 --- a/uwsgi.ini +++ b/uwsgi.ini @@ -19,20 +19,20 @@ stats = /tmp/harvey.stats memory-report = true ; app setup -uwsgi-socket = 127.0.0.1:5000 +http = 0.0.0.0:5000 module = wsgi:APP ; tuning -workers = 2 -max-worker-lifetime = 300 -socket-timeout = 30 +# workers = 2 +# max-worker-lifetime = 300 +harakiri = 30 buffer-size = 24576 # crons -cron = 0 0 -1 -1 -1 venv/bin/python scripts/remove_old_logs.py +# cron = 0 0 -1 -1 -1 venv/bin/python scripts/remove_old_logs.py ; logging -daemonize = $(HOME)/harvey/logs/harvey.log +# daemonize = $(HOME)/harvey/logs/harvey.log ; TODO: unify timestamps since internal Python is logging with `asctime` log-format = %(ltime) [pid: %(pid)|app: -|req: -/-] %(addr) (%(user)) {%(vars) vars in %(pktsize) bytes} %(method) %(uri) => generated %(rsize) bytes in %(msecs) msecs (%(proto) %(status)) %(headers) headers in %(hsize) bytes (%(switches) switches on core %(core)) -log-maxsize = 2000000 ; 2mb +; log-maxsize = 2000000 ; 2mb