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