added docker containerization

main
zacc806 2024-01-18 17:30:31 +06:00
parent a51aec0556
commit b2135361fb
7 changed files with 75 additions and 8 deletions

7
.dockerignore Normal file
View File

@ -0,0 +1,7 @@
__pycache__
*.pyc
*.pyo
*.pyd
.Python
env
*.sqlite3

30
Dockerfile Normal file
View File

@ -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"]

View File

@ -8,7 +8,7 @@ SECRET_KEY = 'django-insecure-&!4%-vjbqun^7idhr9ov$3*!233xczz4zt4i1bj_x&ur14makw
DEBUG = True DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1', 'localhost'] ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [ INSTALLED_APPS = [
'django.contrib.admin', 'django.contrib.admin',
@ -130,7 +130,7 @@ STATIC_URL = '/static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
#python -Xutf8 manage.py dumpdata -o db.json #python -Xutf8 manage.py dumpdata -o db.json
#python manage.py loaddata db.json
STATIC_URL = '/static/' STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / '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_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
# AWS_STORAGE_BUCKET_NAME = 'e--shop-bucket' # AWS_STORAGE_BUCKET_NAME = 'e--shop-bucket'
if os.getcwd() == '/app' :
DEBUG = False

View File

@ -13,4 +13,8 @@ urlpatterns = [
] ]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 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)

16
docker-compose.yml Normal file
View File

@ -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:

View File

@ -57,7 +57,7 @@ class Product(models.Model):
count = int(reviews['count']) count = int(reviews['count'])
return 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]) return reverse('shop:product_details', args=[self.category.slug, self.slug])
class Meta: class Meta:

13
start.sh Normal file
View File

@ -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