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

Configure docker for development #465

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions .github/workflows/test-and-deploy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Test than deploy

on:
on:
push:
branches: [master]
pull_request:
Expand All @@ -24,7 +24,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements.txt -r requirements-test.txt
- name: Install JS/CSS dependencies
run: npm install
- name: Build JS/CSS with webpack
Expand All @@ -51,7 +51,7 @@ jobs:
fail_ci_if_error: true
verbose: true
if: matrix.python-version == 3.8

deploy:
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
Expand Down
87 changes: 54 additions & 33 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,39 +1,60 @@
FROM node:current-alpine AS build_frontend
FROM node:16-alpine3.13 AS build-frontend
WORKDIR /app

COPY package.json package-lock.json ./
RUN npm ci
COPY webpack.config.js .
COPY frontend ./frontend
RUN npm run build
COPY package.json \
package-lock.json \
./
RUN npm clean-install
MrQubo marked this conversation as resolved.
Show resolved Hide resolved

COPY webpack.config.js .
COPY frontend ./frontend
RUN npm run build


FROM python:alpine
FROM python:3.9-alpine3.13 AS runner
WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev libffi-dev zlib-dev jpeg-dev

RUN pip install --upgrade pip
COPY requirements.txt .
RUN pip install -r requirements.txt
RUN pip install gunicorn

COPY . .
COPY --from=build_frontend /app/static/dist static/dist

RUN echo "import os" > wwwapp/local_settings.py
RUN echo "SECRET_KEY = os.environ['SECRET_KEY']" >> wwwapp/local_settings.py
RUN echo "ALLOWED_HOSTS = ['*']" >> wwwapp/local_settings.py
RUN echo "DATABASES = {'default': {'ENGINE': 'django.db.backends.postgresql_psycopg2', 'HOST': 'db', 'NAME': 'aplikacjawww', 'USER': 'app', 'PASSWORD': 'app'}}" >> wwwapp/local_settings.py
RUN echo "GOOGLE_ANALYTICS_KEY = None" >> wwwapp/local_settings.py
RUN echo "MEDIA_ROOT = os.environ['MEDIA_ROOT']" >> wwwapp/local_settings.py
RUN echo "SENDFILE_ROOT = os.environ['SENDFILE_ROOT']" >> wwwapp/local_settings.py
RUN echo "USE_X_FORWARDED_HOST = True" >> wwwapp/local_settings.py
RUN echo "SESSION_COOKIE_SECURE = False" >> wwwapp/local_settings.py
RUN echo "CSRF_COOKIE_SECURE = False" >> wwwapp/local_settings.py

CMD gunicorn wwwapp.wsgi:application --bind 0.0.0.0:8000
ENTRYPOINT ["./entrypoint.sh"]


RUN apk update \
&& apk add \
cargo \
gcc \
jpeg-dev \
libffi-dev \
musl-dev \
openssl-dev \
postgresql-dev \
python3-dev \
zlib-dev \
;

ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1

RUN pip install --upgrade pip setuptools
COPY requirements.txt .
RUN pip install gunicorn -r requirements.txt

COPY --from=build-frontend \
/app/static/dist \
static/dist
COPY . .

RUN echo "import os" >>wwwapp/local_settings.py \
&& echo "DEBUG = True" >>wwwapp/local_settings.py \
&& echo "SECRET_KEY = os.environ['SECRET_KEY']" >>wwwapp/local_settings.py \
&& echo "ALLOWED_HOSTS = ['*']" >>wwwapp/local_settings.py \
&& echo "class Anything: __contains__ = lambda *args: True" >>wwwapp/local_settings.py \
&& echo "INTERNAL_IPS = Anything()" >>wwwapp/local_settings.py \
&& echo "DATABASES = {'default': {'ENGINE': 'django.db.backends.postgresql_psycopg2', 'HOST': 'db', 'NAME': 'aplikacjawww', 'USER': 'app', 'PASSWORD': 'app'}}" >>wwwapp/local_settings.py \
&& echo "GOOGLE_ANALYTICS_KEY = None" >>wwwapp/local_settings.py \
&& echo "MEDIA_ROOT = os.environ['MEDIA_ROOT']" >>wwwapp/local_settings.py \
&& echo "SENDFILE_ROOT = os.environ['SENDFILE_ROOT']" >>wwwapp/local_settings.py \
&& echo "USE_X_FORWARDED_HOST = True" >>wwwapp/local_settings.py \
&& echo "SESSION_COOKIE_SECURE = False" >>wwwapp/local_settings.py \
&& echo "CSRF_COOKIE_SECURE = False" >>wwwapp/local_settings.py \
;


