-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Django project setup with Docker configuration and production settings
- Loading branch information
Showing
15 changed files
with
302 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
# Use Python 3.10 slim image | ||
FROM python:3.10-slim | ||
|
||
# Set environment variables | ||
ENV PYTHONDONTWRITEBYTECODE 1 | ||
ENV PYTHONUNBUFFERED 1 | ||
ENV DEBIAN_FRONTEND noninteractive | ||
|
||
# Install system dependencies | ||
RUN apt-get update && apt-get install -y \ | ||
gcc \ | ||
libpq-dev \ | ||
docker.io \ | ||
yara \ | ||
libmagic1 \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
# Set work directory | ||
# Set working directory | ||
WORKDIR /app | ||
|
||
# Install Python dependencies | ||
# Copy requirements file | ||
COPY requirements.txt . | ||
|
||
# Install Python dependencies | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
|
||
# Copy project | ||
COPY . . | ||
# Copy project files | ||
COPY . /app/ | ||
|
||
# Create necessary directories | ||
RUN mkdir -p /app/logs /app/uploads /app/instance | ||
RUN mkdir -p /app/static /app/media | ||
|
||
# Set permissions | ||
RUN chmod -R 755 /app | ||
|
||
# Run gunicorn | ||
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "4", "--threads", "4", "app:app"] | ||
# Collect static files | ||
RUN python manage.py collectstatic --noinput | ||
|
||
# Expose port | ||
EXPOSE 8000 | ||
|
||
# Command to run the application | ||
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "ghostsec.wsgi:application"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,58 @@ | ||
version: '3.8' | ||
|
||
services: | ||
web: | ||
build: . | ||
ports: | ||
- "0.0.0.0:5000:5000" | ||
command: gunicorn --bind 0.0.0.0:8000 ghostsec.wsgi:application | ||
volumes: | ||
- static_volume:/app/static | ||
- media_volume:/app/media | ||
environment: | ||
- FLASK_APP=app.py | ||
- FLASK_ENV=production | ||
- DATABASE_URL=postgresql://postgres:postgres@db:5432/ghostsec | ||
- DEBUG=0 | ||
- POSTGRES_DB=ghostsec | ||
- POSTGRES_USER=postgres | ||
- POSTGRES_PASSWORD=postgres | ||
- POSTGRES_HOST=db | ||
- POSTGRES_PORT=5432 | ||
- REDIS_URL=redis://redis:6379/0 | ||
- HOST=0.0.0.0 | ||
volumes: | ||
- .:/app | ||
- /var/run/docker.sock:/var/run/docker.sock | ||
depends_on: | ||
- db | ||
- redis | ||
networks: | ||
- ghostsec_network | ||
|
||
db: | ||
image: postgres:15 | ||
image: postgres:13 | ||
volumes: | ||
- postgres_data:/var/lib/postgresql/data | ||
environment: | ||
- POSTGRES_DB=ghostsec | ||
- POSTGRES_USER=postgres | ||
- POSTGRES_PASSWORD=postgres | ||
- POSTGRES_DB=ghostsec | ||
volumes: | ||
- postgres_data:/var/lib/postgresql/data | ||
ports: | ||
- "5432:5432" | ||
networks: | ||
- ghostsec_network | ||
|
||
redis: | ||
image: redis:7 | ||
volumes: | ||
- redis_data:/data | ||
ports: | ||
- "6379:6379" | ||
image: redis:6 | ||
networks: | ||
- ghostsec_network | ||
|
||
nginx: | ||
image: nginx:latest | ||
ports: | ||
- "0.0.0.0:80:80" | ||
- "0.0.0.0:443:443" | ||
image: nginx:1.19 | ||
volumes: | ||
- ./nginx.conf:/etc/nginx/nginx.conf:ro | ||
- ./ssl:/etc/nginx/ssl:ro | ||
- ./nginx/conf.d:/etc/nginx/conf.d:ro | ||
- static_volume:/app/static | ||
- media_volume:/app/media | ||
ports: | ||
- "80:80" | ||
depends_on: | ||
- web | ||
networks: | ||
- ghostsec_network | ||
|
||
networks: | ||
ghostsec_network: | ||
driver: bridge | ||
|
||
volumes: | ||
postgres_data: | ||
redis_data: | ||
static_volume: | ||
media_volume: |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
""" | ||
Django settings for ghostsec project. | ||
""" | ||
|
||
import os | ||
from pathlib import Path | ||
|
||
# Build paths inside the project like this: BASE_DIR / 'subdir'. | ||
BASE_DIR = Path(__file__).resolve().parent.parent | ||
|
||
# SECURITY WARNING: keep the secret key used in production secret! | ||
SECRET_KEY = os.environ.get('SECRET_KEY', 'django-insecure-your-secret-key-here') | ||
|
||
# SECURITY WARNING: don't run with debug turned on in production! | ||
DEBUG = os.environ.get('DEBUG', '0') == '1' | ||
|
||
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', 'localhost 127.0.0.1').split(' ') | ||
|
||
# Application definition | ||
INSTALLED_APPS = [ | ||
'django.contrib.admin', | ||
'django.contrib.auth', | ||
'django.contrib.contenttypes', | ||
'django.contrib.sessions', | ||
'django.contrib.messages', | ||
'django.contrib.staticfiles', | ||
'ghostsec_auth', | ||
'ctf', | ||
'forum', | ||
'learning', | ||
'learning_environments', | ||
'main', | ||
'marketplace', | ||
'news', | ||
'oauth', | ||
] | ||
|
||
MIDDLEWARE = [ | ||
'django.middleware.security.SecurityMiddleware', | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.middleware.common.CommonMiddleware', | ||
'django.middleware.csrf.CsrfViewMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'django.contrib.messages.middleware.MessageMiddleware', | ||
'django.middleware.clickjacking.XFrameOptionsMiddleware', | ||
] | ||
|
||
ROOT_URLCONF = 'ghostsec.urls' | ||
|
||
TEMPLATES = [ | ||
{ | ||
'BACKEND': 'django.template.backends.django.DjangoTemplates', | ||
'DIRS': [os.path.join(BASE_DIR, 'templates')], | ||
'APP_DIRS': True, | ||
'OPTIONS': { | ||
'context_processors': [ | ||
'django.template.context_processors.debug', | ||
'django.template.context_processors.request', | ||
'django.contrib.auth.context_processors.auth', | ||
'django.contrib.messages.context_processors.messages', | ||
], | ||
}, | ||
}, | ||
] | ||
|
||
WSGI_APPLICATION = 'ghostsec.wsgi.application' | ||
|
||
# Database | ||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.postgresql', | ||
'NAME': os.environ.get('POSTGRES_DB', 'ghostsec'), | ||
'USER': os.environ.get('POSTGRES_USER', 'postgres'), | ||
'PASSWORD': os.environ.get('POSTGRES_PASSWORD', 'postgres'), | ||
'HOST': os.environ.get('POSTGRES_HOST', 'db'), | ||
'PORT': os.environ.get('POSTGRES_PORT', '5432'), | ||
} | ||
} | ||
|
||
# Cache | ||
CACHES = { | ||
'default': { | ||
'BACKEND': 'django_redis.cache.RedisCache', | ||
'LOCATION': os.environ.get('REDIS_URL', 'redis://redis:6379/0'), | ||
'OPTIONS': { | ||
'CLIENT_CLASS': 'django_redis.client.DefaultClient', | ||
} | ||
} | ||
} | ||
|
||
# Password validation | ||
AUTH_PASSWORD_VALIDATORS = [ | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', | ||
}, | ||
] | ||
|
||
# Internationalization | ||
LANGUAGE_CODE = 'en-us' | ||
TIME_ZONE = 'UTC' | ||
USE_I18N = True | ||
USE_TZ = True | ||
|
||
# Static files (CSS, JavaScript, Images) | ||
STATIC_URL = '/static/' | ||
STATIC_ROOT = os.path.join(BASE_DIR, 'static') | ||
|
||
# Media files | ||
MEDIA_URL = '/media/' | ||
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') | ||
|
||
# Default primary key field type | ||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' | ||
|
||
# Security settings | ||
if not DEBUG: | ||
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') | ||
SECURE_SSL_REDIRECT = True | ||
SESSION_COOKIE_SECURE = True | ||
CSRF_COOKIE_SECURE = True | ||
SECURE_BROWSER_XSS_FILTER = True | ||
SECURE_CONTENT_TYPE_NOSNIFF = True | ||
X_FRAME_OPTIONS = 'DENY' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""ghostsec URL Configuration""" | ||
from django.contrib import admin | ||
from django.urls import path | ||
from django.http import JsonResponse | ||
|
||
def health_check(request): | ||
return JsonResponse({"status": "healthy"}) | ||
|
||
urlpatterns = [ | ||
path('admin/', admin.site.urls), | ||
path('health/', health_check, name='health_check'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
""" | ||
WSGI config for ghostsec project. | ||
""" | ||
|
||
import os | ||
|
||
from django.core.wsgi import get_wsgi_application | ||
|
||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ghostsec.settings') | ||
|
||
application = get_wsgi_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env python | ||
"""Django's command-line utility for administrative tasks.""" | ||
import os | ||
import sys | ||
|
||
|
||
def main(): | ||
"""Run administrative tasks.""" | ||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ghostsec.settings') | ||
try: | ||
from django.core.management import execute_from_command_line | ||
except ImportError as exc: | ||
raise ImportError( | ||
"Couldn't import Django. Are you sure it's installed? Did you " | ||
"forget to activate a virtual environment?" | ||
) from exc | ||
execute_from_command_line(sys.argv) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.