From b2135361fba2b45fbe54e2865ce54389385708da Mon Sep 17 00:00:00 2001 From: zacc806 Date: Thu, 18 Jan 2024 17:30:31 +0600 Subject: [PATCH] added docker containerization --- .dockerignore | 7 +++++++ Dockerfile | 30 ++++++++++++++++++++++++++++++ core/settings.py | 9 +++------ core/urls.py | 6 +++++- docker-compose.yml | 16 ++++++++++++++++ shop/models.py | 2 +- start.sh | 13 +++++++++++++ 7 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 start.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..824f589 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +__pycache__ +*.pyc +*.pyo +*.pyd +.Python +env +*.sqlite3 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6054ca6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +# Use an official Python runtime as a parent image +FROM python:3.10 + +# Set environment variables +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +# Set the working directory in the container +WORKDIR /app + +# Copy the current directory contents into the container at /app +COPY . /app + +# Install any needed packages specified in requirements.txt +RUN pip install --upgrade pip +RUN pip install --no-cache-dir -r requirements.txt +RUN python manage.py collectstatic --noinput + +# Copy the startup script +COPY start.sh /app/ +RUN chmod +x /app/start.sh + +# Make port 8000 available to the world outside this container +EXPOSE 8000 + +# Define environment variable +ENV NAME World + +# Run manage.py when the container launches +CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] diff --git a/core/settings.py b/core/settings.py index 25bd0df..d441b9f 100644 --- a/core/settings.py +++ b/core/settings.py @@ -8,7 +8,7 @@ SECRET_KEY = 'django-insecure-&!4%-vjbqun^7idhr9ov$3*!233xczz4zt4i1bj_x&ur14makw DEBUG = True -ALLOWED_HOSTS = ['127.0.0.1', 'localhost'] +ALLOWED_HOSTS = ['*'] INSTALLED_APPS = [ 'django.contrib.admin', @@ -130,7 +130,7 @@ STATIC_URL = '/static/' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' #python -Xutf8 manage.py dumpdata -o db.json -#python manage.py loaddata db.json + STATIC_URL = '/static/' STATIC_ROOT = BASE_DIR / 'static' @@ -166,7 +166,4 @@ PASSWORD_RESET_TIMEOUT_DAYS = 1 # AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY') -# AWS_STORAGE_BUCKET_NAME = 'e--shop-bucket' - -if os.getcwd() == '/app' : - DEBUG = False \ No newline at end of file +# AWS_STORAGE_BUCKET_NAME = 'e--shop-bucket' \ No newline at end of file diff --git a/core/urls.py b/core/urls.py index cfe1982..a81e41b 100644 --- a/core/urls.py +++ b/core/urls.py @@ -13,4 +13,8 @@ urlpatterns = [ ] urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) -urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) \ No newline at end of file +urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + +if settings.DEBUG: + urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..5c4e65a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +version: '3' + +services: + web: + build: . + command: sh -c "/app/start.sh" + volumes: + - .:/app + - ./media:/app/media + ports: + - "8000:8000" + environment: + - DEBUG=1 + +volumes: + media: \ No newline at end of file diff --git a/shop/models.py b/shop/models.py index e49bf6b..9ec5348 100644 --- a/shop/models.py +++ b/shop/models.py @@ -57,7 +57,7 @@ class Product(models.Model): count = int(reviews['count']) return count - def get_prodcut_details_url(self): + def get_product_details_url(self): return reverse('shop:product_details', args=[self.category.slug, self.slug]) class Meta: diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..a74e3ae --- /dev/null +++ b/start.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +# Apply database migrations +echo "Applying database migrations..." +python manage.py migrate + +# Load data from db.json +echo "Loading data from db.json..." +python manage.py loaddata db.json + +# Start the Django development server +echo "Starting Django server..." +python manage.py runserver 0.0.0.0:8000 --insecure \ No newline at end of file