Skip to content

Commit

Permalink
Merge pull request #72 from wildjames/dev
Browse files Browse the repository at this point in the history
Fix deployment?
  • Loading branch information
wildjames authored Nov 5, 2023
2 parents 7cf6b07 + 261275a commit 9a11682
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 47 deletions.
43 changes: 28 additions & 15 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,36 @@ services:
web:
build: .
ports:
- "8000:8000"
- "80:80"
environment:
EMAIL_HOST: smtp-mail.outlook.com
EMAIL_PORT: 587
EMAIL_USE_TLS: "1"
EMAIL_HOST_USER: [email protected]
DEFAULT_FROM_EMAIL: [email protected]
EMAIL_HOST_PASSWORD: password
DEBUG: "0"
EMAIL_HOST_USER:
DEFAULT_FROM_EMAIL:
EMAIL_HOST_PASSWORD:

DJANGO_DEBUG: true
DJANGO_LOGGING_LEVEL: debug

DJANGO_HOST_PORT: "8000"
DJANGO_DB_NAME: todoqueue
DJANGO_DB_USER: root
DJANGO_DB_PASSWORD: password
DJANGO_DB_HOST: host.docker.internal
DJANGO_DB_PORT: "3306"
DJANGO_SUPERUSER_EMAIL: [email protected]
DJANGO_SUPERUSER_USERNAME: James
DJANGO_SUPERUSER_PASSWORD: password
volumes:
- .:/app
DJANGO_DB_NAME:
DJANGO_DB_USER:
DJANGO_DB_PASSWORD:
DJANGO_DB_HOST:
DJANGO_DB_PORT:

DJANGO_CACHE_BACKEND: "redis"
DJANGO_CACHE_LOCATION: "redis://cache:8379/1"

DJANGO_SUPERUSER_EMAIL:
DJANGO_SUPERUSER_USERNAME:
DJANGO_SUPERUSER_PASSWORD:
depends_on:
- cache

cache:
image: "redis:alpine"
ports:
- "8379:6379"

60 changes: 36 additions & 24 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
# Use an official Python runtime as base image
FROM python:3.11-slim
# Stage 1: Build the React frontend
FROM node:21 as frontend-builder
WORKDIR /app
COPY todoqueue_frontend/package.json todoqueue_frontend/package-lock.json ./
RUN npm install
COPY todoqueue_frontend ./
RUN npm run build


# Stage 2: Build the Django backend
FROM python:3.12-slim as backend-builder
WORKDIR /app
COPY todoqueue_backend/requirements.txt ./
RUN apt-get update \
&& apt-get install -y --no-install-recommends gcc libpq-dev python3-dev default-libmysqlclient-dev pkg-config \
&& pip install --upgrade pip \
&& pip install -r requirements.txt
COPY todoqueue_backend todoqueue_backend
RUN ls -alh


# Install system dependencies
# Stage 3: Setup Nginx and final image
FROM python:3.12-slim
# Install Nginx
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
gcc \
libpq-dev \
python3-dev \
default-libmysqlclient-dev \
pkg-config \
npm \
&& apt-get install -y --no-install-recommends nginx default-libmysqlclient-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY ./todoqueue_backend/requirements.txt /app/
WORKDIR /app
RUN pip install --upgrade pip \
&& pip install -r requirements.txt
# Copy the built React static files from the frontend-builder stage
COPY --from=frontend-builder /app/build /var/www/html

# Copy Django application
COPY todoqueue_backend /app/todoqueue_backend
# Copy the installed dependencies from the backend-builder stage
COPY --from=backend-builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=backend-builder /usr/local/bin /usr/local/bin

# Copy frontend application, and build it
COPY todoqueue_frontend /app/todoqueue_frontend
WORKDIR /app/todoqueue_frontend
RUN npm install
RUN npm run build
# Copy the Django application from the backend-builder stage
WORKDIR /app
COPY --from=backend-builder /app/todoqueue_backend /app/

WORKDIR /app/todoqueue_backend
# Add Nginx configuration file
COPY ./nginx.conf /etc/nginx/nginx.conf

CMD ["./run_server.sh"]
# Start Nginx and the Django application using the entrypoint script
CMD ["./run_server.sh"]
38 changes: 38 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
daemon off;

events {}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;


# Set up the server
server {
listen 80;

# Serve static files for the frontend
location / {
root /var/www/html;
try_files $uri /index.html;
}

# Proxy requests to /api to the Django backend
location /api {
proxy_pass http://localhost:8000;
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;
}

# Proxy requests to /api to the Django backend
location /admin {
proxy_pass http://localhost:8000;
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;
}
}
}
5 changes: 4 additions & 1 deletion todoqueue_backend/run_server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ python manage.py createsuperuser --noinput || true
echo "Collecting static files..." && \
python manage.py collectstatic --noinput && \

# Start Nginx in the background
echo "Starting Nginx..."
nginx &

# Start the Django server
echo "Starting the Django server..."

exec gunicorn todoqueue_backend.wsgi:application --bind 0.0.0.0:$DJANGO_HOST_PORT
2 changes: 0 additions & 2 deletions todoqueue_backend/todoqueue_backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@
# Static files configurations
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [os.path.join(BASE_DIR, "../todoqueue_frontend/build/static/")]
logger.info(f"Static files dirs: {STATICFILES_DIRS}")


# email settings
Expand Down
5 changes: 0 additions & 5 deletions todoqueue_backend/todoqueue_backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,4 @@
path("admin/", admin.site.urls),
path("api/", include("tasks.urls")),
path("api/", include("accounts.urls")),

# Serve the built React app
re_path(r'^static/(?P<path>.*)$', serve),
# Catch-all route to serve the React SPA
re_path(r'^.*$', TemplateView.as_view(template_name='index.html')),
]

0 comments on commit 9a11682

Please sign in to comment.