From c9b54cd4c5e6328273e33bdb3208fe907c6c0e3c Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Mon, 10 Jan 2022 13:52:40 +0100 Subject: [PATCH 1/3] # Docker * adds docker related configurations * directly working configuration provided --- Dockerfile | 17 ++++++++ docker-compose.yml | 55 ++++++++++++++++++++++++++ konova/celery.py | 2 +- konova/sub_settings/django_settings.py | 9 +++-- 4 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..e97a087b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM python:3.7-slim +ENV PYTHONUNBUFFERED 1 + +WORKDIR /konova + +# Install some dependencies +RUN apt update +RUN apt install -y gdal-bin + +# Copy requirements file into workspace +COPY ./requirements.txt /konova/ +RUN pip install --upgrade pip +RUN pip install -r requirements.txt + +# Copy rest of project into workspace +COPY . /konova/ + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..5bddc633 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,55 @@ +version: '3.3' + +services: + redis: + image: redis + volumes: + - /redis/data:/bitnami/redis/data + environment: + - REDIS_PASSWORD=CHANGE_ME + + konova: + external_links: + - postgis:db + build: . + container_name: "konova-docker" + command: python manage.py runserver 0.0.0.0:8000 + volumes: + - .:/konova + ports: + - "8001:8000" + depends_on: + - redis + environment: + - POSTGRES_NAME=konova + - POSTGRES_PORT=5432 + - POSTGRES_PASSWORD=michel + - POSTGRES_USER=konova + - POSTGRES_HOST=db + - REDIS_HOST=redis + +# To provide a celery worker instance, we need to add the celery worker as an own service + celery_worker: + external_links: + - postgis:db + build: . + container_name: "konova-worker-docker" + command: celery -A konova worker -l INFO + volumes: + - .:/konova + depends_on: + - konova + environment: + - POSTGRES_NAME=konova + - POSTGRES_PORT=5432 + - POSTGRES_PASSWORD=michel + - POSTGRES_USER=konova + - POSTGRES_HOST=db + - REDIS_HOST=redis + +# Instead of an own, new network, we need to connect to the existing one, which is provided by the postgis container +# NOTE: THIS NETWORK MUST EXIST +networks: + default: + external: + name: postgis_nat_it_backend \ No newline at end of file diff --git a/konova/celery.py b/konova/celery.py index 478f2844..ab06cf7c 100644 --- a/konova/celery.py +++ b/konova/celery.py @@ -17,7 +17,7 @@ app.config_from_object('django.conf:settings', namespace='CELERY') app.autodiscover_tasks() # Declare redis as broker -app.conf.broker_url = 'redis://localhost:6379/0' +app.conf.broker_url = f"redis://{os.environ.get('REDIS_HOST')}:6379/0" @app.task(bind=True) diff --git a/konova/sub_settings/django_settings.py b/konova/sub_settings/django_settings.py index df875a81..951fd468 100644 --- a/konova/sub_settings/django_settings.py +++ b/konova/sub_settings/django_settings.py @@ -119,10 +119,11 @@ WSGI_APPLICATION = 'konova.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'django.contrib.gis.db.backends.postgis', - 'NAME': 'konova', - 'USER': 'postgres', - 'HOST': '127.0.0.1', - 'PORT': '5432', + 'NAME': os.environ.get('POSTGRES_NAME'), + 'USER': os.environ.get('POSTGRES_USER'), + 'HOST': os.environ.get('POSTGRES_HOST'), + 'PASSWORD': os.environ.get('POSTGRES_PASSWORD'), + 'PORT': os.environ.get('POSTGRES_PORT'), } } From 7075d7074c93efb14dfac3a2d21fc54f46341ea4 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Mon, 10 Jan 2022 15:27:19 +0100 Subject: [PATCH 2/3] # Docker Production * adds further settings to create a production-ready docker configuration --- Dockerfile | 3 +++ docker-compose.yml | 23 +++++++++++++++++++---- konova/sub_settings/django_settings.py | 2 +- nginx/Dockerfile | 4 ++++ nginx/nginx.conf | 20 ++++++++++++++++++++ requirements.txt | 1 + 6 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 nginx/Dockerfile create mode 100644 nginx/nginx.conf diff --git a/Dockerfile b/Dockerfile index e97a087b..aad14222 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,3 +15,6 @@ RUN pip install -r requirements.txt # Copy rest of project into workspace COPY . /konova/ +# Move static files in designated folder +RUN python manage.py collectstatic --noinput + diff --git a/docker-compose.yml b/docker-compose.yml index 5bddc633..9569bd7a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,7 @@ version: '3.3' services: redis: image: redis + container_name: "konova-redis-cache" volumes: - /redis/data:/bitnami/redis/data environment: @@ -13,11 +14,12 @@ services: - postgis:db build: . container_name: "konova-docker" - command: python manage.py runserver 0.0.0.0:8000 + command: gunicorn konova.wsgi:application --bind 0.0.0.0:8000 volumes: - .:/konova - ports: - - "8001:8000" + - static_file_volume:/konova/static # Point to the volume for static files. Shared with nginx service + expose: + - 8000 depends_on: - redis environment: @@ -47,9 +49,22 @@ services: - POSTGRES_HOST=db - REDIS_HOST=redis + nginx: + build: ./nginx + container_name: "konova-nginx-server" + ports: + - "1337:80" + depends_on: + - konova + volumes: + - static_file_volume:/konova/static # Point to the volume for static files. Shared with konova service + # Instead of an own, new network, we need to connect to the existing one, which is provided by the postgis container # NOTE: THIS NETWORK MUST EXIST networks: default: external: - name: postgis_nat_it_backend \ No newline at end of file + name: postgis_nat_it_backend + +volumes: + static_file_volume: \ No newline at end of file diff --git a/konova/sub_settings/django_settings.py b/konova/sub_settings/django_settings.py index 951fd468..e94d4baf 100644 --- a/konova/sub_settings/django_settings.py +++ b/konova/sub_settings/django_settings.py @@ -30,7 +30,7 @@ BASE_DIR = os.path.dirname( SECRET_KEY = '5=9-)2)h$u9=!zrhia9=lj-2#cpcb8=#$7y+)l$5tto$3q(n_+' # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = False ALLOWED_HOSTS = [ "127.0.0.1", diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 00000000..4c49d2ee --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,4 @@ +FROM nginx:alpine + +RUN rm /etc/nginx/conf.d/default.conf +COPY nginx.conf /etc/nginx/conf.d \ No newline at end of file diff --git a/nginx/nginx.conf b/nginx/nginx.conf new file mode 100644 index 00000000..26b326ca --- /dev/null +++ b/nginx/nginx.conf @@ -0,0 +1,20 @@ +upstream konova { + server konova:8000; +} + +server { + + listen 80; + + location / { + proxy_pass http://konova; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $host; + proxy_redirect off; + } + + location /static/ { + alias /konova/static/; + } + +} diff --git a/requirements.txt b/requirements.txt index 763913c4..20e0bff8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -48,3 +48,4 @@ webservices==0.7 wrapt==1.13.3 xmltodict==0.12.0 zipp==3.4.1 +gunicorn==20.1.0 \ No newline at end of file From 2fdf6606b33d2d6c8d038329e76b9bc3669f819b Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Tue, 11 Jan 2022 08:32:04 +0100 Subject: [PATCH 3/3] # Docker * minor adjustments --- docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 9569bd7a..82d8b60f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,7 +25,7 @@ services: environment: - POSTGRES_NAME=konova - POSTGRES_PORT=5432 - - POSTGRES_PASSWORD=michel + - POSTGRES_PASSWORD=CHANGE_ME - POSTGRES_USER=konova - POSTGRES_HOST=db - REDIS_HOST=redis @@ -44,7 +44,7 @@ services: environment: - POSTGRES_NAME=konova - POSTGRES_PORT=5432 - - POSTGRES_PASSWORD=michel + - POSTGRES_PASSWORD=CHANGE_ME - POSTGRES_USER=konova - POSTGRES_HOST=db - REDIS_HOST=redis