Skip to content

Commit 175ebf8

Browse files
authored
Add dockerize, django-environ (#203)
* Remove unused cache purge * Use django-environ to read config from env * Pin dependencies for reproducible builds * Add dockerize and a simple entrypoint
1 parent 3349936 commit 175ebf8

File tree

13 files changed

+63
-75
lines changed

13 files changed

+63
-75
lines changed

.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
POSTGRES_USER=postgres
2+
POSTGRES_PASSWORD=postgres
3+
POSTGRES_DB=catpol
4+
5+
DATABASE_URL=postgres://postgres:postgres@db/catpol

Dockerfile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
FROM alexstefanescu/catpol-dependencies
22

3-
RUN pip3 install --upgrade pip setuptools
3+
RUN pip3 install --upgrade pip setuptools \
4+
&& wget -qO- https://github.com/jwilder/dockerize/releases/download/v0.2.0/dockerize-linux-amd64-v0.2.0.tar.gz | tar -zxf - -C /usr/bin \
5+
&& chown root:root /usr/bin/dockerize
46

57
ARG ENVIRONMENT=dev
68
ENV DJANGO_SETTINGS_MODULE=project_template.settings.${ENVIRONMENT}
79

8-
# RUN find -type d -name __pycache__ -prune -exec rm -rf {} \; && \
9-
# rm -rf ~/.cache/pip
10+
COPY ./ /opt/catpol
11+
12+
# Re-install dependencies since it might have updated from cache
13+
RUN pip3 install -r requirements-${ENVIRONMENT}.txt
1014

11-
COPY . /opt/catpol
1215
RUN python3 manage.py check
13-
CMD python3 manage.py migrate --run-syncdb \
14-
&& python3 manage.py runserver 0.0.0.0:8000
16+
17+
ENTRYPOINT ["/opt/catpol/docker-entrypoint"]
1518
EXPOSE 8000

catpol-dependencies/Dockerfile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ RUN pip3 install --upgrade pip setuptools
1313
ARG ENVIRONMENT=dev
1414

1515
WORKDIR /opt/catpol
16-
COPY ./requirements* /opt/catpol/
17-
RUN pip3 install -r requirements-${ENVIRONMENT}.txt
1816

19-
# RUN find -type d -name __pycache__ -prune -exec rm -rf {} \; && \
20-
# rm -rf ~/.cache/pip
17+
COPY ./requirements* /opt/catpol/
18+
RUN pip3 install -r requirements-${ENVIRONMENT}.txt

database.env

Lines changed: 0 additions & 3 deletions
This file was deleted.

docker-compose.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
version: '3'
22
services:
3-
catpol-database:
3+
web:
4+
build: .
5+
command: ls -la /opt/catpol && dockerize --wait tcp://db:3306
6+
ports:
7+
- "8000:8000"
8+
volumes:
9+
- ./:/opt/catpol
10+
11+
db:
412
image: "postgres:12"
5-
container_name: catpol_psql
613
ports:
714
- "5432:5432"
8-
env_file:
9-
- database.env
1015
volumes:
11-
- database-data:/var/lib/postgresql/data/
12-
catpol:
13-
build: .
14-
container_name: catpol
15-
ports:
16-
- "8000:8000"
16+
- database-data:/var/lib/postgresql/data/
1717

1818
volumes:
19-
database-data:
19+
database-data:

docker-entrypoint

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
PORT=${PORT:-"8000"}
4+
5+
echo "Migrating databse"
6+
python3 manage.py migrate --run-syncdb
7+
8+
echo "Start web server on $PORT"
9+
python3 manage.py runserver "0.0.0.0:$PORT"

project_template/settings/base.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@
1212

1313
import os
1414

15+
import environ
1516
from moonsheep.settings import *
1617

18+
root = environ.Path(__file__) - 3 # three folder back (/a/b/c/ - 3 = /)
19+
env = environ.Env(
20+
# set casting, default value
21+
DEBUG=(bool, False)
22+
)
23+
environ.Env.read_env(f"{root}/.env") # reading .env file
24+
25+
1726
MOONSHEEP.update({
1827
'APP': 'project_template' # TODO list from document models instead of defining here
1928
})
@@ -100,6 +109,14 @@
100109
USE_L10N = True
101110
USE_TZ = True
102111

112+
113+
DATABASES = {
114+
# read os.environ['DATABASE_URL'] and raises ImproperlyConfigured exception if not found
115+
'default': env.db('DATABASE_URL'),
116+
# read os.environ['SQLITE_URL']
117+
'extra': env.db('SQLITE_URL', default=f"sqlite:///{os.path.join(BASE_DIR, 'db.sqlite3'),}")
118+
}
119+
103120
# Static files (CSS, JavaScript, Images)
104121
# https://docs.djangoproject.com/en/2.0/howto/static-files/
105122

project_template/settings/dev.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,10 @@
3434
INSTALLED_APPS += ['debug_toolbar', 'django_extensions']
3535
MIDDLEWARE.insert(1, 'debug_toolbar.middleware.DebugToolbarMiddleware')
3636

37+
# TODO: read it from env or generate a new one
3738
SECRET_KEY = 'https://uploads.skyhighnetworks.com/wp-content/uploads/2015/08/06195203/Bart-Chalkboard-for-Blog-Post.png'
3839

3940
# Database
4041
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
4142

42-
DATABASES = {
43-
'default': {
44-
'ENGINE': 'django.db.backends.postgresql',
45-
'NAME': 'catpol',
46-
'USER': 'postgres',
47-
'PASSWORD': 'postgres',
48-
'HOST': 'catpol-database',
49-
'PORT': '5432',
50-
},
51-
'sqlite': {
52-
'ENGINE': 'django.db.backends.sqlite3',
53-
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
54-
},
55-
}
56-
57-
5843
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

project_template/settings/production.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
1616

17+
# TODO: read secret env form env, it should be added at deploy time
1718
SECRET_KEY = '${SECRET_KEY}'
1819

1920
# To be double sure
@@ -29,17 +30,6 @@
2930
# Database
3031
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
3132

32-
# DATABASES = {
33-
# 'default': {
34-
# 'ENGINE': 'django.db.backends.postgresql',
35-
# 'NAME': 'moonsheep',
36-
# 'USER': 'moonsheep',
37-
# 'PASSWORD': 'moonsheep',
38-
# 'HOST': 'postgres',
39-
# 'PORT': 5432,
40-
# }
41-
# }
42-
4333
DATABASES = {
4434
'default': {
4535
'ENGINE': 'django.db.backends.postgresql_psycopg2',

project_template/settings/stage.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
1616

17+
# TODO: read secret env form env, it should be added at deploy time
1718
SECRET_KEY = '${SECRET_KEY}'
1819

1920
# To be double sure
@@ -24,18 +25,3 @@
2425
INTERNAL_IPS = []
2526

2627
WSGI_APPLICATION = 'project_template.wsgi.application'
27-
28-
29-
# Database
30-
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
31-
32-
# DATABASES = {
33-
# 'default': {
34-
# 'ENGINE': 'django.db.backends.postgresql',
35-
# 'NAME': 'moonsheep',
36-
# 'USER': 'moonsheep',
37-
# 'PASSWORD': 'moonsheep',
38-
# 'HOST': 'postgres',
39-
# 'PORT': 5432,
40-
# }
41-
# }

0 commit comments

Comments
 (0)