Skip to content

Commit 5ee5ac5

Browse files
committed
Add Docker setup
1 parent c6aecbf commit 5ee5ac5

File tree

5 files changed

+83
-14
lines changed

5 files changed

+83
-14
lines changed

.env

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
DEBUG=0
2+
DB_ENGINE=django.db.backends.postgresql
3+
DB_HOST=db
4+
DB_PORT=5432
5+
DB_NAME=django_index
6+
DB_USER=testUser
7+
DB_PASSWORD=django_index_password
8+
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]

Dockerfile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM bitnami/python:3.12.7
2+
3+
# set working directory
4+
WORKDIR /usr/src/app
5+
6+
# install system dependencies
7+
RUN apt-get update \
8+
&& apt-get -qq -y install netcat-traditional \
9+
libglib2.0-0 libsm6 libxext6 libxrender-dev libgomp1 \
10+
graphviz graphviz-dev gcc \
11+
&& apt-get -qq clean
12+
13+
# install python dependencies
14+
COPY ./requirements.txt /usr/src/app/requirements.txt
15+
16+
RUN pip install --upgrade pip
17+
RUN pip install --no-cache-dir -r requirements.txt
18+
19+
# copy project
20+
COPY . /usr/src/app/

django_index_project/settings.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""
1212

1313
from pathlib import Path
14+
import os
1415

1516
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1617
BASE_DIR = Path(__file__).resolve().parent.parent
@@ -23,7 +24,7 @@
2324
SECRET_KEY = "django-insecure-+031!wnlhf@ij86qi%7mu^f+r4hhezl=tz&05mvbopvh^)g_@u"
2425

2526
# SECURITY WARNING: don't run with debug turned on in production!
26-
DEBUG = True
27+
DEBUG = int(os.environ.get("DEBUG", 0))
2728

2829
ALLOWED_HOSTS = ["*"]
2930

@@ -74,21 +75,14 @@
7475
# Database
7576
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
7677

77-
# DATABASES = {
78-
# 'default': {
79-
# 'ENGINE': 'django.db.backends.sqlite3',
80-
# 'NAME': BASE_DIR / 'db.sqlite3',
81-
# }
82-
# }
83-
8478
DATABASES = {
8579
"default": {
86-
"ENGINE": "django.db.backends.postgresql",
87-
"HOST": "127.0.0.1",
88-
"PORT": 5432,
89-
"USER": "monitaur",
90-
"PASSWORD": "dummy",
91-
"NAME": "monitaur",
80+
"ENGINE": os.environ.get("DB_ENGINE", "django.db.backends.sqlite3"),
81+
"HOST": os.environ.get("DB_HOST", "localhost"),
82+
"PORT": os.environ.get("DB_PORT", "5432"),
83+
"USER": os.environ.get("DB_USER", "postgres"),
84+
"PASSWORD": os.environ.get("DB_PASSWORD", "postgres"),
85+
"NAME": os.environ.get("DB_NAME", "postgres"),
9286
}
9387
}
9488

docker-compose.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
services:
2+
3+
api:
4+
build: .
5+
command: python manage.py runserver 0.0.0.0:8080
6+
entrypoint: ["/usr/src/app/entrypoint.sh"]
7+
volumes:
8+
- .:/usr/src/app
9+
ports:
10+
- 8080:8080
11+
depends_on:
12+
- db
13+
env_file:
14+
- .env
15+
networks:
16+
- django_index_network
17+
18+
db:
19+
image: postgres:latest
20+
volumes:
21+
- db_data:/var/lib/postgresql/data/
22+
environment:
23+
- POSTGRES_USER=testUser
24+
- POSTGRES_PASSWORD=django_index_password
25+
- POSTGRES_DB=django_index
26+
networks:
27+
- django_index_network
28+
29+
30+
networks:
31+
django_index_network:
32+
33+
volumes:
34+
db_data:

entrypoint.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
3+
while ! nc -z db 5432; do
4+
echo "Waiting for database...."
5+
sleep 0.1
6+
done
7+
8+
echo "Database started"
9+
10+
python manage.py migrate --no-input
11+
# python manage.py seed_db --no-input
12+
13+
exec "$@"

0 commit comments

Comments
 (0)