Skip to content

Commit 26be9cd

Browse files
committed
updates
1 parent 5ebc9ed commit 26be9cd

12 files changed

+63
-65
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Michael Herman
3+
Copyright (c) 2023 TestDriven.io
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Want to learn how to build this?
44

5-
Check out the [post](https://testdriven.io/dockerizing-django-with-postgres-gunicorn-and-nginx).
5+
Check out the [tutorial](https://testdriven.io/dockerizing-django-with-postgres-gunicorn-and-nginx).
66

77
## Want to use this project?
88

app/Dockerfile

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pull official base image
2-
FROM python:3.9.6-alpine
2+
FROM python:3.11.4-slim-buster
33

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

11-
# install psycopg2 dependencies
12-
RUN apk update \
13-
&& apk add postgresql-dev gcc python3-dev musl-dev
11+
# install system dependencies
12+
RUN apt-get update && apt-get install -y netcat
1413

1514
# install dependencies
1615
RUN pip install --upgrade pip

app/Dockerfile.prod

+11-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
###########
44

55
# pull official base image
6-
FROM python:3.9.6-alpine as builder
6+
FROM python:3.11.4-slim-buster as builder
77

88
# set work directory
99
WORKDIR /usr/src/app
@@ -12,17 +12,17 @@ WORKDIR /usr/src/app
1212
ENV PYTHONDONTWRITEBYTECODE 1
1313
ENV PYTHONUNBUFFERED 1
1414

15-
# install psycopg2 dependencies
16-
RUN apk update \
17-
&& apk add postgresql-dev gcc python3-dev musl-dev
15+
# install system dependencies
16+
RUN apt-get update && \
17+
apt-get install -y --no-install-recommends gcc
1818

1919
# lint
2020
RUN pip install --upgrade pip
21-
RUN pip install flake8==3.9.2
22-
COPY . .
21+
RUN pip install flake8==6.0.0
22+
COPY . /usr/src/app/
2323
RUN flake8 --ignore=E501,F401 .
2424

25-
# install dependencies
25+
# install python dependencies
2626
COPY ./requirements.txt .
2727
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
2828

@@ -32,13 +32,13 @@ RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requir
3232
#########
3333

3434
# pull official base image
35-
FROM python:3.9.6-alpine
35+
FROM python:3.11.4-slim-buster
3636

3737
# create directory for the app user
3838
RUN mkdir -p /home/app
3939

4040
# create the app user
41-
RUN addgroup -S app && adduser -S app -G app
41+
RUN addgroup --system app && adduser --system --group app
4242

4343
# create the appropriate directories
4444
ENV HOME=/home/app
@@ -49,9 +49,10 @@ RUN mkdir $APP_HOME/mediafiles
4949
WORKDIR $APP_HOME
5050

5151
# install dependencies
52-
RUN apk update && apk add libpq
52+
RUN apt-get update && apt-get install -y --no-install-recommends netcat
5353
COPY --from=builder /usr/src/app/wheels /wheels
5454
COPY --from=builder /usr/src/app/requirements.txt .
55+
RUN pip install --upgrade pip
5556
RUN pip install --no-cache /wheels/*
5657

5758
# copy entrypoint.prod.sh

app/hello_django/asgi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
It exposes the ASGI callable as a module-level variable named ``application``.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
7+
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
88
"""
99

1010
import os
1111

1212
from django.core.asgi import get_asgi_application
1313

14-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hello_django.settings')
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_django.settings")
1515

1616
application = get_asgi_application()

app/hello_django/settings.py

+35-37
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
"""
22
Django settings for hello_django project.
33
4-
Generated by 'django-admin startproject' using Django 3.2.6.
4+
Generated by 'django-admin startproject' using Django 4.2.3.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/3.2/topics/settings/
7+
https://docs.djangoproject.com/en/4.2/topics/settings/
88
99
For the full list of settings and their values, see
10-
https://docs.djangoproject.com/en/3.2/ref/settings/
10+
https://docs.djangoproject.com/en/4.2/ref/settings/
1111
"""
1212

1313
import os
1414
from pathlib import Path
1515

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

2019

2120
# Quick-start development settings - unsuitable for production
22-
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
21+
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
2322

2423
SECRET_KEY = os.environ.get("SECRET_KEY")
2524

@@ -29,6 +28,7 @@
2928
# For example: 'DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]'
3029
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(" ")
3130

31+
CSRF_TRUSTED_ORIGINS = ["http://localhost:1337"]
3232

3333
# Application definition
3434

@@ -44,38 +44,38 @@
4444
]
4545

4646
MIDDLEWARE = [
47-
'django.middleware.security.SecurityMiddleware',
48-
'django.contrib.sessions.middleware.SessionMiddleware',
49-
'django.middleware.common.CommonMiddleware',
50-
'django.middleware.csrf.CsrfViewMiddleware',
51-
'django.contrib.auth.middleware.AuthenticationMiddleware',
52-
'django.contrib.messages.middleware.MessageMiddleware',
53-
'django.middleware.clickjacking.XFrameOptionsMiddleware',
47+
"django.middleware.security.SecurityMiddleware",
48+
"django.contrib.sessions.middleware.SessionMiddleware",
49+
"django.middleware.common.CommonMiddleware",
50+
"django.middleware.csrf.CsrfViewMiddleware",
51+
"django.contrib.auth.middleware.AuthenticationMiddleware",
52+
"django.contrib.messages.middleware.MessageMiddleware",
53+
"django.middleware.clickjacking.XFrameOptionsMiddleware",
5454
]
5555

56-
ROOT_URLCONF = 'hello_django.urls'
56+
ROOT_URLCONF = "hello_django.urls"
5757

5858
TEMPLATES = [
5959
{
60-
'BACKEND': 'django.template.backends.django.DjangoTemplates',
61-
'DIRS': [],
62-
'APP_DIRS': True,
63-
'OPTIONS': {
64-
'context_processors': [
65-
'django.template.context_processors.debug',
66-
'django.template.context_processors.request',
67-
'django.contrib.auth.context_processors.auth',
68-
'django.contrib.messages.context_processors.messages',
60+
"BACKEND": "django.template.backends.django.DjangoTemplates",
61+
"DIRS": [],
62+
"APP_DIRS": True,
63+
"OPTIONS": {
64+
"context_processors": [
65+
"django.template.context_processors.debug",
66+
"django.template.context_processors.request",
67+
"django.contrib.auth.context_processors.auth",
68+
"django.contrib.messages.context_processors.messages",
6969
],
7070
},
7171
},
7272
]
7373

74-
WSGI_APPLICATION = 'hello_django.wsgi.application'
74+
WSGI_APPLICATION = "hello_django.wsgi.application"
7575

7676

7777
# Database
78-
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
78+
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
7979

8080
DATABASES = {
8181
"default": {
@@ -90,40 +90,38 @@
9090

9191

9292
# Password validation
93-
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
93+
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
9494

9595
AUTH_PASSWORD_VALIDATORS = [
9696
{
97-
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
97+
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
9898
},
9999
{
100-
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
100+
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
101101
},
102102
{
103-
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
103+
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
104104
},
105105
{
106-
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
106+
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
107107
},
108108
]
109109

110110

111111
# Internationalization
112-
# https://docs.djangoproject.com/en/3.2/topics/i18n/
112+
# https://docs.djangoproject.com/en/4.2/topics/i18n/
113113

114-
LANGUAGE_CODE = 'en-us'
114+
LANGUAGE_CODE = "en-us"
115115

116-
TIME_ZONE = 'UTC'
116+
TIME_ZONE = "UTC"
117117

118118
USE_I18N = True
119119

120-
USE_L10N = True
121-
122120
USE_TZ = True
123121

124122

125123
# Static files (CSS, JavaScript, Images)
126-
# https://docs.djangoproject.com/en/3.2/howto/static-files/
124+
# https://docs.djangoproject.com/en/4.2/howto/static-files/
127125

128126
STATIC_URL = "/static/"
129127
STATIC_ROOT = BASE_DIR / "staticfiles"
@@ -133,6 +131,6 @@
133131

134132

135133
# Default primary key field type
136-
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
134+
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
137135

138-
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
136+
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

app/hello_django/wsgi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
It exposes the WSGI callable as a module-level variable named ``application``.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
7+
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
88
"""
99

1010
import os
1111

1212
from django.core.wsgi import get_wsgi_application
1313

14-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hello_django.settings')
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_django.settings")
1515

1616
application = get_wsgi_application()

app/manage.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def main():
88
"""Run administrative tasks."""
9-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hello_django.settings')
9+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello_django.settings")
1010
try:
1111
from django.core.management import execute_from_command_line
1212
except ImportError as exc:
@@ -18,5 +18,5 @@ def main():
1818
execute_from_command_line(sys.argv)
1919

2020

21-
if __name__ == '__main__':
21+
if __name__ == "__main__":
2222
main()

app/requirements.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Django==3.2.6
2-
gunicorn==20.1.0
3-
psycopg2-binary==2.9.1
1+
Django==4.2.3
2+
gunicorn==21.2.0
3+
psycopg2-binary==2.9.6

docker-compose.prod.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ services:
1616
depends_on:
1717
- db
1818
db:
19-
image: postgres:13.0-alpine
19+
image: postgres:15
2020
volumes:
2121
- postgres_data:/var/lib/postgresql/data/
2222
env_file:

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ services:
1313
depends_on:
1414
- db
1515
db:
16-
image: postgres:13.0-alpine
16+
image: postgres:15
1717
volumes:
1818
- postgres_data:/var/lib/postgresql/data/
1919
environment:

nginx/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM nginx:1.21-alpine
1+
FROM nginx:1.25
22

33
RUN rm /etc/nginx/conf.d/default.conf
44
COPY nginx.conf /etc/nginx/conf.d

0 commit comments

Comments
 (0)