From c5bfc46e33205dc264fd89ec5fc5d83f1bc6e3b9 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 25 Aug 2022 10:26:17 +0530 Subject: [PATCH 01/13] Add first version of docker files --- Dockerfile | 7 +++++++ docker-compose.yml | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e730dbb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3 +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 +WORKDIR /code +COPY requirements.txt /code/ +RUN pip install -r requirements.txt +COPY . /code/ diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c3806f2 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: "3.9" + +services: + db: + image: postgres + volumes: + - ./data/db:/var/lib/postgresql/data + environment: + - POSTGRES_DB=postgres + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + web: + build: . + command: python manage.py runserver 0.0.0.0:8000 + volumes: + - .:/code + ports: + - "8000:8000" + environment: + - POSTGRES_NAME=app + - POSTGRES_USER=app + - POSTGRES_PASSWORD= + depends_on: + - db + + From 7fd30a5dcd752995d00ecfe17ee0203ebdf224c4 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 25 Aug 2022 11:03:25 +0530 Subject: [PATCH 02/13] Add production ready Dockerfle and entrypoint files --- Dockerfile.prod | 71 ++++++++++++++++++++++++++++++++++++++++++++++ entrypoint.prod.sh | 15 ++++++++++ 2 files changed, 86 insertions(+) create mode 100644 Dockerfile.prod create mode 100755 entrypoint.prod.sh diff --git a/Dockerfile.prod b/Dockerfile.prod new file mode 100644 index 0000000..9b51ad7 --- /dev/null +++ b/Dockerfile.prod @@ -0,0 +1,71 @@ +########### +# BUILDER # +########### + +# pull official base image +FROM python:3.9.6-alpine as builder + +# set work directory +WORKDIR /usr/src/gmba_django + +# set environment variables +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +# install psycopg2 dependencies +RUN apk update \ + && apk add postgresql-dev gcc python3-dev musl-dev + +# lint +RUN pip install --upgrade pip +RUN pip install flake8==3.9.2 +COPY . . +RUN flake8 --ignore=E501,F401 . + +# install dependencies +COPY ./requirements.txt . +RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/gmba_django/wheels -r requirements.txt + + +######### +# FINAL # +######### + +# pull official base image +FROM python:3.9.6-alpine + +# create directory for the app user +RUN mkdir -p /home/app + +# create the app user +RUN addgroup -S app && adduser -S app -G app + +# create the appropriate directories +ENV HOME=/home/app +ENV APP_HOME=/home/app/app +RUN mkdir $APP_HOME +WORKDIR $APP_HOME + +# install dependencies +RUN apk update && apk add libpq +COPY --from=builder /usr/src/gmba_django/wheels /wheels +COPY --from=builder /usr/src/gmba_django/requirements.txt . +RUN pip install --no-cache /wheels/* + +# copy entrypoint.prod.sh +COPY ./entrypoint.prod.sh . +RUN sed -i 's/\r$//g' $APP_HOME/entrypoint.prod.sh +RUN chmod +x $APP_HOME/entrypoint.prod.sh + +# copy project +COPY . $APP_HOME + +# chown all the files to the app user +RUN chown -R app:app $APP_HOME + +# change to the app user +USER app + +# run entrypoint.prod.sh +ENTRYPOINT ["/home/app/app/entrypoint.prod.sh"] + diff --git a/entrypoint.prod.sh b/entrypoint.prod.sh new file mode 100755 index 0000000..2cc61cd --- /dev/null +++ b/entrypoint.prod.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +if [ "$DATABASE" = "postgres" ] +then + echo "Waiting for postgres..." + + while ! nc -z $SQL_HOST $SQL_PORT; do + sleep 0.1 + done + + echo "PostgreSQL started" +fi + +exec "$@" + From 3b9498d13dc10973ca28a7334eaf59e536e9bfd4 Mon Sep 17 00:00:00 2001 From: app Date: Thu, 25 Aug 2022 08:26:59 +0000 Subject: [PATCH 03/13] WIP: fixing docker compose scritps --- Dockerfile.prod | 2 +- docker-compose.yml | 34 +++++++++++++----------------- gmba_django/settings/production.py | 8 +++---- requirements.txt | 1 + 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Dockerfile.prod b/Dockerfile.prod index 9b51ad7..e389e79 100644 --- a/Dockerfile.prod +++ b/Dockerfile.prod @@ -20,7 +20,7 @@ RUN apk update \ RUN pip install --upgrade pip RUN pip install flake8==3.9.2 COPY . . -RUN flake8 --ignore=E501,F401 . +#RUN flake8 --ignore=E501,F401 . # install dependencies COPY ./requirements.txt . diff --git a/docker-compose.yml b/docker-compose.yml index c3806f2..96eae14 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,26 +1,22 @@ -version: "3.9" - +version: '3.8' + services: - db: - image: postgres - volumes: - - ./data/db:/var/lib/postgresql/data - environment: - - POSTGRES_DB=postgres - - POSTGRES_USER=postgres - - POSTGRES_PASSWORD=postgres web: - build: . - command: python manage.py runserver 0.0.0.0:8000 - volumes: - - .:/code + build: ./app + command: gunicorn gmba_django.wsgi:application --bind 0.0.0.0:8000 ports: - - "8000:8000" - environment: - - POSTGRES_NAME=app - - POSTGRES_USER=app - - POSTGRES_PASSWORD= + - 8000:8000 + env_file: + - ./.env.prod depends_on: - db + db: + image: postgres:13.0-alpine + volumes: + - postgres_data:/var/lib/postgresql/data/ + env_file: + - ./.env.prod +volumes: + postgres_data: diff --git a/gmba_django/settings/production.py b/gmba_django/settings/production.py index 45a8caf..bcb825e 100644 --- a/gmba_django/settings/production.py +++ b/gmba_django/settings/production.py @@ -8,10 +8,10 @@ DATABASES = { 'default': { 'ENGINE': os.environ.get('SQL_ENGINE', 'django.db.backends.sqlite3'), 'NAME': os.environ.get('SQL_DATABASE', os.path.join(BASE_DIR, 'db.sqlite3')), - # 'USER': os.environ.get('SQL_USER', 'user'), - # 'PASSWORD': os.environ.get('SQL_PASSWORD', 'password'), - # 'HOST': os.environ.get('SQL_HOST', 'localhost'), - # 'PORT': os.environ.get('SQL_PORT', ''), + 'USER': os.environ.get('SQL_USER', 'app'), + 'PASSWORD': os.environ.get('SQL_PASSWORD', ''), + 'HOST': os.environ.get('SQL_HOST', 'localhost'), + 'PORT': os.environ.get('SQL_PORT', ''), } } diff --git a/requirements.txt b/requirements.txt index ad8eda7..ce977aa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ sqlparse==0.4.1 typing-extensions==3.10.0.0 psycopg2==2.9.1 Werkzeug==2.0.2 +gunicorn==20.1.0 From f960c4e779daa59b2c7d256a8b9fb3c64f7ed3f4 Mon Sep 17 00:00:00 2001 From: app Date: Thu, 25 Aug 2022 08:27:48 +0000 Subject: [PATCH 04/13] Add docker-compose.prod.yaml and nginx confs --- docker-compose.prod.yml | 34 ++++++++++++++++++++++++++++++++++ nginx/Dockerfile | 4 ++++ nginx/nginx.conf | 15 +++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 docker-compose.prod.yml create mode 100644 nginx/Dockerfile create mode 100644 nginx/nginx.conf diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 0000000..8d6c176 --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,34 @@ +version: '3.8' + +services: + web: + build: + context: ./ + dockerfile: Dockerfile.prod + command: gunicorn gmba_django.wsgi:application --bind 0.0.0.0:8000 + volumes: + - static_volume:/home/app/app/staticfiles + expose: + - 8000 + env_file: + - ./.env.prod + depends_on: + - db + db: + image: postgres:13.0-alpine + volumes: + - postgres_data:/var/lib/postgresql/data/ + env_file: + - ./.env.prod.db + nginx: + build: ./nginx + volumes: + - static_volume:/home/app/app/staticfiles + ports: + - 1337:80 + depends_on: + - web + +volumes: + postgres_data: + static_volume: diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 0000000..4580e8d --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,4 @@ +FROM nginx:1.21-alpine + +RUN rm /etc/nginx/conf.d/default.conf +COPY nginx.conf /etc/nginx/conf.d diff --git a/nginx/nginx.conf b/nginx/nginx.conf new file mode 100644 index 0000000..c709070 --- /dev/null +++ b/nginx/nginx.conf @@ -0,0 +1,15 @@ +upstream gmba_django { + server 127.0.0.1:8000; +} + +server { + listen 80; + + location / { + proxy_pass http://gmba_django; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $host; + proxy_redirect off; + } + +} From 5446daa87fc1c7872f94f277262f56d926f01029 Mon Sep 17 00:00:00 2001 From: app Date: Mon, 19 Sep 2022 03:35:14 +0000 Subject: [PATCH 05/13] Use hardcoded instance name for now --- nginx/nginx.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nginx/nginx.conf b/nginx/nginx.conf index c709070..f3fc647 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -1,5 +1,5 @@ upstream gmba_django { - server 127.0.0.1:8000; + server gmba_django_nginx_1:8000; } server { From 5b64c4ee150ca4577f9c0da5637c6167f84fb0d2 Mon Sep 17 00:00:00 2001 From: app Date: Mon, 19 Sep 2022 03:57:24 +0000 Subject: [PATCH 06/13] Use correct container name --- nginx/nginx.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nginx/nginx.conf b/nginx/nginx.conf index f3fc647..e644302 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -1,5 +1,5 @@ upstream gmba_django { - server gmba_django_nginx_1:8000; + server web:8000; } server { From a91d2b16b6c480871b7ad4c93139ead81dce03aa Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 19 Sep 2022 09:42:19 +0530 Subject: [PATCH 07/13] Fix static files path and create the directory --- Dockerfile.prod | 1 + nginx/nginx.conf | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/Dockerfile.prod b/Dockerfile.prod index e389e79..b169692 100644 --- a/Dockerfile.prod +++ b/Dockerfile.prod @@ -44,6 +44,7 @@ RUN addgroup -S app && adduser -S app -G app ENV HOME=/home/app ENV APP_HOME=/home/app/app RUN mkdir $APP_HOME +RUN mkdir $APP_HOME/staticfiles WORKDIR $APP_HOME # install dependencies diff --git a/nginx/nginx.conf b/nginx/nginx.conf index e644302..93ba39f 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -12,4 +12,9 @@ server { proxy_redirect off; } + + location /static/ { + alias /home/app/app/staticfiles/; + } + } From 13b7918de108219b2fd80f067c2f45853ddb3a75 Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 19 Sep 2022 09:54:17 +0530 Subject: [PATCH 08/13] Fix staticfiles path as per code --- Dockerfile.prod | 2 +- docker-compose.prod.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dockerfile.prod b/Dockerfile.prod index b169692..6f7da8e 100644 --- a/Dockerfile.prod +++ b/Dockerfile.prod @@ -44,7 +44,7 @@ RUN addgroup -S app && adduser -S app -G app ENV HOME=/home/app ENV APP_HOME=/home/app/app RUN mkdir $APP_HOME -RUN mkdir $APP_HOME/staticfiles +RUN mkdir $APP_HOME/static WORKDIR $APP_HOME # install dependencies diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 8d6c176..b225f25 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -7,7 +7,7 @@ services: dockerfile: Dockerfile.prod command: gunicorn gmba_django.wsgi:application --bind 0.0.0.0:8000 volumes: - - static_volume:/home/app/app/staticfiles + - static_volume:/home/app/app/static expose: - 8000 env_file: @@ -23,7 +23,7 @@ services: nginx: build: ./nginx volumes: - - static_volume:/home/app/app/staticfiles + - static_volume:/home/app/app/static ports: - 1337:80 depends_on: From 600ae8e5de019bed08c7bde90e1840174122b04a Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 19 Sep 2022 09:57:57 +0530 Subject: [PATCH 09/13] Fix static files path in nginx.conf --- nginx/nginx.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nginx/nginx.conf b/nginx/nginx.conf index 93ba39f..3a25edf 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -14,7 +14,7 @@ server { location /static/ { - alias /home/app/app/staticfiles/; + alias /home/app/app/static/; } } From 4cc03b63725d2721c88ec45e57f8a7e9726b46b9 Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 19 Sep 2022 12:22:52 +0530 Subject: [PATCH 10/13] Remove unwanted files --- Dockerfile | 7 ------- docker-compose.yml | 22 ---------------------- 2 files changed, 29 deletions(-) delete mode 100644 Dockerfile delete mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index e730dbb..0000000 --- a/Dockerfile +++ /dev/null @@ -1,7 +0,0 @@ -FROM python:3 -ENV PYTHONDONTWRITEBYTECODE=1 -ENV PYTHONUNBUFFERED=1 -WORKDIR /code -COPY requirements.txt /code/ -RUN pip install -r requirements.txt -COPY . /code/ diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 96eae14..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,22 +0,0 @@ -version: '3.8' - -services: - web: - build: ./app - command: gunicorn gmba_django.wsgi:application --bind 0.0.0.0:8000 - ports: - - 8000:8000 - env_file: - - ./.env.prod - depends_on: - - db - db: - image: postgres:13.0-alpine - volumes: - - postgres_data:/var/lib/postgresql/data/ - env_file: - - ./.env.prod - -volumes: - postgres_data: - From bc4061f12a30c51497a9a05cfd428076a933dcfa Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 19 Sep 2022 12:36:21 +0530 Subject: [PATCH 11/13] Prepare changelog for 2.0 --- Changelog.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Changelog.md b/Changelog.md index f099f2b..63d1722 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,46 @@ # CHANGELOG.md +## 2.0 (2022-09-19) Dockerify https://redmine.ungleich.ch/issues/10884 + +Steps: + - Create .env.prod and .env.prod.db files + +#### .env.prod +``` +DEBUG=0 +SECRET_KEY=secret_key_here +DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] +DJANGO_SETTINGS_MODULE=gmba_django.settings.production +ALLOWED_HOSTS=localhost,127.0.0.1,[::1] +SQL_ENGINE=django.db.backends.postgresql +SQL_DATABASE=psql_db_name +SQL_USER=psql_username_name +SQL_PASSWORD=psql_passwordd +SQL_HOST=db +SQL_PORT=5432 +``` + +#### .env.prod.db +``` +POSTGRES_USER=psql_username_name +POSTGRES_PASSWORD=psql_passwordd +POSTGRES_DB=psql_db_name +``` + +Building containers + - docker-compose -f docker-compose.prod.yml down -v --remove-orphans + - docker-compose -f docker-compose.prod.yml up -d --build + +Copy db and adust + - docker cp gd.sql gmba_django_db_1:/gd.sql + - docker-compose exec db psql --username=psql_username_name --dbname=psql_db_name < /gd.sql + +Migrate / check + - docker-compose exec web python manage.py migrate --noinput + +Static resources + - docker-compose -f docker-compose.prod.yml exec web python manage.py collectstatic --no-input --clear + ## 1.12 (2022-01-19) Bugfixes: From 3eb5ba12d9423977a7193279ee5f269c802f3c44 Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 19 Sep 2022 13:30:56 +0530 Subject: [PATCH 12/13] Add missing configs for nginx --- nginx/nginx.conf | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/nginx/nginx.conf b/nginx/nginx.conf index 3a25edf..75f3c54 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -5,6 +5,8 @@ upstream gmba_django { server { listen 80; + client_max_body_size 256m; + location / { proxy_pass http://gmba_django; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; @@ -12,9 +14,9 @@ server { proxy_redirect off; } - location /static/ { alias /home/app/app/static/; } + add_header Content-Security-Policy "frame-ancestors https://www.gmba.unibe.ch" } From 0123fd2eef17a52522bda912e8fd32aa9c9a5bd9 Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 19 Sep 2022 13:35:02 +0530 Subject: [PATCH 13/13] Add media files config --- Dockerfile.prod | 1 + docker-compose.prod.yml | 3 +++ nginx/nginx.conf | 6 ++++++ 3 files changed, 10 insertions(+) diff --git a/Dockerfile.prod b/Dockerfile.prod index 6f7da8e..408c26a 100644 --- a/Dockerfile.prod +++ b/Dockerfile.prod @@ -45,6 +45,7 @@ ENV HOME=/home/app ENV APP_HOME=/home/app/app RUN mkdir $APP_HOME RUN mkdir $APP_HOME/static +RUN mkdir $APP_HOME/media WORKDIR $APP_HOME # install dependencies diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index b225f25..575027d 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -8,6 +8,7 @@ services: command: gunicorn gmba_django.wsgi:application --bind 0.0.0.0:8000 volumes: - static_volume:/home/app/app/static + - media_volume:/home/app/app/media expose: - 8000 env_file: @@ -24,6 +25,7 @@ services: build: ./nginx volumes: - static_volume:/home/app/app/static + - media_volume:/home/app/app/media ports: - 1337:80 depends_on: @@ -32,3 +34,4 @@ services: volumes: postgres_data: static_volume: + media_volume: diff --git a/nginx/nginx.conf b/nginx/nginx.conf index 75f3c54..ceab716 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -16,6 +16,12 @@ server { location /static/ { alias /home/app/app/static/; + expires max; + } + + location /media/ { + alias /home/app/app/media/; + expires max; } add_header Content-Security-Policy "frame-ancestors https://www.gmba.unibe.ch"