├── web ├── tests │ ├── __init__.py │ └── test_env_settings.py ├── docker_django │ ├── __init__.py │ ├── apps │ │ ├── __init__.py │ │ └── todo │ │ │ ├── __init__.py │ │ │ ├── tests.py │ │ │ ├── admin.py │ │ │ ├── urls.py │ │ │ ├── models.py │ │ │ ├── views.py │ │ │ └── templates │ │ │ ├── _base.html │ │ │ └── home.html │ ├── urls.py │ ├── wsgi.py │ └── settings.py ├── static │ └── main.css ├── requirements.txt ├── Dockerfile └── manage.py ├── .gitignore ├── nginx ├── Dockerfile └── sites-enabled │ └── django_project ├── .env ├── README.md ├── production.yml └── docker-compose.yml /web/tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /web/docker_django/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /web/docker_django/apps/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /web/docker_django/apps/todo/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /web/static/main.css: -------------------------------------------------------------------------------- 1 | /* custom styles */ 2 | 3 | .container { 4 | max-width: 500px; 5 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.db 2 | .DS_Store 3 | .idea 4 | venv 5 | env 6 | __pycache__ 7 | .venv 8 | .cache 9 | -------------------------------------------------------------------------------- /web/requirements.txt: -------------------------------------------------------------------------------- 1 | Django==2.1.7 2 | gunicorn==19.9.0 3 | psycopg2==2.7.7 4 | redis==3.2.1 5 | -------------------------------------------------------------------------------- /web/docker_django/apps/todo/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /web/docker_django/apps/todo/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /nginx/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM tutum/nginx 2 | 3 | RUN rm /etc/nginx/sites-enabled/default 4 | 5 | COPY sites-enabled/ /etc/nginx/sites-enabled 6 | -------------------------------------------------------------------------------- /web/docker_django/apps/todo/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import re_path 2 | 3 | from . import views 4 | 5 | urlpatterns = [ 6 | re_path(r'^$', views.home, name='home'), 7 | ] 8 | -------------------------------------------------------------------------------- /web/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.7-slim 2 | 3 | RUN python -m pip install --upgrade pip 4 | 5 | COPY requirements.txt requirements.txt 6 | RUN python -m pip install -r requirements.txt 7 | 8 | COPY . . 9 | -------------------------------------------------------------------------------- /.env: -------------------------------------------------------------------------------- 1 | # Add Environment Variables 2 | 3 | 4 | SECRET_KEY=5(15ds+i2+%ik6z&!yer+ga9m=e%jcqiz_5wszg)r-z!2--b2d 5 | DB_NAME=postgres 6 | DB_USER=postgres 7 | DB_PASS=postgres 8 | DB_SERVICE=postgres 9 | DB_PORT=5432 -------------------------------------------------------------------------------- /web/docker_django/apps/todo/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class Item(models.Model): 5 | text = models.TextField(blank=False, null=False) 6 | date_posted = models.DateField(auto_now=True) 7 | -------------------------------------------------------------------------------- /web/docker_django/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import include, re_path 2 | from django.contrib import admin 3 | 4 | urlpatterns = [ 5 | re_path(r'^admin/', admin.site.urls), 6 | re_path(r'^', include('docker_django.apps.todo.urls')), 7 | ] 8 | -------------------------------------------------------------------------------- /web/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "docker_django.settings") 7 | 8 | from django.core.management import execute_from_command_line 9 | 10 | execute_from_command_line(sys.argv) 11 | -------------------------------------------------------------------------------- /nginx/sites-enabled/django_project: -------------------------------------------------------------------------------- 1 | server { 2 | 3 | listen 80; 4 | server_name example.org; 5 | charset utf-8; 6 | 7 | location /static { 8 | alias /usr/src/app/static; 9 | } 10 | 11 | location / { 12 | proxy_pass http://web:8000; 13 | proxy_set_header Host $host; 14 | proxy_set_header X-Real-IP $remote_addr; 15 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /web/docker_django/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for docker_django 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/1.8/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", "docker_django.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /web/docker_django/apps/todo/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render, redirect 2 | from .models import Item 3 | from redis import Redis 4 | 5 | 6 | redis = Redis(host='redis', port=6379) 7 | 8 | 9 | def home(request): 10 | if request.method == 'POST': 11 | Item.objects.create(text=request.POST['item_text']) 12 | return redirect('/') 13 | items = Item.objects.all() 14 | counter = redis.incr('counter') 15 | return render(request, 'home.html', {'items': items, 'counter': counter}) 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Django Development With Docker Compose and Machine 2 | 3 | Featuring: 4 | 5 | - Docker v18.09.2 6 | - Docker Compose v1.23.2 7 | - Docker Machine v0.16.1 8 | - Python 3.7.3 9 | 10 | Blog post -> https://realpython.com/blog/python/django-development-with-docker-compose-and-machine/ 11 | 12 | ### OS X Instructions 13 | 14 | 1. Start new machine - `docker-machine create -d virtualbox dev;` 15 | 1. Configure your shell to use the new machine environment - `eval $(docker-machine env dev)` 16 | 1. Build images - `docker-compose build` 17 | 1. Start services - `docker-compose up -d` 18 | 1. Create migrations - `docker-compose run web /usr/local/bin/python manage.py migrate` 19 | 1. Grab IP - `docker-machine ip dev` - and view in your browser 20 | -------------------------------------------------------------------------------- /web/docker_django/apps/todo/templates/_base.html: -------------------------------------------------------------------------------- 1 | {% load staticfiles %} 2 | 3 | 4 |
5 |
10 | This page has been viewed {{counter}} times!
12 |