From beeedcd82132857f0f9fd6d65dd2d18820a742ff Mon Sep 17 00:00:00 2001 From: SeyedMojtaba1 Date: Fri, 24 Oct 2025 13:22:27 +0330 Subject: [PATCH] Django docker-compose and Dockerfile. --- Dockerfile | 20 ++++++++++++- app/config/settings.py | 11 +++---- docker-compose.yml | 67 +++++++++++++++++++++++++++++++++++++++++- nginx/nginx.conf | 29 +++++++++++++++++- 4 files changed, 119 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 26a8f8a..7a5d5f0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1 +1,19 @@ -# ADD DOCKERFILE CONTENT HERE \ No newline at end of file +FROM python:3.12-slim + +WORKDIR /app + +COPY requirements.txt . + +RUN apt-get update && apt-get install -y \ + build-essential \ + libpq-dev \ + gcc \ + && rm -rf /var/lib/apt/lists/* + +RUN python -m venv /venv && /venv/bin/pip install --upgrade pip +RUN /venv/bin/pip install -r requirements.txt + +COPY . . + + +EXPOSE 8000 diff --git a/app/config/settings.py b/app/config/settings.py index 77ada4c..2eef93b 100644 --- a/app/config/settings.py +++ b/app/config/settings.py @@ -17,9 +17,10 @@ SECRET_KEY = env('SECRET_KEY', default='django-insecure-change-this-key') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = env('DEBUG') +DEBUG = True +#env('DEBUG') -ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', default=['localhost', '127.0.0.1']) +ALLOWED_HOSTS = ['localhost', '127.0.0.1', '*'] # Application definition INSTALLED_APPS = [ @@ -29,9 +30,9 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - + # Third party apps - + # Local apps 'core', ] @@ -137,4 +138,4 @@ STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' # Default primary key field type -DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' \ No newline at end of file +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/docker-compose.yml b/docker-compose.yml index a3437d1..302914d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1 +1,66 @@ -# ADD Docker Compose CONTENT HERE \ No newline at end of file +version: '3.9' + +services: + + redis: + image: docker.arvancloud.ir/redis:alpine3.22 + restart: on-failure + container_name: redis + ports: + - "6379:6379" + volumes: + - redis_data:/data + + db: + image: docker.arvancloud.ir/postgres:16.10-alpine + container_name: django_postgresql + environment: + POSTGRES_DB: djangodb + POSTGRES_USER: djangouser + POSTGRES_PASSWORD: djangopass + POSTGRES_HOST: db + POSTGRES_PORT: 5432 + volumes: + - db_data:/var/lib/postgresql/data + ports: + - "5432:5432" + healthcheck: + test: ["CMD-SHELL", "pg_isready -h localhost -U $POSTGRES_USER || exit 1"] + interval: 10s + timeout: 5s + retries: 5 + + django: + build: . + container_name: django_project + command: sh -c "cd /app/app && + /venv/bin/gunicorn --bind 0.0.0.0:8000 config.wsgi:application && + celery -A MediCaseBack worker -l info -P solo" + volumes: + - .:/app + - static_volume:/app/staticfiles + - media_volume:/app/media + ports: + - "8000:8000" + depends_on: + redis: + condition: service_started + db: + condition: service_started + + nginx: + container_name: web-server + image: nginx:stable-perl + ports: + - "80:80" + volumes: + - ./nginx/nginx.conf:/etc/nginx/nginx.conf + restart: on-failure + depends_on: + - django + +volumes: + db_data: + redis_data: + static_volume: + media_volume: diff --git a/nginx/nginx.conf b/nginx/nginx.conf index ed468cf..8fb7c65 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -1 +1,28 @@ -# ADD NGINX CONTENT HERE \ No newline at end of file +user nginx; +worker_processes auto; +pid /run/nginx.pid; + +events { + worker_connections 2048; +} + +http{ + upstream django_project { + server django:8000; + } + + server{ + listen 80 default_server; + server_name _; + access_log /var/log/nginx/access.log; + error_log /var/log/nginx/error.log; + + location / { + proxy_pass http://django_project; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_redirect off; + } + } +}