· 5 years ago · Jan 09, 2020, 08:24 PM
1-= file .env.dev =-
2
3DEBUG=1
4SECRET_KEY=foo
5DJANGO_ALLOWED_HOSTS=[localhost 127.0.0.1 [::1]]
6SQL_ENGINE=django.db.backends.postgresql
7SQL_DATABASE=hello_django_dev
8SQL_USER=hello_django
9SQL_PASSWORD=hello_django
10SQL_HOST=db
11SQL_PORT=5432
12
13-= file Dockerfile =-
14
15# pull official base image
16FROM python:3.8.0-alpine
17
18# set work directory
19WORKDIR /usr/src/app
20
21# set environment variables
22ENV PYTHONDONTWRITEBYTECODE 1
23ENV PYTHONUNBUFFERED 1
24
25# install psycopg2 dependencies
26RUN apk update \
27 && apk add postgresql-dev gcc python3-dev musl-dev
28
29# install dependencies
30RUN pip install --upgrade pip
31COPY ./requirements.txt /usr/src/app/requirements.txt
32RUN pip install -r requirements.txt
33
34# copy project
35COPY . /usr/src/app/
36
37RUN python manage.py makemigrations && python manage.py migrate
38
39-= file docker-compose.yml =-
40
41version: '3.7'
42
43services:
44 web:
45 build: ./app
46 command: python manage.py runserver 0.0.0.0:8000
47 volumes:
48 - ./app/:/usr/src/app/
49 ports:
50 - 8000:8000
51 env_file:
52 - ./.env.dev
53 depends_on:
54 - db
55 db:
56 image: postgres:12.0-alpine
57 volumes:
58 - postgres_data:/var/lib/postgresql/data/
59 environment:
60 - POSTGRES_USER=hello_django
61 - POSTGRES_PASSWORD=hello_django
62 - POSTGRES_DB=hello_django_dev
63
64volumes:
65 postgres_data:
66
67
68-= file settings.py =-
69
70import os
71
72# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
73BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
74
75
76# Quick-start development settings - unsuitable for production
77# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
78
79# SECURITY WARNING: keep the secret key used in production secret!
80SECRET_KEY = 'foo'
81
82# SECURITY WARNING: don't run with debug turned on in production!
83DEBUG = int(os.environ.get("DEBUG", default=0))
84
85ALLOWED_HOSTS = ['localhost', '127.0.0.1', '[::1]']
86
87DATABASES = {
88 'default': {
89 "ENGINE": os.environ.get("SQL_ENGINE", "django.db.backends.sqlite3"),
90 "NAME": os.environ.get("SQL_DATABASE", os.path.join(BASE_DIR, "db.sqlite3")),
91 "USER": os.environ.get("SQL_USER", "user"),
92 "PASSWORD": os.environ.get("SQL_PASSWORD", "password"),
93 "HOST": os.environ.get("SQL_HOST", "localhost"),
94 "PORT": os.environ.get("SQL_PORT", "5432"),
95 }
96}