From a267e4c45cafe845af5dece057197165d9eb683e Mon Sep 17 00:00:00 2001 From: behruz-dev Date: Sun, 16 Nov 2025 10:43:32 +0500 Subject: [PATCH] adding docker and docker compose file to running project, configurate nginx.conf file for DNS --- .dockerignore | 1 + config/settings.py | 2 +- docker-compose.yaml | 74 ++++++++++++++++++++++++++ docker/Dockerfile.nginx | 3 ++ docker/Dockerfile.web | 25 +++++++++ requirements.txt | 4 ++ resources/layout/nginx.conf | 51 ++++++++++++++++++ resources/scripts/backup.sh | 5 ++ resources/scripts/entrypoint-server.sh | 8 +++ resources/scripts/entrypoint.sh | 7 +++ 10 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 docker/Dockerfile.nginx create mode 100644 docker/Dockerfile.web create mode 100644 requirements.txt create mode 100644 resources/layout/nginx.conf create mode 100644 resources/scripts/backup.sh create mode 100644 resources/scripts/entrypoint-server.sh create mode 100644 resources/scripts/entrypoint.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f7275bb --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +venv/ diff --git a/config/settings.py b/config/settings.py index c5c5817..b77d45b 100644 --- a/config/settings.py +++ b/config/settings.py @@ -115,7 +115,7 @@ USE_TZ = True # https://docs.djangoproject.com/en/5.2/howto/static-files/ STATIC_URL = 'static/' - +STATIC_ROOT = BASE_DIR / 'resources/static' # Default primary key field type # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field diff --git a/docker-compose.yaml b/docker-compose.yaml index e69de29..9cbba19 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -0,0 +1,74 @@ +services: + nginx: + networks: + - uyqur + ports: + - ${PORT:-8001}:80 + volumes: + - ./resources/layout/nginx.conf:/etc/nginx/nginx.conf + - ./resources/:/usr/share/nginx/html/resources/ + build: + context: . + dockerfile: ./docker/Dockerfile.nginx + depends_on: + - web + restart: always + + web: + networks: + - uyqur + build: + context: . + dockerfile: ./docker/Dockerfile.web + command: ${COMMAND:-sh ./resources/scripts/entrypoint.sh} + environment: + - PYTHONPYCACHEPREFIX=/var/cache/pycache + volumes: + - './:/code' + depends_on: + - db + - redis + restart: always + + db: + image: postgres:17 + networks: + - uyqur + environment: + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_DB: ${POSTGRES_DB} + volumes: + - pg_data:/var/lib/postgresql/data + restart: always + + redis: + networks: + - uyqur + image: redis + restart: always + + # celery: + # build: + # context: . + # dockerfile: ./docker/Dockerfile.web + # command: celery -A config worker --loglevel=info + # volumes: + # - "./:/code" + # depends_on: + # - redis + # - web + # networks: + # - uyqur + # environment: + # - CELERY_BROKER_URL=${CELERY_BROKER_URL:-redis://redis:6379/0} + # - CELERY_RESULT_BACKEND=${CELERY_RESULT_BACKEND:-redis://redis:6379/0} + # restart: always + +volumes: + pg_data: null + + +networks: + uyqur: + driver: bridge \ No newline at end of file diff --git a/docker/Dockerfile.nginx b/docker/Dockerfile.nginx new file mode 100644 index 0000000..42738e3 --- /dev/null +++ b/docker/Dockerfile.nginx @@ -0,0 +1,3 @@ +FROM nginx:alpine + +COPY ./resources/layout/nginx.conf /etc/nginx/nginx.conf \ No newline at end of file diff --git a/docker/Dockerfile.web b/docker/Dockerfile.web new file mode 100644 index 0000000..1f375d5 --- /dev/null +++ b/docker/Dockerfile.web @@ -0,0 +1,25 @@ +FROM python:3.12 + +WORKDIR /code + +RUN apt-get update && \ + apt-get install -y \ + gdal-bin \ + libgdal-dev \ + python3-gdal \ + libgeos-dev \ + libproj-dev \ + g++ \ + wget \ + libfontconfig \ + libxrender1 \ + libjpeg-dev \ + xfonts-base && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + + +COPY ./ /code +RUN --mount=type=cache,target=/root/.cache/pip python3 -m pip install -r requirements.txt + +CMD ["sh", "./resources/scripts/entrypoint.sh"] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..54c5cde --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +Django==5.2 +gunicorn==23.0.0 +uvicorn==0.38.0 +celery \ No newline at end of file diff --git a/resources/layout/nginx.conf b/resources/layout/nginx.conf new file mode 100644 index 0000000..8a23271 --- /dev/null +++ b/resources/layout/nginx.conf @@ -0,0 +1,51 @@ +worker_processes 1; + +events { + worker_connections 1024; +} + +http { + include mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + access_log /var/log/nginx/access.log main; + + sendfile on; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; + client_max_body_size 1024M; + + server { + listen 80; + + server_name _; + + location / { + proxy_pass http://web:8000; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto https; + proxy_set_header Host $http_host; + } + location /ws/ { + proxy_pass http://web:8000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $http_host; + } + + location /resources/static/ { + alias /usr/share/nginx/html/resources/staticfiles/; + } + + location /resources/media/ { + alias /usr/share/nginx/html/resources/media/; + } + } +} \ No newline at end of file diff --git a/resources/scripts/backup.sh b/resources/scripts/backup.sh new file mode 100644 index 0000000..5bac180 --- /dev/null +++ b/resources/scripts/backup.sh @@ -0,0 +1,5 @@ +file=/tmp/db-$(/usr/bin/date +\%Y-%m-%d-%H:%M:%S).sql +container=postgres +/usr/bin/docker container exec $container pg_dump -U postgres django > $file +mc cp $file b2/buket-name +rm $file \ No newline at end of file diff --git a/resources/scripts/entrypoint-server.sh b/resources/scripts/entrypoint-server.sh new file mode 100644 index 0000000..b1c9cb5 --- /dev/null +++ b/resources/scripts/entrypoint-server.sh @@ -0,0 +1,8 @@ +#!/bin/bash +python3 manage.py collectstatic --noinput +python3 manage.py migrate --noinput + +gunicorn config.wsgi:application -b 0.0.0.0:8000 --workers $(($(nproc) * 2 + 1)) + + +exit $? \ No newline at end of file diff --git a/resources/scripts/entrypoint.sh b/resources/scripts/entrypoint.sh new file mode 100644 index 0000000..46e4391 --- /dev/null +++ b/resources/scripts/entrypoint.sh @@ -0,0 +1,7 @@ +#!/bin/bash +python3 manage.py collectstatic --noinput +python3 manage.py migrate --noinput + +uvicorn config.asgi:application --host 0.0.0.0 --port 8000 --reload --reload-dir apps --reload-dir config + +exit $?