Skip to content

Commit c1b37ce

Browse files
committed
Add docker-compose file for local development.
1 parent 397ac26 commit c1b37ce

File tree

9 files changed

+168
-4
lines changed

9 files changed

+168
-4
lines changed

.env.template.docker

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
SECRET_KEY=<####SECRET####>
2+
DEBUG=True
3+
ALLOWED_HOSTS=0.0.0.0,127.0.0.1,localhost
4+
DJANGO_SETTINGS_MODULE=djangosnippets.settings.development
5+
SEARCHBOX_SSL_URL=http://elasticsearch:9200/
6+
SESSION_COOKIE_SECURE=False
7+
DATABASE_URL=postgres://djangosnippets:djangosnippets@db/djangosnippets
8+
POSTGRES_USER=djangosnippets
9+
POSTGRES_PASSWORD=djangosnippets
10+
POSTGRES_DB=djangosnippets
11+
REDISTOGO_URL=redis://redis:6379/0

.env.template.local

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SECRET_KEY=<####SECRET####>
2+
DEBUG=True
3+
ALLOWED_HOSTS=0.0.0.0,127.0.0.1,localhost
4+
DJANGO_SETTINGS_MODULE=djangosnippets.settings.development
5+
SEARCHBOX_SSL_URL=http://elasticsearch:9200/
6+
SESSION_COOKIE_SECURE=False
7+
DATABASE_URL=postgres://djangosnippets:djangosnippets@db/djangosnippets
8+
REDISTOGO_URL=redis://redis:6379/0

