├── Dockerfile ├── README.md ├── docker-compose.yml ├── my_project ├── manage.py ├── my_app │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── models.py │ ├── urls.py │ └── views.py └── my_project │ ├── __init__.py │ ├── asgi.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── prometheus └── prometheus.yml └── requirements.txt /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.8.5-alpine 2 | 3 | RUN pip install --upgrade pip 4 | COPY ./requirements.txt . 5 | RUN pip install -r requirements.txt 6 | 7 | COPY ./my_project /app 8 | 9 | WORKDIR /app 10 | 11 | RUN python manage.py migrate 12 | 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Django/ Prometheus/ Docker Compose Example 2 | 3 | 4 | ### Description 5 | 6 | A setup to run 3 Docker containers (2 Django services and 1 Prometheus service) using Docker Compose. The Prometheus container is used to monitor the 2 Django apps. 7 | 8 | The Prometheus container runs at port 9090. 9 | 10 | Django "app1" runs at port 8000. 11 | 12 | Django "app2" runs at port 8080. 13 | 14 | 15 | ### How to Run? 16 | 17 | ``` 18 | docker-compose -f docker-compose.yml up 19 | ``` 20 | 21 | Check: ```127.0.0.1:9090```, ```127.0.0.1:8000``` and ```127.0.0.1:8080``` 22 | 23 | For Prometheus: 24 | 25 | Go to ```status``` then ```targets``` to find the Django services. 26 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.7' 2 | 3 | services: 4 | app_1: 5 | container_name: app1 6 | build: 7 | context: . 8 | dockerfile: ./Dockerfile 9 | ports: 10 | - "8000:8000" 11 | command: python manage.py runserver 0.0.0.0:8000 12 | 13 | app_2: 14 | container_name: app2 15 | build: 16 | context: . 17 | dockerfile: ./Dockerfile 18 | ports: 19 | - "8080:8080" 20 | command: python manage.py runserver 0.0.0.0:8080 21 | 22 | prometheus: 23 | image: prom/prometheus 24 | volumes: 25 | - ./prometheus/:/etc/prometheus/ 26 | ports: 27 | - "9090:9090" 28 | -------------------------------------------------------------------------------- /my_project/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /my_project/my_app/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dotja/django-prometheus-docker/d87e9c00683823ca39feeadb4c30b1664eba9f1a/my_project/my_app/__init__.py -------------------------------------------------------------------------------- /my_project/my_app/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /my_project/my_app/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class MyAppConfig(AppConfig): 5 | name = 'my_app' 6 | -------------------------------------------------------------------------------- /my_project/my_app/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | -------------------------------------------------------------------------------- /my_project/my_app/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | from . import views 3 | 4 | urlpatterns = [ 5 | path('', views.home, name='home'), 6 | ] -------------------------------------------------------------------------------- /my_project/my_app/views.py: -------------------------------------------------------------------------------- 1 | from django.http import HttpResponse 2 | 3 | def home(request): 4 | html = "

Hello!

" 5 | return HttpResponse(html) 6 | -------------------------------------------------------------------------------- /my_project/my_project/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dotja/django-prometheus-docker/d87e9c00683823ca39feeadb4c30b1664eba9f1a/my_project/my_project/__init__.py -------------------------------------------------------------------------------- /my_project/my_project/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for my_project project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /my_project/my_project/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for my_project project. 3 | 4 | Generated by 'django-admin startproject' using Django 3.1.6. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/3.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = '25t)mpn4*fo7b#f(3_n(vram9q332pz*-gduo!+$j98z5-z#z#' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = ['*'] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'my_app', 41 | 'django_prometheus', 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django_prometheus.middleware.PrometheusBeforeMiddleware', 46 | 'django.middleware.security.SecurityMiddleware', 47 | 'django.contrib.sessions.middleware.SessionMiddleware', 48 | 'django.middleware.common.CommonMiddleware', 49 | 'django.middleware.csrf.CsrfViewMiddleware', 50 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 51 | 'django.contrib.messages.middleware.MessageMiddleware', 52 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 53 | 'django_prometheus.middleware.PrometheusAfterMiddleware', 54 | 55 | ] 56 | 57 | ROOT_URLCONF = 'my_project.urls' 58 | 59 | TEMPLATES = [ 60 | { 61 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 62 | 'DIRS': [], 63 | 'APP_DIRS': True, 64 | 'OPTIONS': { 65 | 'context_processors': [ 66 | 'django.template.context_processors.debug', 67 | 'django.template.context_processors.request', 68 | 'django.contrib.auth.context_processors.auth', 69 | 'django.contrib.messages.context_processors.messages', 70 | ], 71 | }, 72 | }, 73 | ] 74 | 75 | WSGI_APPLICATION = 'my_project.wsgi.application' 76 | 77 | 78 | # Database 79 | # https://docs.djangoproject.com/en/3.1/ref/settings/#databases 80 | 81 | DATABASES = { 82 | 'default': { 83 | 'ENGINE': 'django.db.backends.sqlite3', 84 | 'NAME': BASE_DIR / 'db.sqlite3', 85 | } 86 | } 87 | 88 | 89 | # Password validation 90 | # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators 91 | 92 | AUTH_PASSWORD_VALIDATORS = [ 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 101 | }, 102 | { 103 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 104 | }, 105 | ] 106 | 107 | 108 | # Internationalization 109 | # https://docs.djangoproject.com/en/3.1/topics/i18n/ 110 | 111 | LANGUAGE_CODE = 'en-us' 112 | 113 | TIME_ZONE = 'UTC' 114 | 115 | USE_I18N = True 116 | 117 | USE_L10N = True 118 | 119 | USE_TZ = True 120 | 121 | 122 | # Static files (CSS, JavaScript, Images) 123 | # https://docs.djangoproject.com/en/3.1/howto/static-files/ 124 | 125 | STATIC_URL = '/static/' 126 | -------------------------------------------------------------------------------- /my_project/my_project/urls.py: -------------------------------------------------------------------------------- 1 | """my_project URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/3.1/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path, include 18 | 19 | urlpatterns = [ 20 | path('', include("my_app.urls")), 21 | path('admin/', admin.site.urls), 22 | path('', include('django_prometheus.urls')), 23 | ] 24 | -------------------------------------------------------------------------------- /my_project/my_project/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for my_project project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /prometheus/prometheus.yml: -------------------------------------------------------------------------------- 1 | global: 2 | scrape_interval: 15s 3 | evaluation_interval: 15s 4 | 5 | scrape_configs: 6 | - job_name: monitoring 7 | metrics_path: /metrics 8 | static_configs: 9 | - targets: 10 | - app1:8000 11 | - app2:8080 12 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | asgiref==3.3.1 2 | Django==3.1.6 3 | django-prometheus==2.1.0 4 | prometheus-client==0.9.0 5 | pytz==2021.1 6 | sqlparse==0.4.1 7 | --------------------------------------------------------------------------------