Skip to content

Commit

Permalink
add Docker
Browse files Browse the repository at this point in the history
  • Loading branch information
Edmaroff committed Mar 28, 2024
1 parent 04b9f87 commit 8a6d1b4
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ instance/
local_settings.py
db.sqlite3
db.sqlite3-journal
static/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
61 changes: 61 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
version: '3.9'

volumes:
postgres_volume:
static_volume:
media_volume:

services:
# Database Postgres
db:
image: postgres:latest
container_name: postgres
env_file:
- ./retail_order_api/.env
volumes:
- postgres_volume:/var/lib/postgresql/data/
ports:
- "5432:5432"

# Django App
web:
build: ./retail_order_api
container_name: retail_order_api
ports:
- "8000:8000"
env_file:
- ./retail_order_api/.env
volumes:
- static_volume:/usr/src/app/static
- media_volume:/usr/src/app/media
depends_on:
- db
command: sh /usr/src/app/entrypoint.sh

# web-server
nginx:
build: ./nginx
container_name: nginx
restart: on-failure
ports:
- '1337:80'
volumes:
- static_volume:/static
- media_volume:/media
depends_on:
- web

# Redis
redis:
image: redis:latest
container_name: redis
ports:
- "6300:6379"

# Celery
celery:
build: ./retail_order_api
command: celery -A retail_order_api worker -l info
depends_on:
- redis
- db
3 changes: 3 additions & 0 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM nginx:1.24-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
30 changes: 30 additions & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
upstream retail-order {
server retail_order_api:8000;
}

server {

listen 80;
server_name localhost;

client_max_body_size 100M;
proxy_force_ranges on;
add_header Accept-Ranges bytes;

location / {
proxy_ignore_client_abort on;
proxy_pass http://retail-order;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}


location /static/ {
alias /static/;
}

location /media/ {
alias /media/;
}
}
27 changes: 27 additions & 0 deletions retail_order_api/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
### main_dockerignore template
# Personal
manual.md
initial_task.md
README.md
static/

# Git
.gitignore
.git

# Environments
venv_wsl
#.env
.venv
env/
venv/
venv_wsl/
ENV/
env.bak/
venv.bak/

# PyCharm
.idea/

# Byte-compiled / optimized / DLL files
__pycache__/
12 changes: 6 additions & 6 deletions retail_order_api/.env.template
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Django
SECRET_KEY=your_secret_key
DEBUG=True
DEBUG=True#Для Docker =True
ALLOWED_HOSTS=localhost,127.0.0.1

# PostgreSQL
ALLOWED_HOSTS=localhost,127.0.0.1
DB_ENGINE=django.db.backends.postgresql
DB_NAME=your_db_name
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_HOST=your_db_host
DB_PORT=your_db_port
DB_HOST=your_db_host#Для Docker =db
DB_PORT=your_db_port#Для Docker =5432

# SMTP
EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
Expand All @@ -28,5 +28,5 @@ SOCIAL_AUTH_GITHUB_KEY=your_github_key
SOCIAL_AUTH_GITHUB_SECRET=your_github_secret

# Celery
CELERY_BROKER_URL=your_celery_broker_url
CELERY_RESULT_BACKEND=your_celery_result_backend
CELERY_BROKER_URL=your_celery_broker_url#Для Docker =redis://redis:6379
CELERY_RESULT_BACKEND=your_celery_result_backend#Для Docker =redis://redis:6379
13 changes: 13 additions & 0 deletions retail_order_api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.12-alpine

WORKDIR /usr/src/app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERD 1

COPY requirements.txt /usr/src/app/requirements.txt

RUN python3 -m pip install --upgrade pip
RUN pip3 install -r requirements.txt

COPY . /usr/src/app
2 changes: 1 addition & 1 deletion retail_order_api/backend/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CustomUserAdmin(UserAdmin):
),
(
_("Permissions"),
{"fields": ("is_superuser", "groups", "user_permissions")},
{"fields": ("is_active", "is_superuser", "groups", "user_permissions")},
),
(_("Important dates"), {"fields": ("last_login", "date_joined")}),
]
Expand Down
2 changes: 1 addition & 1 deletion retail_order_api/backend/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.3 on 2024-03-22 04:40
# Generated by Django 5.0.3 on 2024-03-28 09:41

import django.contrib.auth.validators
import django.core.validators
Expand Down
3 changes: 3 additions & 0 deletions retail_order_api/backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ def create_user(self, email, password=None, **extra_fields):

def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault("is_superuser", True)
extra_fields.setdefault("is_staff", True)

if extra_fields.get("is_superuser") is not True:
raise ValueError("Суперпользователь должен иметь is_superuser=True.")
if extra_fields.get("is_staff") is not True:
raise ValueError("Суперпользователь должен иметь is_staff=True.")

return self._create_user(email, password, **extra_fields)

Expand Down
10 changes: 10 additions & 0 deletions retail_order_api/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

python manage.py makemigrations --noinput
python manage.py migrate --noinput
echo "Apply database migrations"
python manage.py shell -c "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.filter(username='root').exists() or User.objects.create_superuser('[email protected]', 'root')"
echo "Create SUPERUSER"
python manage.py collectstatic --no-input
echo "Starting server"
gunicorn retail_order_api.wsgi:application --bind 0.0.0.0:8000
Binary file modified retail_order_api/requirements.txt
Binary file not shown.
14 changes: 6 additions & 8 deletions retail_order_api/retail_order_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,14 @@

WSGI_APPLICATION = "retail_order_api.wsgi.application"
ROOT_URLCONF = "retail_order_api.urls"

DATABASES = {
"default": {
"ENGINE": env.str("DB_ENGINE", "django.db.backends.sqlite3"),
"NAME": env.str("DB_NAME", os.path.join(BASE_DIR, "db.sqlite3")),
"USER": env.str("DB_USER", "user"),
"PASSWORD": env.str("DB_PASSWORD", "password"),
"HOST": env.str("DB_HOST", "localhost"),
"PORT": env.str("DB_PORT", "5432"),
"ENGINE": env.str("POSTGRES_ENGINE", "django.db.backends.sqlite3"),
"NAME": env.str("POSTGRES_DB", os.path.join(BASE_DIR, "db.sqlite3")),
"USER": env.str("POSTGRES_USER", "user"),
"PASSWORD": env.str("POSTGRES_PASSWORD", "password"),
"HOST": env.str("POSTGRES_HOST", "localhost"),
"PORT": env.str("POSTGRES_PORT", "5432"),
}
}

Expand Down Expand Up @@ -210,7 +209,6 @@
CELERY_RESULT_BACKEND = env.str("CELERY_RESULT_BACKEND", "redis://localhost:6379")
CELERY_BROKER_URL = env.str("CELERY_BROKER_URL", "redis://localhost:6379")


# django-baton
BATON = {
"SITE_HEADER": "retail-order-api",
Expand Down
2 changes: 1 addition & 1 deletion retail_order_api/tests/backend/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def test_delete_shop(self, authenticated_client_shop, shop_factory):
class TestProductDetailView:
"""Тесты для ProductDetailView."""

url = reverse("backend:products_detail")
url = reverse("backend:products_in_shops")

@staticmethod
def assert_response(response, expected_ids):
Expand Down

0 comments on commit 8a6d1b4

Please sign in to comment.