Skip to content

Commit

Permalink
Dockerizing the scorer
Browse files Browse the repository at this point in the history
  • Loading branch information
nutrina committed Oct 4, 2022
1 parent 4cd3a6d commit 5c2988f
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 20 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# list of files to exclude from docker build
.git
.cache
db.sqlite3
5 changes: 5 additions & 0 deletions .env-sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

DEBUG=on
SECRET_KEY=this_should_be_a super_secret_key
DATABASE_URL=sqlite:////db.sqlite3
ALLOWED_HOSTS='["example.host.com"]'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__pycache__
.env
53 changes: 53 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# https://pipenv.pypa.io/en/latest/basics/#pipenv-and-docker-containers
FROM docker.io/python:3.10 AS base

RUN apt update && \
apt install -y python3-dev libpq-dev


#########################################################
# Builder
#########################################################
FROM base AS builder

RUN pip install --user pipenv

# Tell pipenv to create venv in the current directory
ENV PIPENV_VENV_IN_PROJECT=1

ADD Pipfile.lock Pipfile /usr/src/

WORKDIR /usr/src


# NOTE: If you install binary packages required for a python module, you need
# to install them again in the runtime. For example, if you need to install pycurl
# you need to have pycurl build dependencies libcurl4-gnutls-dev and libcurl3-gnutls
# In the runtime container you need only libcurl3-gnutls

# RUN apt install -y libcurl3-gnutls libcurl4-gnutls-dev

RUN /root/.local/bin/pipenv sync

RUN /usr/src/.venv/bin/python -c "import django; print(django.__version__)"



#########################################################
# Runtime
#########################################################
FROM base AS runtime

RUN mkdir -v /usr/src/venv

COPY --from=builder /usr/src/.venv/ /usr/src/.venv/
COPY . /app

RUN /usr/src/.venv/bin/python -c "import django; print(django.__version__)"
ENV PATH="/usr/src/.venv/bin/:${PATH}"

WORKDIR /app

RUN STATIC_ROOT=/app/static SECRET_KEY=secret_is_irelevent_here DATABASE_URL=sqlite:////dunmmy_db.sqlite3 python manage.py collectstatic --noinput

CMD ["gunicorn", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "scorer.asgi:application", "-b", "0.0.0.0:8000"]
3 changes: 2 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ whitenoise = "*"
pyyaml = "*"
uritemplate = "*"
django-cors-headers = "*"
django-environ = "*"

[dev-packages]
black = "*"
Expand All @@ -24,4 +25,4 @@ pylint = "*"
pylint-django = "*"

[requires]
python_version = "3.8"
python_version = "3.10"
26 changes: 17 additions & 9 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

Start like: `gunicorn scorer.asgi:application -k uvicorn.workers.UvicornWorker`
# Getting Started

or `uvicorn scorer.asgi:application --reload`
Create virtual env and install dependencies: `pipenv install`

Start the dev server:
- `gunicorn -w 4 -k uvicorn.workers.UvicornWorker scorer.asgi:application`
- or `uvicorn scorer.asgi:application --reload`
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3'

services:
scorer:
build: .
restart: unless-stopped
env_file: .env

volumes:
- ./db.sqlite3:/db.sqlite3

ports:
- 80:8000
25 changes: 17 additions & 8 deletions scorer/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

from pathlib import Path

import environ

env = environ.Env(
# set casting, default value
DEBUG=(bool, False),
)

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

Expand All @@ -20,16 +27,15 @@
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-rbb-*4!aa&z&rt*!5hf=b-l84+5_mq^xh$*^=!4hv+@%!g)a+h"
SECRET_KEY = env("SECRET_KEY", default="some-secret-value")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = env("DEBUG", default=True)

ALLOWED_HOSTS = []
ALLOWED_HOSTS = env.json("ALLOWED_HOSTS", default=[])


# Application definition

INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
Expand Down Expand Up @@ -83,10 +89,7 @@
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
"default": env.db(default="sqlite:////db.sqlite3"),
}


Expand Down Expand Up @@ -158,3 +161,9 @@
"level": "DEBUG",
},
}

STATIC_ROOT = BASE_DIR / "static"

print("=" * 80)
print("ALLOWED_HOSTS", ALLOWED_HOSTS)
print("=" * 80)

0 comments on commit 5c2988f

Please sign in to comment.