From 96fc8a55693be11d060bb419d6d56256c1e88b04 Mon Sep 17 00:00:00 2001 From: Aarni Halinen Date: Sun, 3 Nov 2019 16:06:14 +0200 Subject: [PATCH] Update development docker setup --- .env.sample | 2 + Dockerfile | 25 +++++-- Dockerfile.prod | 20 ++--- docker-compose.yml | 11 ++- setup.sh | 3 +- sikweb/base.py | 37 ---------- wait-for-it.sh | 177 --------------------------------------------- 7 files changed, 39 insertions(+), 236 deletions(-) delete mode 100755 wait-for-it.sh diff --git a/.env.sample b/.env.sample index 5be805f..65ab9dd 100644 --- a/.env.sample +++ b/.env.sample @@ -1,8 +1,10 @@ HOST=localhost +DEBUG=True SECRET_KEY=7p$85^4ibb^p4-=vs44b7!y0e-zemugze18@a#30&71=a8)dp( TG_BOT_TOKEN= EMAIL_HOST= EMAIL_PASSWD= DB_USER=postgres DB_PASSWD=postgres +DB_HOST=db DB_PORT=5432 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 3123a63..1473924 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,19 @@ -FROM python:3.7 +FROM python:3.7-alpine ENV PYTHONUNBUFFERED 1 -ENV IS_DOCKER 1 -RUN mkdir /code -WORKDIR /code -ADD requirements.txt /code/ -RUN env -ADD . /code/ +ENV IS_DOCKER 0 +WORKDIR /app +COPY requirements.txt ./ +COPY requirements.production.txt ./ +COPY . ./ + +# uWSGI, gunicorn etc. +RUN apk add --no-cache python3-dev build-base linux-headers pcre-dev openssl bash \ + # PSQL + && apk add --no-cache postgresql-dev \ + # Pillow + && apk add --no-cache jpeg-dev zlib-dev \ + && pip install --upgrade pip \ + && pip install -r requirements.txt \ + && pip install -r requirements.production.txt + +RUN python manage.py collectstatic --noinput diff --git a/Dockerfile.prod b/Dockerfile.prod index d8299d5..299a4ce 100644 --- a/Dockerfile.prod +++ b/Dockerfile.prod @@ -7,19 +7,15 @@ COPY requirements.production.txt ./ COPY . ./ # uWSGI, gunicorn etc. -RUN apk add --no-cache --virtual .build-deps python3-dev build-base linux-headers pcre-dev openssl \ - # PSQL - && apk add --no-cache postgresql-dev \ - # Pillow - && apk add --no-cache jpeg-dev zlib-dev \ - && pip install --upgrade pip \ - && pip install -r requirements.txt \ - && pip install -r requirements.production.txt \ - && apk del .build-deps +RUN apk add --no-cache python3-dev build-base linux-headers pcre-dev openssl \ + # PSQL + && apk add --no-cache postgresql-dev \ + # Pillow + && apk add --no-cache jpeg-dev zlib-dev \ + && pip install --upgrade pip \ + && pip install -r requirements.txt \ + && pip install -r requirements.production.txt RUN python manage.py collectstatic --noinput -# run migrate -# python manage.py migrate --noinput - ENTRYPOINT ["sh", "-c", "./production_entrypoint.sh"] diff --git a/docker-compose.yml b/docker-compose.yml index 6a7b964..198c3eb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,12 +2,19 @@ version: '3' services: db: - image: postgres + image: postgres:12 + volumes: + - dbdata:/var/lib/postgresql/data web: build: . image: registry.gitlab.com/sahkoinsinoorikilta/vtmk/web2.0-backend:dev - command: ["bash", "-c", "cd /code && ./wait-for-it.sh db:5432 -- bash setup.sh --no-input --no-npm && python manage.py runserver 0.0.0.0:8000"] + command: ["bash", "-c", "cd /app & bash setup.sh --no-input --no-npm && gunicorn -w 4 -b 0.0.0.0:8000 sikweb.wsgi"] + env_file: + - .env.sample ports: - "8000:8000" depends_on: - db + +volumes: + dbdata: diff --git a/setup.sh b/setup.sh index 53c1a71..d2f82b6 100755 --- a/setup.sh +++ b/setup.sh @@ -3,7 +3,7 @@ echo "SIK WEB 2.0" echo "This script will set up the environment for this project." echo "=========================================================" -echo "Dependencies: python>3.5" +echo "Dependencies: python3.7" INTERACTIVE="true" USE_NPM="true" @@ -65,6 +65,7 @@ fi set -e set -x pip install -r requirements.txt +pip install -r requirements.production.txt $USE_NPM && npm install python manage.py migrate python manage.py createdefaultadmin diff --git a/sikweb/base.py b/sikweb/base.py index 7c026d7..5caee47 100644 --- a/sikweb/base.py +++ b/sikweb/base.py @@ -7,19 +7,12 @@ from django.utils.translation import ugettext_lazy as _ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -IS_DOCKER = bool(os.getenv('IS_DOCKER', None)) - # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/ SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer' -if not IS_DOCKER: - ALLOWED_HOSTS = [] -else: - ALLOWED_HOSTS = ["*"] - # Logger level LOGGERLEVEL = logging.DEBUG @@ -166,36 +159,6 @@ TEMPLATES = [ WSGI_APPLICATION = 'sikweb.wsgi.application' - -# Database -# https://docs.djangoproject.com/en/1.9/ref/settings/#databases - -if not IS_DOCKER: - DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql_psycopg2', - 'NAME': 'sik', - 'USER': 'sik', - 'PASSWORD': 'password123', - 'HOST': 'localhost', - 'PORT': '5432', - 'TEST': { - 'NAME': 'sik_test', - }, - }, - } -else: - logging.info('Using docker database configuration') - DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql_psycopg2', - 'NAME': 'postgres', - 'USER': 'postgres', - 'PASSWORD': 'postgres', - 'HOST': 'db', - 'PORT': '5432', - }, - } # Password validation # https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators diff --git a/wait-for-it.sh b/wait-for-it.sh deleted file mode 100755 index bbe4043..0000000 --- a/wait-for-it.sh +++ /dev/null @@ -1,177 +0,0 @@ -#!/usr/bin/env bash -# Use this script to test if a given TCP host/port are available - -cmdname=$(basename $0) - -echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } - -usage() -{ - cat << USAGE >&2 -Usage: - $cmdname host:port [-s] [-t timeout] [-- command args] - -h HOST | --host=HOST Host or IP under test - -p PORT | --port=PORT TCP port under test - Alternatively, you specify the host and port as host:port - -s | --strict Only execute subcommand if the test succeeds - -q | --quiet Don't output any status messages - -t TIMEOUT | --timeout=TIMEOUT - Timeout in seconds, zero for no timeout - -- COMMAND ARGS Execute command with args after the test finishes -USAGE - exit 1 -} - -wait_for() -{ - if [[ $TIMEOUT -gt 0 ]]; then - echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT" - else - echoerr "$cmdname: waiting for $HOST:$PORT without a timeout" - fi - start_ts=$(date +%s) - while : - do - if [[ $ISBUSY -eq 1 ]]; then - nc -z $HOST $PORT - result=$? - else - (echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1 - result=$? - fi - if [[ $result -eq 0 ]]; then - end_ts=$(date +%s) - echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds" - break - fi - sleep 1 - done - return $result -} - -wait_for_wrapper() -{ - # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 - if [[ $QUIET -eq 1 ]]; then - timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT & - else - timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT & - fi - PID=$! - trap "kill -INT -$PID" INT - wait $PID - RESULT=$? - if [[ $RESULT -ne 0 ]]; then - echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT" - fi - return $RESULT -} - -# process arguments -while [[ $# -gt 0 ]] -do - case "$1" in - *:* ) - hostport=(${1//:/ }) - HOST=${hostport[0]} - PORT=${hostport[1]} - shift 1 - ;; - --child) - CHILD=1 - shift 1 - ;; - -q | --quiet) - QUIET=1 - shift 1 - ;; - -s | --strict) - STRICT=1 - shift 1 - ;; - -h) - HOST="$2" - if [[ $HOST == "" ]]; then break; fi - shift 2 - ;; - --host=*) - HOST="${1#*=}" - shift 1 - ;; - -p) - PORT="$2" - if [[ $PORT == "" ]]; then break; fi - shift 2 - ;; - --port=*) - PORT="${1#*=}" - shift 1 - ;; - -t) - TIMEOUT="$2" - if [[ $TIMEOUT == "" ]]; then break; fi - shift 2 - ;; - --timeout=*) - TIMEOUT="${1#*=}" - shift 1 - ;; - --) - shift - CLI=("$@") - break - ;; - --help) - usage - ;; - *) - echoerr "Unknown argument: $1" - usage - ;; - esac -done - -if [[ "$HOST" == "" || "$PORT" == "" ]]; then - echoerr "Error: you need to provide a host and port to test." - usage -fi - -TIMEOUT=${TIMEOUT:-15} -STRICT=${STRICT:-0} -CHILD=${CHILD:-0} -QUIET=${QUIET:-0} - -# check to see if timeout is from busybox? -# check to see if timeout is from busybox? -TIMEOUT_PATH=$(realpath $(which timeout)) -if [[ $TIMEOUT_PATH =~ "busybox" ]]; then - ISBUSY=1 - BUSYTIMEFLAG="-t" -else - ISBUSY=0 - BUSYTIMEFLAG="" -fi - -if [[ $CHILD -gt 0 ]]; then - wait_for - RESULT=$? - exit $RESULT -else - if [[ $TIMEOUT -gt 0 ]]; then - wait_for_wrapper - RESULT=$? - else - wait_for - RESULT=$? - fi -fi - -if [[ $CLI != "" ]]; then - if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then - echoerr "$cmdname: strict mode, refusing to execute subprocess" - exit $RESULT - fi - exec "${CLI[@]}" -else - exit $RESULT -fi