Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(Django): update Docker & co #386

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ COMPOSE_PATH_SEPARATOR=;
# dev is default target
COMPOSE_FILE=docker-compose.yml;docker/dev.yml

API_PORT=127.0.0.1:8000
SECRET_KEY=key

DEBUG=True

# CORS
CORS_ALLOW_ORIGINS=["http://localhost","http://localhost:8000","http://localhost:5173"]
ALLOWED_HOSTS="localhost,localhost:8000,localhost:5173"

API_PORT=127.0.0.1:8000

# authentication server
OAUTH2_SERVER_URL=https://world.openfoodfacts.org/cgi/auth.pl
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,6 @@ dmypy.json

# Project specific

static/app
static/img/*/*
www/app
www/img/*/*
gh_pages
13 changes: 8 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ENV PYTHONUNBUFFERED=1 \
PYSETUP_PATH="/opt/pysetup" \
VENV_PATH="/opt/pysetup/.venv" \
POETRY_HOME="/opt/poetry" \
POETRY_VERSION=1.6.1 \
POETRY_VERSION=1.8.3 \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"
Expand Down Expand Up @@ -46,20 +46,23 @@ RUN groupadd -g $USER_GID off && \
mkdir -p /opt/open-prices && \
mkdir -p /opt/open-prices/data && \
mkdir -p /opt/open-prices/img && \
mkdir -p /opt/open-prices/static && \
chown off:off -R /opt/open-prices /home/off
COPY --chown=off:off app /opt/open-prices/app
COPY --chown=off:off alembic /opt/open-prices/alembic
COPY --chown=off:off config /opt/open-prices/config
COPY --chown=off:off open_prices /opt/open-prices/open_prices

COPY docker/docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh

COPY --chown=off:off poetry.lock pyproject.toml alembic.ini /opt/open-prices/
COPY --chown=off:off poetry.lock pyproject.toml manage.py /opt/open-prices/

USER off:off
WORKDIR /opt/open-prices
ENTRYPOINT /docker-entrypoint.sh $0 $@

CMD ["uvicorn", "app.api:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"]
RUN ["python", "manage.py", "collectstatic", "--noinput"]

CMD ["gunicorn", "config.wsgi", "--bind", "0.0.0.0:8000", "--workers", "1"]


# building dev packages
Expand Down
8 changes: 5 additions & 3 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv("SECRET_KEY")
SECRET_KEY = os.getenv("SECRET_KEY", "set-in-production")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.getenv("DEBUG") == "True"

ALLOWED_HOSTS = [x.strip() for x in os.getenv("ALLOWED_HOSTS").split(",")]
ALLOWED_HOSTS = [x.strip() for x in os.getenv("ALLOWED_HOSTS", "").split(",")]


# Application definition
Expand Down Expand Up @@ -135,7 +135,9 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_URL = "static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = "/static/"


# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
Expand Down
11 changes: 9 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ x-api-common: &api-common
- POSTGRES_PASSWORD
- POSTGRES_HOST
- POSTGRES_PORT
- CORS_ALLOW_ORIGINS
- SECRET_KEY
- DEBUG
- ALLOWED_HOSTS
- OAUTH2_SERVER_URL
- SENTRY_DNS
- LOG_LEVEL
Expand All @@ -21,6 +23,7 @@ services:
- images:/opt/open-prices/img
- data-dump:/opt/open-prices/data
- home_cache:/home/off/.cache
- ./static:/opt/open-prices/static
depends_on:
- postgres

Expand Down Expand Up @@ -55,7 +58,9 @@ services:
volumes:
# Mount the nginx configuration file
- ./nginx.conf:/etc/nginx/nginx.conf
# Mount the static files
# Mount the static files (for index & vue.js app)
- ./www:/var/www
# Mount the Django staticfiles
- ./static:/var/static
# Mount the images
- images:/var/img
Expand All @@ -76,3 +81,5 @@ volumes:
# store ~/.cache in a volume
home_cache:
name: ${COMPOSE_PROJECT_NAME:-open_prices}_home_cache
static:
name: ${COMPOSE_PROJECT_NAME:-open_prices}_static
2 changes: 1 addition & 1 deletion docker/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ services:
api:
<<: *api-base
# uvicorn in reload mode
command: ["uvicorn", "app.api:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "8000", "--reload"]
command: ["gunicorn", "config.wsgi", "--bind", "0.0.0.0:8000", "--reload"]

scheduler:
<<: *api-base
Expand Down
18 changes: 16 additions & 2 deletions nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ http {
# There is a single server block for all requests
listen 80 default_server;
listen [::]:80 default_server;
root /var/static;
root /var/www;
index index.html;

location /api/ {
Expand All @@ -53,6 +53,16 @@ http {
client_max_body_size 50M;
}

location /admin/ {
proxy_pass http://api:8000$request_uri;
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 https;
proxy_read_timeout 90;
client_max_body_size 50M;
}

location ~* \.(woff|woff2|otf|jpg|jpeg|png|webp|css|js|ttf|svg|ico)$ {
add_header Cache-Control public;
add_header Pragma public;
Expand All @@ -62,7 +72,7 @@ http {

# Make vue.js app available under /app
location /app/ {
alias /var/static/app/;
alias /var/www/app/;
try_files $uri $uri/ /app/index.html;

location ~* \.(woff|woff2|otf|jpg|jpeg|png|webp|css|js|ttf|svg|ico)$ {
Expand All @@ -73,6 +83,10 @@ http {
}
}

location /static/ {
root /var;
}

location /img {
alias /var/img;

Expand Down
Loading
Loading