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

Improving docker #52

Merged
merged 5 commits into from
Nov 3, 2020
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
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,35 @@ There are some changes.

This project uses docker and docker-compose.

To build the containers with the right dependencies and also install the pip requirements, use:
To build the containers with the right dependencies, install the node dependencies (for the style of the templates) and the pip requirements (for the django applications), use:

$ docker-compose -f local.yml build

Once this has been done, you can start the containers/services using the following command.

$ docker-compose -f local.yml up -d

If you remove the ``-d``, you will see the logs on screen, but the containers will run only while your terminal is open.
In this version, the Django process is not run automatically, instead you have to start the server yourself.
This will take care of starting the database container and the webserver. You'll be able to load the website on your browser by visiting [localhost:8000](http://localhost:8000)

The following command will start the django server at port 8000 in the container and mirror it to your host machine.
If you remove the ``-d``, you will see the logs on screen, but the containers will run only while your terminal is open.

$ docker-compose -f local.yml run --publish 127.0.0.1:8000:8000 django python manage.py runserver 0.0.0.0:8000

To stop the containers you can use:

$ docker-compose -f local.yml down

You can run the other manage commands with:

$ docker-compose -f local.yml run django python manage.py command_name
$ docker-compose -f local.yml run --rm django python3 manage.py command_name

For the frontend, we use webpack.
To now run webpack you can use the following command in the src dir:

$ yarn webpack
If you are running a container, webpack can be run with:

$ docker-compose -f local.yml run --rm django /node_modules/.bin/webpack --mode=production

You can change the ``--mode`` to ``--mode=development``.

You can also watch for design changes using:

$ docker-compose -f local.yml run --rm django /node_modules/.bin/webpack --mode=development --watch

1 change: 1 addition & 0 deletions compose/local/django/.yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--modules-folder ../node_modules
50 changes: 37 additions & 13 deletions compose/local/django/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
FROM python:3.7-alpine
FROM ubuntu:18.04

ENV PYTHONUNBUFFERED 1

RUN apk update \
# psycopg2 dependencies
&& apk add --virtual build-deps gcc python3-dev musl-dev \
&& apk add postgresql-dev \
ENV TZ=Europe/Amsterdam
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update \
&& apt-get -y install wget gnupg python3 python3-pip python3-dev

RUN python3 -m pip install --upgrade pip

RUN sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt bionic-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -


RUN apt-get update \
&& apt-get install -y gcc libpq-dev \
# Pillow dependencies
&& apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
# CFFI dependencies
&& apk add libffi-dev py-cffi \
libjpeg-dev zlib1g \
# Translations dependencies
&& apk add gettext \
# https://docs.djangoproject.com/en/dev/ref/django-admin/#dbshell
&& apk add postgresql-client \
gettext \
# Postgres
postgresql-12 postgresql-client-12 \
# Git for cloning dependencies
&& apk add git
git


RUN apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_current.x | bash -
RUN apt-get install -y nodejs

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -

RUN sh -c 'echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list'

RUN apt-get update && \
apt-get -y install yarn

# Requirements are installed here to ensure they will be cached.
COPY ./requirements /requirements
Expand All @@ -29,8 +49,12 @@ COPY ./compose/local/django/start /start
RUN sed -i 's/\r$//g' /start
RUN chmod +x /start

COPY ./yarn.lock /yarn.lock
COPY ./package.json /package.json
RUN yarn install

WORKDIR /app

EXPOSE 8000

ENTRYPOINT ["/entrypoint"]

7 changes: 4 additions & 3 deletions compose/local/django/start
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/bin/sh

set -o errexit
set -o pipefail
# set -o pipefail
set -o nounset

/node_modules/.bin/webpack --mode=production

#python manage.py migrate
#python manage.py runserver_plus 0.0.0.0:8000
python3 manage.py migrate
python3 manage.py runserver_plus 0.0.0.0:8000

4 changes: 2 additions & 2 deletions compose/production/django/entrypoint
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

set -o errexit
set -o pipefail
# set -o pipefail
set -o nounset


Expand All @@ -17,7 +17,7 @@ fi
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"

postgres_ready() {
python << END
python3<< END
import sys

import psycopg2
Expand Down
2 changes: 1 addition & 1 deletion compose/production/postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM postgres:11.3
FROM postgres:12.4

COPY ./compose/production/postgres/maintenance /usr/local/bin/maintenance
RUN chmod +x /usr/local/bin/maintenance/*
Expand Down
55 changes: 55 additions & 0 deletions compose/production/postgres/maintenance/restore_heroku
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash


### Restore database from a backup.
###
### Parameters:
### <1> filename of an existing backup.
###
### Usage:
### $ docker-compose -f <environment>.yml (exec |run --rm) postgres restore <1>


set -o errexit
set -o pipefail
set -o nounset


working_dir="$(dirname ${0})"
source "${working_dir}/_sourced/constants.sh"
source "${working_dir}/_sourced/messages.sh"


if [[ -z ${1+x} ]]; then
message_error "Backup filename is not specified yet it is a required parameter. Make sure you provide one and try again."
exit 1
fi
backup_filename="${BACKUP_DIR_PATH}/${1}"
if [[ ! -f "${backup_filename}" ]]; then
message_error "No backup with the specified filename found. Check out the 'backups' maintenance script output to see if there is one and try again."
exit 1
fi

message_welcome "Restoring the '${POSTGRES_DB}' database from the '${backup_filename}' backup..."

if [[ "${POSTGRES_USER}" == "postgres" ]]; then
message_error "Restoring as 'postgres' user is not supported. Assign 'POSTGRES_USER' env with another one and try again."
exit 1
fi

export PGHOST="${POSTGRES_HOST}"
export PGPORT="${POSTGRES_PORT}"
export PGUSER="${POSTGRES_USER}"
export PGPASSWORD="${POSTGRES_PASSWORD}"
export PGDATABASE="${POSTGRES_DB}"

message_info "Dropping the database..."
dropdb "${PGDATABASE}"

message_info "Creating a new database..."
createdb --owner="${POSTGRES_USER}"

message_info "Applying the backup to the new database..."
pg_restore -d "${POSTGRES_DB}" ${backup_filename}

message_success "The '${POSTGRES_DB}' database has been restored from the '${backup_filename}' backup."
7 changes: 6 additions & 1 deletion klimaat_helpdesk/core/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django import forms
from django.db import ProgrammingError
from django.utils.translation import gettext_lazy as _

from klimaat_helpdesk.cms.models import AnswerCategory
Expand All @@ -13,8 +14,12 @@ class ClimateQuestionForm(forms.Form):
Form used when users ask a new question. Fields are combined into
one field for the GitLab integration.
"""
try:
choices = [(c.name, c.name) for c in AnswerCategory.objects.all()]
except ProgrammingError as e:
choices = ['general',]
categories = forms.MultipleChoiceField(widget=TagWidget,
choices=[(c.name, c.name) for c in AnswerCategory.objects.all()], required=False)
choices=choices, required=False)
main_question = forms.CharField(max_length=1000, required=True, label='Mijn vraag is*')
relevant_location = forms.CharField(max_length=1000, required=False, label='Locatie (bijvoorbeeld Amsterdam of Europa)')
relevant_timespan = forms.CharField(max_length=1000, required=False, label='Tijdperk (bijvoorbeeld de komende 10 jaar)')
Expand Down
2 changes: 0 additions & 2 deletions local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ services:
ports:
- "8000:8000"
command: /start
expose:
- "8000"

postgres:
build:
Expand Down