Skip to content

Commit e65cdcf

Browse files
authored
Merge pull request testdrivenio#13 from testdrivenio/update
remove pipenv, add multi-stage build, and upgrade to python 3.8
2 parents 07cf292 + 7b4bc1a commit e65cdcf

17 files changed

+123
-139
lines changed

.env.dev-sample

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DEBUG=1
2+
SECRET_KEY=foo
3+
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
4+
SQL_ENGINE=django.db.backends.postgresql
5+
SQL_DATABASE=hello_django_dev
6+
SQL_USER=hello_django
7+
SQL_PASSWORD=hello_django
8+
SQL_HOST=db
9+
SQL_PORT=5432
10+
DATABASE=postgres

.env-sample renamed to .env.prod-sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
DEBUG=0
22
SECRET_KEY=change_me
3+
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
34
SQL_ENGINE=django.db.backends.postgresql
45
SQL_DATABASE=hello_django_prod
56
SQL_USER=hello_django
File renamed without changes.

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*.pyc
22
__pycache
33
.DS_Store
4-
.env
5-
.env.db
4+
.env.dev
5+
.env.prod
6+
.env.prod.db

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ Check out the [post](https://testdriven.io/dockerizing-django-with-postgres-guni
1010

1111
Uses the default Django development server.
1212

13-
1. Update the environment variables in the *docker-compose.yml* file.
13+
1. Rename *.env.dev-sample* to *.env.dev*.
14+
1. Update the environment variables in the *docker-compose.yml* and *.env.dev* files.
1415
1. Build the images and run the containers:
1516

1617
```sh
@@ -23,7 +24,7 @@ Uses the default Django development server.
2324

2425
Uses gunicorn + nginx.
2526

26-
1. Rename *.env-sample* to *.env* and *.env.db-sample* to *.env.db*. Update the environment variables.
27+
1. Rename *.env.prod-sample* to *.env.prod* and *.env.prod.db-sample* to *.env.prod.db*. Update the environment variables.
2728
1. Build the images and run the containers:
2829

2930
```sh

app/Dockerfile

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pull official base image
2-
FROM python:3.7.4-alpine
2+
FROM python:3.8.0-alpine
33

44
# set work directory
55
WORKDIR /usr/src/app
@@ -8,18 +8,14 @@ WORKDIR /usr/src/app
88
ENV PYTHONDONTWRITEBYTECODE 1
99
ENV PYTHONUNBUFFERED 1
1010

11-
# install psycopg2
11+
# install psycopg2 dependencies
1212
RUN apk update \
13-
&& apk add --virtual build-deps gcc python3-dev musl-dev \
14-
&& apk add postgresql-dev \
15-
&& pip install psycopg2 \
16-
&& apk del build-deps
13+
&& apk add postgresql-dev gcc python3-dev musl-dev
1714

1815
# install dependencies
1916
RUN pip install --upgrade pip
20-
RUN pip install pipenv
21-
COPY ./Pipfile /usr/src/app/Pipfile
22-
RUN pipenv install --skip-lock --system --dev
17+
COPY ./requirements.txt /usr/src/app/requirements.txt
18+
RUN pip install -r requirements.txt
2319

2420
# copy entrypoint.sh
2521
COPY ./entrypoint.sh /usr/src/app/entrypoint.sh

app/Dockerfile.prod

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
###########
2+
# BUILDER #
3+
###########
4+
15
# pull official base image
2-
FROM python:3.7.4-alpine
6+
FROM python:3.8.0-alpine as builder
37

48
# set work directory
59
WORKDIR /usr/src/app
@@ -8,24 +12,57 @@ WORKDIR /usr/src/app
812
ENV PYTHONDONTWRITEBYTECODE 1
913
ENV PYTHONUNBUFFERED 1
1014

11-
# install psycopg2
15+
# install psycopg2 dependencies
1216
RUN apk update \
13-
&& apk add --virtual build-deps gcc python3-dev musl-dev \
14-
&& apk add postgresql-dev \
15-
&& pip install psycopg2 \
16-
&& apk del build-deps
17+
&& apk add postgresql-dev gcc python3-dev musl-dev
1718

18-
# install dependencies
19+
# lint
1920
RUN pip install --upgrade pip
20-
RUN pip install pipenv
21-
COPY ./Pipfile /usr/src/app/Pipfile
22-
RUN pipenv install --skip-lock --system
21+
RUN pip install flake8
22+
COPY . /usr/src/app/
23+
RUN flake8 --ignore=E501,F401 .
24+
25+
# install dependencies
26+
COPY ./requirements.txt .
27+
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
28+
29+
30+
#########
31+
# FINAL #
32+
#########
33+
34+
# pull official base image
35+
FROM python:3.8.0-alpine
36+
37+
# create directory for the app user
38+
RUN mkdir -p /home/app
39+
40+
# create the app user
41+
RUN addgroup -S app && adduser -S app -G app
42+
43+
# create the home directory
44+
ENV HOME=/home/app
45+
ENV APP_HOME=/home/app/web
46+
RUN mkdir $APP_HOME
47+
WORKDIR $APP_HOME
48+
49+
# install dependencies
50+
RUN apk update && apk add libpq
51+
COPY --from=builder /usr/src/app/wheels /wheels
52+
COPY --from=builder /usr/src/app/requirements.txt .
53+
RUN pip install --no-cache /wheels/*
2354

2455
# copy entrypoint-prod.sh
25-
COPY ./entrypoint.prod.sh /usr/src/app/entrypoint.prod.sh
56+
COPY ./entrypoint.prod.sh $APP_HOME
2657

2758
# copy project
28-
COPY . /usr/src/app/
59+
COPY . $APP_HOME
60+
61+
# chown all the files to the app user
62+
RUN chown -R app:app $APP_HOME
63+
64+
# change to the app user
65+
USER app
2966

3067
# run entrypoint.prod.sh
31-
ENTRYPOINT ["/usr/src/app/entrypoint.prod.sh"]
68+
ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]

app/Pipfile

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

app/Pipfile.lock

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

app/hello_django/settings.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Django settings for hello_django project.
33
4-
Generated by 'django-admin startproject' using Django 2.2.4.
4+
Generated by 'django-admin startproject' using Django 2.2.6.
55
66
For more information on this file, see
77
https://docs.djangoproject.com/en/2.2/topics/settings/
@@ -19,24 +19,26 @@
1919
# Quick-start development settings - unsuitable for production
2020
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
2121

22-
SECRET_KEY = os.environ.get('SECRET_KEY')
22+
SECRET_KEY = os.environ.get("SECRET_KEY")
2323

24-
DEBUG = int(os.environ.get('DEBUG', default=0))
24+
DEBUG = int(os.environ.get("DEBUG", default=0))
2525

26-
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
26+
# 'DJANGO_ALLOWED_HOSTS' should be a single string of hosts with a space between each.
27+
# For example: 'DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]'
28+
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(" ")
2729

2830

2931
# Application definition
3032

3133
INSTALLED_APPS = [
32-
'django.contrib.admin',
33-
'django.contrib.auth',
34-
'django.contrib.contenttypes',
35-
'django.contrib.sessions',
36-
'django.contrib.messages',
37-
'django.contrib.staticfiles',
38-
39-
'upload',
34+
"django.contrib.admin",
35+
"django.contrib.auth",
36+
"django.contrib.contenttypes",
37+
"django.contrib.sessions",
38+
"django.contrib.messages",
39+
"django.contrib.staticfiles",
40+
41+
"upload",
4042
]
4143

4244
MIDDLEWARE = [
@@ -74,13 +76,13 @@
7476
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
7577

7678
DATABASES = {
77-
'default': {
78-
'ENGINE': os.environ.get('SQL_ENGINE', 'django.db.backends.sqlite3'),
79-
'NAME': os.environ.get('SQL_DATABASE', os.path.join(BASE_DIR, 'db.sqlite3')),
80-
'USER': os.environ.get('SQL_USER', 'user'),
81-
'PASSWORD': os.environ.get('SQL_PASSWORD', 'password'),
82-
'HOST': os.environ.get('SQL_HOST', 'localhost'),
83-
'PORT': os.environ.get('SQL_PORT', '5432'),
79+
"default": {
80+
"ENGINE": os.environ.get("SQL_ENGINE", "django.db.backends.sqlite3"),
81+
"NAME": os.environ.get("SQL_DATABASE", os.path.join(BASE_DIR, "db.sqlite3")),
82+
"USER": os.environ.get("SQL_USER", "user"),
83+
"PASSWORD": os.environ.get("SQL_PASSWORD", "password"),
84+
"HOST": os.environ.get("SQL_HOST", "localhost"),
85+
"PORT": os.environ.get("SQL_PORT", "5432"),
8486
}
8587
}
8688

@@ -121,8 +123,8 @@
121123
# Static files (CSS, JavaScript, Images)
122124
# https://docs.djangoproject.com/en/2.2/howto/static-files/
123125

124-
STATIC_URL = '/staticfiles/'
125-
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
126+
STATIC_URL = "/staticfiles/"
127+
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
126128

127-
MEDIA_URL = '/mediafiles/'
128-
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')
129+
MEDIA_URL = "/mediafiles/"
130+
MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles")

0 commit comments

Comments
 (0)