Skip to content

Commit

Permalink
Run osmcha-frontend container in compose app
Browse files Browse the repository at this point in the history
This brings the local dev environment closer to production by using
the same frontend container. It also changes the backend to serve with
gunicorn by default, even in dev mode. Also, static files are now always
served by the frontend and never by Django, even in dev mode.
  • Loading branch information
jake-low committed Oct 1, 2024
1 parent 78f43f1 commit d6f009c
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 87 deletions.
17 changes: 9 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM python:3.10-slim-bookworm
ARG DEBIAN_FRONTEND=noninteractive
ARG REQUIREMENTS_FILE=production.txt
ARG DJANGO_SETTINGS_MODULE=config.settings.production

RUN apt-get update -qq -y \
&& apt-get install -y curl wget python3 python3-dev python3-pip git \
Expand All @@ -10,21 +12,20 @@ RUN apt-get update -qq -y \

# Requirements have to be pulled and installed here, otherwise caching won't work
COPY ./requirements /requirements

RUN pip install -r /requirements/production.txt
RUN pip install -r /requirements/$REQUIREMENTS_FILE

COPY . /app
RUN useradd django
RUN chown -R django:django /app
WORKDIR /app

RUN python manage.py collectstatic --noinput

COPY ./compose/django/gunicorn.sh /gunicorn.sh
COPY ./compose/django/entrypoint.sh /entrypoint.sh
RUN sed -i 's/\r//' /entrypoint.sh \
&& sed -i 's/\r//' /gunicorn.sh \
&& chmod +x /entrypoint.sh \
&& chmod +x /gunicorn.sh
RUN chmod +x /entrypoint.sh

WORKDIR /app
USER django
VOLUME /app/staticfiles

ENTRYPOINT ["/entrypoint.sh"]
CMD ["gunicorn", "config.wsgi", "-w", "4", "-b", "0.0.0.0:5000", "--chdir", "/app"]
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

3 changes: 0 additions & 3 deletions compose/django/gunicorn.sh

This file was deleted.

2 changes: 1 addition & 1 deletion compose/django/start-dev.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh
python manage.py migrate
python manage.py runserver_plus 0.0.0.0:8000
python manage.py runserver_plus 0.0.0.0:5000
4 changes: 0 additions & 4 deletions compose/nginx/Dockerfile

This file was deleted.

52 changes: 0 additions & 52 deletions compose/nginx/nginx.conf

This file was deleted.

2 changes: 1 addition & 1 deletion config/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
STATIC_ROOT = str(ROOT_DIR('staticfiles'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = '/static/'
STATIC_URL = '/static/django/'

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = []
Expand Down
13 changes: 1 addition & 12 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@

urlpatterns = []

# If static files are not intercepted by the web-server, serve them with the dev-server:
if settings.DEBUG is False: # if DEBUG is True it will be served automatically
urlpatterns += [
path(
'static/<path>',
static_views.serve,
{'document_root': settings.STATIC_ROOT}
),
]

api_urls = [
path(
'{}'.format(API_BASE_URL),
Expand Down Expand Up @@ -90,8 +80,7 @@ def health_check(request):
'',
include(api_urls)
),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
]

if settings.DEBUG:
# This allows the error pages to be debugged during development, just visit
Expand Down
20 changes: 15 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,27 @@ services:
depends_on:
- postgres
- redis
command: /gunicorn.sh
volumes:
# copy static files into a shared volume so the frontend can serve them
- staticfiles:/app/staticfiles
env_file: .env

nginx:
build: ./compose/nginx
frontend:
image: osmcha-frontend
depends_on:
- django
volumes:
# frontend serves django app's static files via shared volume
- staticfiles:/srv/www/static/django
env_file: .env

ports:
- "0.0.0.0:80:80"

- "0.0.0.0:8000:80"

redis:
image: redis:latest

volumes:
staticfiles:
postgres_data:
postgres_backup:
1 change: 1 addition & 0 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_DB=

BACKEND_URL=http://django:5000
REDIS_URL=redis://redis:6379

# General settings
Expand Down

0 comments on commit d6f009c

Please sign in to comment.