├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── docker-cloud.yml ├── docker-compose.test.yml ├── docker-compose.yml ├── manage.py ├── project_name ├── __init__.py ├── settings.py ├── static │ └── css │ │ └── main.css ├── templates │ ├── base.html │ └── home.html ├── urls.py └── wsgi.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | .env 3 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:2.7 2 | RUN apt-get update -qq && apt-get install -qy netcat 3 | ENV PYTHONUNBUFFERED 1 4 | RUN mkdir /code 5 | WORKDIR /code 6 | ADD requirements.txt /code/ 7 | RUN pip install -r requirements.txt 8 | ADD . /code/ 9 | RUN SECRET_KEY=unset python manage.py collectstatic --no-input 10 | CMD gunicorn {{ project_name }}.wsgi -b 0.0.0.0:8000 --log-file - --access-logfile - -k eventlet --workers 4 --worker-connections 5 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | Copyright 2016 Ben Firshman 180 | 181 | Licensed under the Apache License, Version 2.0 (the "License"); 182 | you may not use this file except in compliance with the License. 183 | You may obtain a copy of the License at 184 | 185 | http://www.apache.org/licenses/LICENSE-2.0 186 | 187 | Unless required by applicable law or agreed to in writing, software 188 | distributed under the License is distributed on an "AS IS" BASIS, 189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 190 | See the License for the specific language governing permissions and 191 | limitations under the License. 192 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Django template for Docker Cloud 2 | 3 | This is a template for Django applications that can be run with Docker Compose locally and Docker Cloud in production. 4 | 5 | ## Getting started 6 | 7 | To get started (replace `myapp` with the name of your app): 8 | 9 | $ docker run -it --rm -v "$PWD":/usr/src/app -w /usr/src/app django django-admin.py startproject --template https://github.com/bfirsh/django-docker-cloud-template/zipball/master myapp 10 | $ cd myapp 11 | 12 | This readme file is now in your app's directory. You can delete this top bit and everything that follows is the start of your app's readme. 13 | 14 | 15 | # {{ project_name }} 16 | 17 | Description of {{ project_name }}. 18 | 19 | ## Running in development 20 | 21 | Install Docker for Mac or Windows. 22 | 23 | Do the initial database migration: 24 | 25 | $ docker-compose run web python manage.py migrate 26 | 27 | Then to run the app: 28 | 29 | $ docker-compose up 30 | 31 | Your app is now available at http://localhost:8000 32 | 33 | ## Running in production 34 | 35 | Upload `docker-cloud.yml` to Docker Cloud, filling in: 36 | 37 | - `SECRET_KEY` with a random string (run `openssl rand -base64 64` to generate) 38 | - `ALLOWED_HOSTS` with the hostname that the app is running on 39 | -------------------------------------------------------------------------------- /docker-cloud.yml: -------------------------------------------------------------------------------- 1 | db: 2 | image: postgres 3 | web: 4 | image: yourorg/{{ project_name }} 5 | ports: 6 | - "80:8000" 7 | links: 8 | - db 9 | environment: 10 | - ENABLE_SSL=true 11 | - SECRET_KEY 12 | - ALLOWED_HOSTS 13 | -------------------------------------------------------------------------------- /docker-compose.test.yml: -------------------------------------------------------------------------------- 1 | # https://docs.docker.com/docker-cloud/feature-reference/automated-testing/ 2 | version: '2' 3 | services: 4 | db: 5 | image: postgres 6 | sut: 7 | build: . 8 | command: sh -c "while ! nc -w 1 -z db 5432; do sleep 1; done; python manage.py test" 9 | links: 10 | - db 11 | environment: 12 | SECRET_KEY: "not secure only use for development" 13 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | db: 4 | image: postgres 5 | web: 6 | build: . 7 | command: python manage.py runserver 0.0.0.0:8000 8 | volumes: 9 | - .:/code 10 | ports: 11 | - "8000:8000" 12 | links: 13 | - db 14 | environment: 15 | DEBUG: "true" 16 | SECRET_KEY: "not secure only use for development" 17 | -------------------------------------------------------------------------------- /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", "{{ project_name }}.settings") 7 | 8 | from django.core.management import execute_from_command_line 9 | 10 | execute_from_command_line(sys.argv) 11 | -------------------------------------------------------------------------------- /project_name/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bfirsh/django-docker-cloud-template/797e90348ec7e707255a89c14f375b6b81b8f741/project_name/__init__.py -------------------------------------------------------------------------------- /project_name/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | For more information on this file, see 3 | https://docs.djangoproject.com/en/1.9/topics/settings/ 4 | 5 | For the full list of settings and their values, see 6 | https://docs.djangoproject.com/en/1.9/ref/settings/ 7 | """ 8 | 9 | import os 10 | import environ 11 | 12 | env = environ.Env( 13 | DEBUG=(bool, False), 14 | ENABLE_SSL=(bool, False), 15 | ALLOWED_HOSTS=(list, []), 16 | SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS=(list, []), 17 | ) 18 | 19 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 20 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 21 | 22 | 23 | # Quick-start development settings - unsuitable for production 24 | # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/ 25 | 26 | # SECURITY WARNING: keep the secret key used in production secret! 27 | SECRET_KEY = env("SECRET_KEY") 28 | 29 | # SECURITY WARNING: don't run with debug turned on in production! 30 | DEBUG = env('DEBUG') 31 | 32 | ALLOWED_HOSTS = env('ALLOWED_HOSTS') 33 | 34 | 35 | # Application definition 36 | 37 | INSTALLED_APPS = [ 38 | 'django.contrib.admin', 39 | 'django.contrib.auth', 40 | 'django.contrib.contenttypes', 41 | 'django.contrib.sessions', 42 | 'django.contrib.messages', 43 | 'django.contrib.staticfiles', 44 | 'bootstrap3', 45 | ] 46 | 47 | MIDDLEWARE_CLASSES = [ 48 | 'django.middleware.security.SecurityMiddleware', 49 | 'django.contrib.sessions.middleware.SessionMiddleware', 50 | 'django.middleware.common.CommonMiddleware', 51 | 'django.middleware.csrf.CsrfViewMiddleware', 52 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 53 | 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 54 | 'django.contrib.messages.middleware.MessageMiddleware', 55 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 56 | ] 57 | 58 | ROOT_URLCONF = '{{ project_name }}.urls' 59 | 60 | TEMPLATES = [ 61 | { 62 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 63 | 'DIRS': [os.path.join(BASE_DIR, '{{ project_name }}/templates')], 64 | 'APP_DIRS': True, 65 | 'OPTIONS': { 66 | 'context_processors': [ 67 | 'django.template.context_processors.debug', 68 | 'django.template.context_processors.request', 69 | 'django.contrib.auth.context_processors.auth', 70 | 'django.contrib.messages.context_processors.messages', 71 | ], 72 | }, 73 | }, 74 | ] 75 | 76 | WSGI_APPLICATION = '{{ project_name }}.wsgi.application' 77 | 78 | 79 | # Database 80 | # https://docs.djangoproject.com/en/1.9/ref/settings/#databases 81 | 82 | DATABASES = { 83 | 'default': env.db('DATABASE_URL', default='psql://postgres@db:5432/postgres'), 84 | } 85 | 86 | 87 | # Password validation 88 | # https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators 89 | 90 | AUTH_PASSWORD_VALIDATORS = [ 91 | { 92 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 93 | }, 94 | { 95 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 96 | }, 97 | { 98 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 99 | }, 100 | { 101 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 102 | }, 103 | ] 104 | 105 | AUTHENTICATION_BACKENDS = ( 106 | 'django.contrib.auth.backends.ModelBackend', 107 | ) 108 | 109 | LOGIN_REDIRECT_URL = '/' 110 | # LOGIN_URL = '/login/' 111 | 112 | # Internationalization 113 | # https://docs.djangoproject.com/en/1.9/topics/i18n/ 114 | 115 | LANGUAGE_CODE = 'en-us' 116 | 117 | TIME_ZONE = 'UTC' 118 | 119 | USE_I18N = True 120 | 121 | USE_L10N = True 122 | 123 | USE_TZ = True 124 | 125 | 126 | # Static files (CSS, JavaScript, Images) 127 | # https://docs.djangoproject.com/en/1.9/howto/static-files/ 128 | 129 | STATIC_URL = '/static/' 130 | if not DEBUG: 131 | STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' 132 | STATICFILES_DIRS = [ 133 | os.path.join(BASE_DIR, "{{ project_name }}/static"), 134 | ] 135 | STATIC_ROOT = os.path.join(BASE_DIR, "{{ project_name }}/static_root") 136 | 137 | # Log everything to the console, including tracebacks 138 | LOGGING = { 139 | 'version': 1, 140 | 'disable_existing_loggers': False, 141 | 'handlers': { 142 | 'console': { 143 | 'class': 'logging.StreamHandler', 144 | }, 145 | }, 146 | 'loggers': { 147 | 'django': { 148 | 'handlers': ['console'], 149 | 'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'), 150 | }, 151 | }, 152 | } 153 | 154 | ENABLE_SSL = env('ENABLE_SSL') 155 | SESSION_COOKIE_SECURE = ENABLE_SSL 156 | CSRF_COOKIE_SECURE = ENABLE_SSL 157 | SECURE_SSL_REDIRECT = ENABLE_SSL 158 | if ENABLE_SSL: 159 | SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') 160 | -------------------------------------------------------------------------------- /project_name/static/css/main.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bfirsh/django-docker-cloud-template/797e90348ec7e707255a89c14f375b6b81b8f741/project_name/static/css/main.css -------------------------------------------------------------------------------- /project_name/templates/base.html: -------------------------------------------------------------------------------- 1 | {% extends 'bootstrap3/bootstrap3.html' %} 2 | 3 | {% load bootstrap3 %} 4 | {% load staticfiles %} 5 | 6 | {% block bootstrap3_title %}{% block title %}{% endblock %} | Site name{% endblock %} 7 | 8 | {% block bootstrap3_extra_head %} 9 | 10 | {% block extra_head %}{% endblock %} 11 | {% endblock %} 12 | 13 | {% block bootstrap3_content %} 14 |
15 | {% autoescape off %}{% bootstrap_messages %}{% endautoescape %} 16 | 17 | {% block content %}(no content){% endblock %} 18 |
19 | 20 | {% endblock %} 21 | -------------------------------------------------------------------------------- /project_name/templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | {% block title %}Home{% endblock %} 3 | 4 | {% block content %} 5 |

Hello World

6 | {% endblock %} 7 | -------------------------------------------------------------------------------- /project_name/urls.py: -------------------------------------------------------------------------------- 1 | """URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/1.9/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: url(r'^$', 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: url(r'^$', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.conf.urls import url, include 14 | 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) 15 | """ 16 | from django.conf.urls import url, include 17 | from django.contrib import admin 18 | from django.views.generic import TemplateView 19 | 20 | urlpatterns = [ 21 | url(r'^$', TemplateView.as_view(template_name="home.html")), 22 | url(r'^admin/', admin.site.urls), 23 | ] 24 | -------------------------------------------------------------------------------- /project_name/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for {{ project_name }} 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.9/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | from whitenoise.django import DjangoWhiteNoise 14 | 15 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings") 16 | 17 | application = get_wsgi_application() 18 | application = DjangoWhiteNoise(application) 19 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Django==1.9.5 2 | psycopg2==2.6.1 3 | django-bootstrap3==7.0.1 4 | django-environ==0.4.0 5 | gunicorn==19.4.5 6 | whitenoise==3.0 7 | eventlet==0.18.4 8 | --------------------------------------------------------------------------------