compose/local/django/Dockerfile

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# define an alias for the specific python version used in this file.
2+
FROM docker.io/python:3.11-slim-bookworm AS python
3+
4+
# Python build stage
5+
FROM python AS python-build-stage
6+
7+
# Install apt packages
8+
RUN apt-get update && apt-get install --no-install-recommends -y \
9+
# dependencies for building Python packages
10+
build-essential \
11+
# psycopg dependencies
12+
libpq-dev
13+
14+
# Requirements are installed here to ensure they will be cached.
15+
COPY ./requirements .
16+
17+
# Create Python Dependency and Sub-Dependency Wheels.
18+
RUN pip wheel --wheel-dir /usr/src/app/wheels \
19+
-r development.txt
20+
21+
# Python 'run' stage
22+
FROM python AS python-run-stage
23+
24+
ARG APP_HOME=/app
25+
26+
ENV PYTHONUNBUFFERED=1
27+
ENV PYTHONDONTWRITEBYTECODE=1
28+
29+
WORKDIR ${APP_HOME}
30+
31+
ARG NODE_MAJOR=20
32+
33+
RUN apt-get update \
34+
&& apt-get install -y ca-certificates curl gnupg \
35+
&& mkdir -p /etc/apt/keyrings \
36+
&& curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
37+
&& echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list \
38+
&& apt-get update \
39+
&& apt-get install nodejs -y \
40+
&& rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man \
41+
&& apt-get clean
42+
43+
# devcontainer dependencies and utils
44+
RUN apt-get update && apt-get install --no-install-recommends -y \
45+
sudo git bash-completion nano ssh
46+
47+
# Create devcontainer user and add it to sudoers
48+
RUN groupadd --gid 1000 dev-user \
49+
&& useradd --uid 1000 --gid dev-user --shell /bin/bash --create-home dev-user \
50+
&& echo dev-user ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/dev-user \
51+
&& chmod 0440 /etc/sudoers.d/dev-user
52+
53+
54+
# Install required system dependencies
55+
RUN apt-get update && apt-get install --no-install-recommends -y \
56+
# psycopg dependencies
57+
libpq-dev \
58+
wait-for-it \
59+
# Translations dependencies
60+
gettext \
61+
# cleaning up unused files
62+
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
63+
&& rm -rf /var/lib/apt/lists/*
64+
65+
# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction
66+
# copy python dependency wheels from python-build-stage
67+
COPY --from=python-build-stage /usr/src/app/wheels /wheels/
68+
69+
# use wheels to install python dependencies
70+
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \
71+
&& rm -rf /wheels/
72+
73+
COPY ./compose/local/django/start /start
74+
RUN sed -i 's/\r$//g' /start
75+
RUN chmod +x /start
76+
77+
CMD ["/start"]

compose/local/django/start

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
set -o errexit
4+
set -o pipefail
5+
set -o nounset
6+
7+
python manage.py migrate
8+
python manage.py tailwind install
9+
python manage.py runserver_plus 0.0.0.0:8000

djangosnippets/settings/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import dj_database_url
44
from django.contrib import messages
55
from django.urls import reverse
6+
from dotenv import load_dotenv
7+
8+
load_dotenv()
69

710

811
def user_url(user):
@@ -178,10 +181,9 @@ def user_url(user):
178181
}
179182

180183

181-
DATABASES = {"default": dj_database_url.config(default="postgres:///djangosnippets")}
184+
DATABASES = {"default": dj_database_url.config(conn_max_age=600, conn_health_checks=True)}
182185
DATABASES["default"]["ATOMIC_REQUESTS"] = True
183186

184-
185187
REST_FRAMEWORK = {
186188
# Use Django's standard `django.contrib.auth` permissions,
187189
# or allow read-only access for unauthenticated users.

docker-compose.local.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
version: "3.7"
2+
3+
x-app: &default-app
4+
build:
5+
context: .
6+
dockerfile: ./compose/local/django/Dockerfile
7+
restart: unless-stopped
8+
volumes:
9+
- .:/app:z
10+
11+
services:
12+
web:
13+
<<: *default-app
14+
container_name: djangosnippets-web-local
15+
ports:
16+
- 8000:8000
17+
env_file:
18+
- ./.env.template.docker
19+
depends_on:
20+
- db
21+
22+
tailwind:
23+
<<: *default-app
24+
container_name: djangosnippets-tailwind-local
25+
command: "python manage.py tailwind start"
26+
# Without tty, no stdin, and tailwind watcher aborts
27+
# https://github.com/tailwindlabs/tailwindcss/issues/5324
28+
tty: true
29+
30+
db:
31+
image: postgres:15.13
32+
container_name: djangosnippets-db-local
33+
restart: always
34+
volumes:
35+
- postgres_data:/var/lib/postgresql/data/
36+
env_file:
37+
- ./.env.template.docker
38+
ports:
39+
- 5432:5432
40+
41+
redis:
42+
image: redis:3.2.12
43+
container_name: djangosnippets-redis-local
44+
restart: always
45+
volumes:
46+
- redisdata:/data
47+
env_file:
48+
- ./.env.template.docker
49+
ports:
50+
- 6379:6379
51+
52+
volumes:
53+
web:
54+
postgres_data:
55+
redisdata:
File renamed without changes.

requirements/base.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ django-taggit==5.0.1
1111
django-tailwind==4.0.1
1212
ipython==7.33.0
1313
Markdown==3.4.4
14-
Pillow==9.1.0
14+
Pillow==11.2.1
1515
Pygments==2.12.0
1616
python-akismet==0.4.2
17+
python-dotenv==1.1.1
1718
requests==2.32.4
1819
six==1.15.0
1920
urllib3==1.26.6
2021
whitenoise==6.1.0
21-
psycopg2-binary==2.9.3
22+
psycopg2-binary==2.9.10
2223
djangorestframework==3.14.0
2324
django-htmx==1.15.0
2425
gevent==22.10.2 # Updated version of gevent

requirements/development.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
-r base.txt
2+
Werkzeug[watchdog]==3.0.6
23
flake8==4.0.1
34
isort==5.8.0
45
pre-commit==2.19.0

0 commit comments

Comments
 (0)