From ef3f2aee107795930c8afa9d054289d45cb80004 Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Fri, 25 Oct 2024 22:57:10 -0400 Subject: [PATCH] =?UTF-8?q?Fast.=20Easy.=20Fun.=20Deployment=20?= =?UTF-8?q?=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/Dockerfile | 17 ++++++++++++----- backend/nginx.conf | 38 ++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 39 +++++++++++++-------------------------- 3 files changed, 63 insertions(+), 31 deletions(-) create mode 100644 backend/nginx.conf diff --git a/backend/Dockerfile b/backend/Dockerfile index 6de1067..1dbe388 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -1,5 +1,4 @@ -# Dockerfile - +# Use the official Python slim image as the base image FROM python:3.10-slim LABEL Developers="Sean Morley" @@ -11,9 +10,9 @@ ENV PYTHONUNBUFFERED 1 # Set the working directory WORKDIR /code -# Install system dependencies +# Install system dependencies (Nginx included) RUN apt-get update \ - && apt-get install -y git postgresql-client gdal-bin libgdal-dev \ + && apt-get install -y git postgresql-client gdal-bin libgdal-dev nginx \ && apt-get clean # Install Python dependencies @@ -24,10 +23,18 @@ RUN pip install -r requirements.txt # Copy the Django project code into the Docker image COPY ./server /code/ +# Copy Nginx configuration +COPY ./nginx.conf /etc/nginx/nginx.conf + # Collect static files RUN python3 manage.py collectstatic --noinput --verbosity 2 # Set the entrypoint script COPY ./entrypoint.sh /code/entrypoint.sh RUN chmod +x /code/entrypoint.sh -ENTRYPOINT ["/code/entrypoint.sh"] + +# Expose ports for NGINX and Gunicorn +EXPOSE 80 8000 + +# Command to start Nginx and Gunicorn +CMD ["bash", "-c", "service nginx start && /code/entrypoint.sh"] diff --git a/backend/nginx.conf b/backend/nginx.conf new file mode 100644 index 0000000..7e123c0 --- /dev/null +++ b/backend/nginx.conf @@ -0,0 +1,38 @@ +worker_processes 1; +events { + worker_connections 1024; +} + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + sendfile on; + keepalive_timeout 65; + + upstream django { + server server:8000; # Use the internal Docker networking + } + + server { + listen 80; # NGINX always listens on port 80 inside the container + server_name localhost; + + location / { + proxy_pass http://server:8000; # Explicitly forward to Django service + 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; + } + + + location /static/ { + alias /code/staticfiles/; # Serve static files directly + } + + location /media/ { + alias /code/media/; # Serve media files directly + } + } +} diff --git a/docker-compose.yml b/docker-compose.yml index 8e5a91e..fee119b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,15 +1,15 @@ services: web: - #build: ./frontend/ - image: ghcr.io/seanmorley15/adventurelog-frontend:latest + build: ./frontend/ + #image: ghcr.io/seanmorley15/adventurelog-frontend:latest container_name: adventurelog-frontend restart: unless-stopped environment: - - PUBLIC_SERVER_URL=http://server:8000 # MOST DOCKER USERS WILL NEVER NEED TO CHANGE THIS, EVEN IF YOU CHANGE THE PORTS - - ORIGIN=http://localhost:8080 - - BODY_SIZE_LIMIT=Infinity # This is measured in bytes + - PUBLIC_SERVER_URL=http://server:8000 # MOST DOCKER USERS WILL NOT NEED TO CHANGE THIS EVER EVEN IF YOU CHANGE THE OUTWARD PORT + - ORIGIN=http://localhost:8015 + - BODY_SIZE_LIMIT=Infinity ports: - - "8080:3000" + - "8015:3000" depends_on: - server @@ -25,43 +25,30 @@ services: - postgres_data:/var/lib/postgresql/data/ server: - #build: ./backend/ - image: ghcr.io/seanmorley15/adventurelog-backend:latest + build: ./backend/ + #image: ghcr.io/seanmorley15/adventurelog-backend:latest container_name: adventurelog-backend restart: unless-stopped environment: - PGHOST=db - PGDATABASE=database - PGUSER=adventure - - PGPASSWORD=changeme123 # This should be the same as the POSTGRES_PASSWORD in the db service + - PGPASSWORD=changeme123 - SECRET_KEY=changeme123 - DJANGO_ADMIN_USERNAME=admin - DJANGO_ADMIN_PASSWORD=admin - DJANGO_ADMIN_EMAIL=admin@example.com - - PUBLIC_URL='http://localhost:81' # NOTE: THIS IS THE PUBLIC URL TO THE **NGINX** SERVER USED FOR MEDIA FILES! - - CSRF_TRUSTED_ORIGINS=https://api.adventurelog.app,https://adventurelog.app # This is a comma separated list of trusted origins for CSRF, this should include where your frontend is hosted. + - PUBLIC_URL='http://localhost:8016' # Match the outward port, used for the creation of image urls + - CSRF_TRUSTED_ORIGINS=http://localhost:8016 # Comma separated list of trusted origins for CSRF - DEBUG=False - - FRONTEND_URL='http://localhost:8080' # This is the URL of the frontend server - #- DISABLE_REGISTRATION=True + - FRONTEND_URL='http://localhost:8015' # Used for email generation. This should be the url of the frontend ports: - - "8000:8000" + - "8016:80" # User can change this to any outward port without breaking the setup depends_on: - db volumes: - adventurelog_media:/code/media/ - nginx: - image: nginx:latest - container_name: adventurelog-nginx - restart: unless-stopped - ports: - - "81:80" - volumes: - - adventurelog_media:/app/media - - ./proxy/nginx.conf:/etc/nginx/conf.d/default.conf:ro - depends_on: - - server - volumes: postgres_data: adventurelog_media: