├── .env ├── .gitignore ├── .idea ├── .name ├── inspectionProfiles │ ├── Project_Default.xml │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── python-django-sample.iml ├── vcs.xml └── workspace.xml ├── Dockerfile ├── README.md ├── chat ├── __init__.py ├── admin.py ├── models.py ├── tests.py ├── urls.py └── views.py ├── config ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py ├── daocloud.yml ├── docker-compose.yml ├── docker-entrypoint.sh ├── manage.py ├── requirements.txt ├── static └── main.css └── templates ├── _base.html └── home.html /.env: -------------------------------------------------------------------------------- 1 | # Add Environment Variables 2 | # Django MYSQL CONFIG 3 | MYSQL_INSTANCE_NAME=django 4 | MYSQL_USERNAME=root 5 | MYSQL_PASSWORD=mysql 6 | MYSQL_PORT_3306_TCP_ADDR=mysql 7 | MYSQL_PORT_3306_TCP_PORT=3306 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.pyc 3 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | python-django-sample -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 65 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | Django 16 | 17 | 18 | GeneralJavaScript 19 | 20 | 21 | JavaScript 22 | 23 | 24 | Python 25 | 26 | 27 | 28 | 29 | Buildout 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/python-django-sample.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 15 | 16 | 17 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 176 | 177 | 178 | 179 | true 180 | 181 | 182 | 183 | 184 | 185 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 220 | 221 | 222 | 223 | 226 | 227 | 230 | 231 | 232 | 233 | 236 | 237 | 240 | 241 | 244 | 245 | 246 | 247 | 250 | 251 | 254 | 255 | 258 | 259 | 260 | 261 | 264 | 265 | 268 | 269 | 272 | 273 | 274 | 275 | 278 | 279 | 282 | 283 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 335 | 336 | 337 | 338 | 339 | 352 | 353 | 366 | 367 | 384 | 385 | 386 | 387 | 388 | 389 | true 390 | 391 | 392 | 393 | 394 | 413 | 414 | 433 | 434 | 455 | 456 | 478 | 479 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 1440842173076 512 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 546 | 549 | 550 | 551 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM daocloud.io/python:2.7 2 | ADD requirements.txt /tmp/requirements.txt 3 | RUN pip install -r /tmp/requirements.txt 4 | RUN mkdir /code 5 | WORKDIR /code 6 | COPY . /code 7 | COPY docker-entrypoint.sh docker-entrypoint.sh 8 | RUN chmod +x docker-entrypoint.sh 9 | EXPOSE 8000 10 | 11 | CMD /code/docker-entrypoint.sh 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 如何构建具有持续交付能力的 Docker 化 Django 应用 2 | 3 | > 目标:我们将之前实现的 **Django + MySQL + Redis** 留言板应用,送上云端,轻松实现应用在云端持续交付。 4 | > 5 | > 本项目代码维护在 **[DaoCloud/python-django-cd-sample](https://github.com/DaoCloud/python-django-cd-sample)** 项目中。 6 | > 7 | > 您可以在 GitHub 找到本项目并获取本文中所提到的所有代码文件。 8 | 9 | 工欲善其器,必先利其器。首先,你需要 **[DaoCloud 帐号](https://www.daocloud.io/)**。 10 | 11 | #### 云端镜像构建 12 | 13 | > 比起本地创建,在云端创建会更简单。 14 | 15 | * 第一步:在控制台点击「代码构建」。 16 | 17 | ![](http://help.daocloud.io/img/screenshots/features/build-flows/dashboard.png) 18 | 19 | * 第二步:在「代码构建」的界面中点击「创建新项目」。 20 | 21 | ![](http://help.daocloud.io/img/screenshots/features/build-flows/build-flows-index.png) 22 | 23 | * 第三步:为项目指定「项目名称」并设置代码源。 24 | 25 | 点击「开始创建」后稍等片刻,您的应用便在云端构建成咯。 26 | 27 | #### 云端部署镜像 28 | 29 | * 第零步:在控制台点击「服务集成」,创建 MySQL 和 Redis 服务 30 | * 第一步:在控制台点击「镜像仓库」。 31 | * 第二步:在「代码构建」的界面中找到需要部署的镜像,点击「部署」。 32 | * 第三步:按照为项目指定「项目名称」,并在 「基础设置」中绑定上 MySQL 和 Redis 服务。 33 | 34 | 如果没有意外的话,您的应用便在云端航行起来咯! 35 | 36 | #### 云端持续集成 37 | 38 | 首先我们需要编写一些测试代码。 39 | 40 | ```python 41 | # chat/tests.py 42 | from django.test import TestCase 43 | from django.test.client import Client 44 | 45 | # Create your tests here. 46 | class ChatTests(TestCase): 47 | client_class = Client 48 | 49 | def test(self): 50 | self.assertEqual(1 + 1, 2) 51 | ``` 52 | 53 | 本地环境下可以使用以下命令来启动测试: 54 | 55 | ```bash 56 | ./manage.py test 57 | ``` 58 | 59 | 当我们写完测试代码之后,我们需要一个持续集成环境来自动执行测试,报告项目的健康状况。 60 | 61 | 我们只需要在源代码的根目录放置 `daocloud.yml` 文件便可以接入 DaoCloud 持续集成系统,每一次源代码的变更都会触发一次 DaoCloud 持续集成。关于 `daocloud.yml` 的格式,您可以参考 **[这里](https://help.daocloud.io/features/continuous-integration/daocloud-yml.html)**。 62 | 63 | *daocloud.yml* 64 | 65 | ```yaml 66 | image: daocloud/ci-python:2.7 67 | services: 68 | - mysql 69 | - redis 70 | 71 | env: 72 | - DAO_TEST = "True" 73 | - MYSQL_INSTANCE_NAME = "test" 74 | - MYSQL_USERNAME = "root" 75 | - MYSQL_PASSWORD = "" 76 | 77 | install: 78 | - pip install coverage 79 | 80 | before_script: 81 | - pip install -r requirements.txt 82 | 83 | script: 84 | - coverage run --source='.' manage.py test 85 | - coverage report 86 | ``` 87 | -------------------------------------------------------------------------------- /chat/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaoCloud/python-django-cd-sample/7c2a69434d875256806110af2829b6c6d4449ca9/chat/__init__.py -------------------------------------------------------------------------------- /chat/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /chat/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 | -------------------------------------------------------------------------------- /chat/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | from django.test.client import Client 3 | 4 | 5 | # Create your tests here. 6 | class ChatTests(TestCase): 7 | client_class = Client 8 | 9 | def test(self): 10 | self.assertEqual(1 + 1, 2) 11 | -------------------------------------------------------------------------------- /chat/urls.py: -------------------------------------------------------------------------------- 1 | from django.conf.urls import url 2 | 3 | from . import views 4 | 5 | urlpatterns = [ 6 | url(r'^$', views.home, name='home'), 7 | ] 8 | -------------------------------------------------------------------------------- /chat/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render, redirect 2 | from .models import Item 3 | from redis import Redis 4 | import os 5 | redis = Redis(host=os.environ['REDIS_PORT_6379_TCP_ADDR'], 6 | port=os.environ['REDIS_PORT_6379_TCP_PORT'], 7 | password=os.environ.get('REDIS_PASSWORD')) 8 | 9 | 10 | def home(request): 11 | if request.method == 'POST': 12 | Item.objects.create(text=request.POST['item_text']) 13 | return redirect('/') 14 | items = Item.objects.all() 15 | counter = redis.incr('counter') 16 | return render(request, 'home.html', {'items': items, 'counter': counter}) 17 | -------------------------------------------------------------------------------- /config/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaoCloud/python-django-cd-sample/7c2a69434d875256806110af2829b6c6d4449ca9/config/__init__.py -------------------------------------------------------------------------------- /config/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for docker_django project. 3 | 4 | Generated by 'django-admin startproject' using Django 1.8.1. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.8/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/1.8/ref/settings/ 11 | """ 12 | 13 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 14 | import os 15 | 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | SITE_ROOT = os.path.dirname(os.path.realpath(__file__)) 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = "5(15ds+i2+%ik6z&!yer+ga9m=e%jcqiz_5wszg)r-z!2--b2d" 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 | # chat 41 | 'chat', 42 | ) 43 | 44 | MIDDLEWARE_CLASSES = ( 45 | 'django.contrib.sessions.middleware.SessionMiddleware', 46 | 'django.middleware.common.CommonMiddleware', 47 | 'django.middleware.csrf.CsrfViewMiddleware', 48 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 49 | 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | 'django.middleware.security.SecurityMiddleware', 53 | ) 54 | 55 | ROOT_URLCONF = 'config.urls' 56 | 57 | 58 | TEMPLATE_CONTEXT_PROCESSORS = ( 59 | "django.contrib.auth.context_processors.auth", 60 | "django.core.context_processors.debug", 61 | "django.core.context_processors.i18n", 62 | "django.core.context_processors.media", 63 | "django.core.context_processors.request", 64 | 65 | ) 66 | 67 | TEMPLATE_LOADERS = ( 68 | 'django.template.loaders.filesystem.Loader', 69 | 'django.template.loaders.app_directories.Loader', 70 | 71 | # 'django.template.loaders.eggs.Loader', 72 | ) 73 | 74 | 75 | TEMPLATE_DIRS = ( 76 | # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 77 | # Always use forward slashes, even on Windows. 78 | # Don't forget to use absolute paths, not relative paths. 79 | os.path.join(BASE_DIR, "templates"), 80 | ) 81 | 82 | WSGI_APPLICATION = 'config.wsgi.application' 83 | 84 | 85 | # Database 86 | # https://docs.djangoproject.com/en/1.8/ref/settings/#databases 87 | 88 | # DATABASES = { 89 | # 'default': { 90 | # 'ENGINE': 'django.db.backends.sqlite3', 91 | # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 92 | # } 93 | # } 94 | DAO_TEST = bool(os.environ.get('DAO_TEST')) 95 | 96 | DATABASES = { 97 | 'default': { 98 | 'ENGINE': 'django.db.backends.mysql', 99 | 'NAME': os.environ['MYSQL_INSTANCE_NAME'], 100 | 'USER': os.environ['MYSQL_USERNAME'], 101 | 'PASSWORD': os.environ['MYSQL_PASSWORD'], 102 | 'HOST': os.environ['MYSQL_PORT_3306_TCP_ADDR'], 103 | 'PORT': os.environ['MYSQL_PORT_3306_TCP_PORT'] 104 | } 105 | } 106 | 107 | 108 | # Internationalization 109 | # https://docs.djangoproject.com/en/1.8/topics/i18n/ 110 | 111 | TIME_ZONE = 'Asia/Shanghai' 112 | 113 | LANGUAGE_CODE = 'zh-hans' 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/1.8/howto/static-files/ 124 | 125 | STATIC_URL = '/static/' 126 | STATICFILES_DIRS = ( 127 | os.path.join(BASE_DIR, 'static'), 128 | ) 129 | 130 | try: 131 | from .local_settings import * 132 | except ImportError: 133 | pass 134 | -------------------------------------------------------------------------------- /config/urls.py: -------------------------------------------------------------------------------- 1 | from django.conf.urls import include, url 2 | from django.contrib import admin 3 | 4 | urlpatterns = [ 5 | url(r'^admin/', include(admin.site.urls)), 6 | url(r'^', include('chat.urls')), 7 | ] 8 | -------------------------------------------------------------------------------- /config/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI CodeVS_Admin 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", "config.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /daocloud.yml: -------------------------------------------------------------------------------- 1 | image: daocloud/ci-python:2.7 2 | services: 3 | - mysql 4 | - redis 5 | 6 | env: 7 | - DAO_TEST = "True" 8 | - MYSQL_INSTANCE_NAME = "test" 9 | - MYSQL_USERNAME = "root" 10 | - MYSQL_PASSWORD = "" 11 | 12 | install: 13 | - pip install coverage 14 | 15 | before_script: 16 | - pip install -r requirements.txt 17 | 18 | script: 19 | - coverage run --source='.' manage.py test 20 | - coverage report 21 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | web: 2 | build: . 3 | ports: 4 | - "8000:8000" 5 | links: 6 | - mysql:mysql 7 | - redis:redis 8 | env_file: .env 9 | # command: /code/manage.py runserver 0.0.0.0:8000 10 | 11 | mysql: 12 | image: daocloud.io/mysql:latest 13 | environment: 14 | - MYSQL_DATABASE=django 15 | - MYSQL_ROOT_PASSWORD=mysql 16 | ports: 17 | - "3306:3306" 18 | 19 | redis: 20 | image: daocloud.io/redis:latest 21 | ports: 22 | - "6379:6379" 23 | 24 | -------------------------------------------------------------------------------- /docker-entrypoint.sh: -------------------------------------------------------------------------------- 1 | /code/manage.py syncdb --noinput 2 | /usr/local/bin/gunicorn config.wsgi:application -w 2 -b :8000 -------------------------------------------------------------------------------- /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", "config.settings") 7 | 8 | from django.core.management import execute_from_command_line 9 | 10 | execute_from_command_line(sys.argv) 11 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Django==1.8.3 2 | gunicorn==19.3.0 3 | mysql-python 4 | redis==2.10.3 5 | -------------------------------------------------------------------------------- /static/main.css: -------------------------------------------------------------------------------- 1 | /* custom styles */ 2 | 3 | .container { 4 | max-width: 500px; 5 | } -------------------------------------------------------------------------------- /templates/_base.html: -------------------------------------------------------------------------------- 1 | {% load staticfiles %} 2 | 3 | 4 | 5 | Django && Docker 的最佳实践 6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | {% block content %} 14 | {% endblock %} 15 |
16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /templates/home.html: -------------------------------------------------------------------------------- 1 | {% extends "_base.html" %} 2 | 3 | 4 | {% block content %} 5 | 6 |
7 | {% load staticfiles %} 8 |
9 | 10 |
11 |

这个页面已经被访问了 {{counter}} 次啦!。◕‿◕。

12 |
13 | 14 |
15 | 16 |
17 | {% csrf_token %} 18 |
19 | 20 |
21 | 22 |
23 | 24 |
25 |
26 | 27 |
28 | {% for item in items %} 29 |

{{item.text}} - {{item.date_posted}}

30 | {% endfor %} 31 |
32 | 33 | {% endblock content %} 34 | --------------------------------------------------------------------------------