Skip to content

Commit fe3e48c

Browse files
Merge pull request #51 from RandomProgramm3r/develop
feat: Dockerize application for development This commit introduces a complete Docker and Docker Compose setup, enabling developers to build, run, and test the application in a consistent, isolated environment. It also prepares the application for containerized deployments. Changes include: - **Dockerfile**: A multi-stage build process is implemented. It separates build-time dependencies from the final runtime image, creating a smaller and more secure production artifact. `gunicorn` is used as the application server. - **docker-compose.yml**: Defines the `app` and `redis` services, linking them together. - **settings.py**: - Configured to connect to the Redis service for Django's caching backend. - Added settings for the Anti-fraud service URLs. - All new configurations (Redis URL, Anti-fraud URLs) are driven by environment variables for flexibility and security, with sensible defaults for local development. - **requirements/prod.txt**: Added `django-redis` and `requests`. - **.dockerignore**: Updated to exclude unnecessary files from the Docker build context, ensuring faster and smaller builds.
2 parents 19f263b + bb1d054 commit fe3e48c

File tree

5 files changed

+105
-15
lines changed

5 files changed

+105
-15
lines changed

.dockerignore

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
.dockerignore
2-
Dockerfile
3-
README.md
1+
# Virtual Environments
2+
venv/
3+
env/
4+
.venv/
5+
.env/
46

7+
# Python cache
8+
__pycache__/
59
*.pyc
6-
*.pyo
7-
*.pyd
8-
__pycache__
910

10-
env/
11-
.venv/
12-
.env
13-
.env.example
14-
.venv/
11+
# Tooling cache & reports
12+
.ruff_cache/
13+
.coverage
14+
.coverage.*
15+
16+
# Version Control
17+
.git/
18+
.gitignore
19+
20+
# IDE and editor directories
21+
.vscode/
22+
.idea/
23+
24+
# Requirements files not needed in production
25+
requirements/dev.txt
1526

16-
.git
17-
.gitignore
27+
# Log files
28+
*.log
29+
*.log.*

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ ENV PYTHONBUFFERED=1 \
77

88
WORKDIR /usr/src/app
99

10-
COPY . .
10+
COPY requirements/prod.txt ./requirements.txt
1111

1212
RUN pip install --upgrade pip && \
13-
pip install -r requirements/prod.txt
13+
pip install --no-cache-dir -r requirements.txt
14+
15+
COPY . .
1416

1517
CMD ["sh", "-c", "cd /usr/src/app/promo_code && python manage.py migrate --noinput && gunicorn promo_code.wsgi:application --bind ${SERVER_ADDRESS}"]
1618

docker-compose.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
services:
2+
web:
3+
build: .
4+
container_name: promo_code
5+
ports:
6+
- 8000:8080
7+
volumes:
8+
- .:/usr/src/app
9+
depends_on:
10+
db:
11+
condition: service_healthy
12+
restart: true
13+
redis:
14+
condition: service_started
15+
antifraud:
16+
condition: service_started
17+
18+
env_file:
19+
- .env
20+
db:
21+
image: postgres:17
22+
container_name: postgres_db
23+
volumes:
24+
- postgres_db:/var/lib/postgresql/data/
25+
environment:
26+
POSTGRES_DB: ${POSTGRES_DATABASE}
27+
POSTGRES_USER: ${POSTGRES_USERNAME}
28+
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
29+
healthcheck:
30+
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USERNAME} -d ${POSTGRES_DATABASE}"]
31+
interval: 10s
32+
timeout: 10s
33+
retries: 5
34+
start_period: 30s
35+
env_file:
36+
- .env
37+
38+
redis:
39+
image: redis:7-alpine
40+
ports:
41+
- "6379:6379"
42+
volumes:
43+
- redis_data:/data
44+
45+
46+
antifraud:
47+
image: lodthe/prod-backend-antifraud:latest
48+
environment:
49+
SERVER_PORT: ${ANTIFRAUD_INTERNAL_PORT}
50+
CACHE_DURATION_MS: ${ANTIFRAUD_CACHE_MS}
51+
SLOWDOWN_AFTER: ${ANTIFRAUD_SLOWDOWN_AFTER}
52+
BLOCKED_EMAILS: ${ANTIFRAUD_BLOCKED_EMAILS}
53+
ports:
54+
- "${ANTIFRAUD_EXTERNAL_PORT}:${ANTIFRAUD_INTERNAL_PORT}"
55+
56+
volumes:
57+
postgres_db:
58+
redis_data:

promo_code/promo_code/settings.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ def load_bool(name, default):
141141
},
142142
}
143143

144+
CACHES = {
145+
'default': {
146+
'BACKEND': 'django_redis.cache.RedisCache',
147+
'LOCATION': os.getenv('REDIS_URL', 'redis://127.0.0.1:6379/0'),
148+
'OPTIONS': {
149+
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
150+
},
151+
},
152+
}
153+
154+
ANTIFRAUD_ADDRESS = f'{os.getenv("ANTIFRAUD_ADDRESS")}'
155+
ANTIFRAUD_VALIDATE_URL = f'{ANTIFRAUD_ADDRESS}/api/validate'
156+
ANTIFRAUD_UPDATE_USER_VERDICT_URL = (
157+
f'{ANTIFRAUD_ADDRESS}/internal/update_user_verdict'
158+
)
144159

145160
AUTH_PASSWORD_VALIDATORS = [
146161
{

requirements/prod.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
django==5.2
2+
django-redis==6.0.0
23
djangorestframework==3.15.2
34
djangorestframework-simplejwt==5.4.0
45
gunicorn==23.0.0
56
psycopg2-binary==2.9.10
67
pycountry==24.6.1
78
python-dotenv==1.0.1
9+
requests==2.32.4
10+
parameterized==0.9.0

0 commit comments

Comments
 (0)