EXPOSE 8000
CMD ./entrypoint.sh
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Django-based application to manage registration of people for [scientific summer
- `./manage.py populate_with_test_data` - script to populate the database with data for development

### Run:
- install postgresql
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only in production deployments - for development, sqlite is used instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me pip install -r requirements.txt failed because of missing pg_config executable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A @krzys-h has said, postgress should not be required for development. requirements.txt file should be adjusted if it is there

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create a requirements.txt file for production

- activate virtualenv (if not yet activated)
- `pip install -r requirements.txt`
- `./manage.py runserver`
Expand Down
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
version: '3.7'

services:

django:
build: .
volumes:
- static_volume:/usr/src/static
- media_volume:/usr/src/media
- uploads_volume:/usr/src/uploads
expose:
- 8000
environment:
- DJANGO_SETTINGS_MODULE=wwwapp.settings_prod
- MEDIA_ROOT=/usr/src/media
Expand All @@ -20,6 +19,7 @@ services:
- SOCIAL_AUTH_FACEBOOK_SECRET=${SOCIAL_AUTH_FACEBOOK_SECRET}
depends_on:
- db

db:
image: postgres:alpine
volumes:
Expand All @@ -28,6 +28,7 @@ services:
- POSTGRES_DB=aplikacjawww
- POSTGRES_USER=app
- POSTGRES_PASSWORD=app

nginx:
image: nginx:alpine
volumes:
Expand Down
26 changes: 19 additions & 7 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
#!/bin/sh

echo "Collecting static files"
set -o pipefail -o nounset


echo >&2 "Collecting static files."
rm -rf /usr/src/static/*
python manage.py collectstatic
python3 manage.py collectstatic

echo "Waiting for postgres..."
echo >&2 "Waiting for postgres..."
while ! nc -z db 5432; do
sleep 0.1
sleep 0.1
done
echo "PostgreSQL started"
python manage.py migrate
echo >&2 "PostgreSQL started."

echo >&2 "Migrating database."
python3 manage.py migrate

echo >&2 "Creating admin user (username: admin, password: admin)."
export [email protected]
export DJANGO_SUPERUSER_PASSWORD=admin \
export DJANGO_SUPERUSER_USERNAME=admin
# This will fail if admin user already exists.
python3 manage.py createsuperuser --noinput || true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is problematic. What if I remove/rename the initial user after creating my own account?

I know this is only for development Docker, but...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What problem do you have in mind? Can you elaborate?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It becomes impossible to remove/change username of the default user, as it will get recreated when the container restarts. If you really want automatic user creation, it should only occur once, when the database is first created.

But personally I don't think this is necessary at all... If you want a development setup, call populate_with_test_data after starting the container which creates the admin account too iirc, if you want a production setup, don't use admin as the password...


exec "$@"
exec gunicorn wwwapp.wsgi:application --bind 0.0.0.0:8000
6 changes: 6 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coverage==5.5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably need to update CI so it properly installs this requirements for running tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He already did - the tests fail for a different reason, the same fail message is in the other PR which doesn't touch requirements

django-coverage-plugin==1.8.0
django-stubs==1.7.0
freezegun==1.1.0
mock==4.0.3
mypy==0.812
26 changes: 9 additions & 17 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
Django==3.1.8
django-crispy-forms==1.11.1
django-tinymce==3.2.0
django-admin-sortable2==0.7.8
django-bleach==0.6.1
django-cleanup==5.1.0
django-sendfile2==0.6.0
django-crispy-forms==1.11.1
django-debug-toolbar==3.2
django-select2==7.6.2
social-auth-app-django==4.0.0
django-admin-sortable2==0.7.8
django-imagekit==4.0.2
Pillow==8.1.2
python-dateutil==2.8.1
django-select2==7.6.2
django-sendfile2==0.6.0
django-tinymce==3.2.0
Django==3.1.8
Faker==6.6.2
Pillow==8.1.2
psycopg2==2.8.6

mock==4.0.3
freezegun==1.1.0
coverage==5.5
django-coverage-plugin==1.8.0

mypy==0.812
django-stubs==1.7.0
python-dateutil==2.8.1
social-auth-app-django==4.0.0