├── .env ├── .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 | #You can Setting Some Environment in there 2 | MYSQL_INSTANCE_NAME=django 3 | MYSQL_USERNAME=root 4 | MYSQL_PASSWORD=mysql -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | python-django-sample -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 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 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | Django 16 | 17 | 18 | General 19 | 20 | 21 | GeneralJavaScript 22 | 23 | 24 | JavaScript 25 | 26 | 27 | Python 28 | 29 | 30 | 31 | 32 | Buildout 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/python-django-sample.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 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 | true 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 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 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 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 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 1441593335351 491 | 492 | 1441593335351 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 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 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 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 | -------------------------------------------------------------------------------- /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 Compose 配置 Django 应用开发环境 2 | 3 | > 目标:搭建基于 Docker 的 Django 应用开发环境。 4 | > 5 | > 本项目代码维护在 **[DaoCloud/python-django-sample](https://github.com/DaoCloud/python-django-sample)** 项目中。 6 | > 7 | > 您可以在 GitHub 找到本项目并获取本文中所提到的所有代码文件。 8 | 9 | ### 前言 10 | 11 | 工欲善其事,必先利其器。这次我们将使用: 12 | 13 | * Docker >= 1.8.0 14 | * Docker Machine >= 0.4.1 15 | * Docker Compose >= 1.4.0 16 | 17 | 等工具,配置基于 Docker 的 Django 开发环境。 18 | 19 | #### Docker 20 | 21 | 一款轻量级容器管理引擎,由 Docker Daemon、Docker Client 组成。 22 | 23 | #### Docker Daemon 24 | 25 | Docker 架构中常驻后台的系统进程,负责接收处理用户发送的请求和管理所有的 Docker 容器,所谓的**运行 Docker** 即代表**运行 Docker Daemon**。 26 | 27 | #### Docker Client 28 | 29 | Docker 架构中用户与 Docker Daemon 建立通信的客户端。 30 | 31 | #### Docker Machine (!!deprecated) 32 | 33 | Docker 官方提供的部署工具。帮助用户快速在运行环境中创建虚拟机服务节点,在虚拟机中安装并配置 Docker,最终帮助用户配置 Docker Client,使得 Docker Client 有能力与虚拟机中的 Docker 建立通信。 34 | 35 | #### Docker Compose 36 | 37 | Docker 官方提供的容器编排工具。随着服务的复杂度增长,容器管理过程的配置项将变得冗长,Compose 可有效帮助用户缓解甚至解决容器部署的复杂性。 38 | 39 | #### 通过 Docker Machine 安装 Docker (!!deprecated) 40 | 41 | > 如果你是 Windows 或 OS X 用户推荐阅读以下章节,将指导您使用 Docker Machine 安装与管理 Docker。 42 | 43 | * 首先通过 `create` 命令创建一台名为 dev 的 VirtualBox 虚拟机,并已经安装好了 Docker。 44 | 45 | ```bash 46 | $ docker-machine create -d virtualbox dev; 47 | INFO[0000] Creating CA: /Users/dev/.docker/machine/certs/ca.pem 48 | INFO[0000] Creating client certificate: /Users/dev/.docker/machine/certs/cert.pem 49 | INFO[0001] Downloading boot2docker.iso to /Users/dev/.docker/machine/cache/boot2docker.iso... 50 | INFO[0035] Creating SSH key... 51 | INFO[0035] Creating VirtualBox VM... 52 | INFO[0043] Starting VirtualBox VM... 53 | INFO[0044] Waiting for VM to start... 54 | INFO[0094] "dev" has been created and is now the active machine. 55 | To point your Docker client at it, run this in your shell: $(docker-machine env dev) 56 | ``` 57 | 58 | > **提示** 59 | > 60 | > 由于 `create` 命令在初始化的时候,会从海外下载一个 ISO 镜像,由于国内网络不稳定,所以可能会在这一步耗费很长时间。 61 | > 62 | > 我们可以通过以下办法进行加速。 63 | > 64 | > OS X 65 | > 66 | > ```bash 67 | > $ mkdir ~/.boot2docker 68 | > $ echo ISOURL = \"https://get.daocloud.io/boot2docker/boot2docker-lastest.iso\" > ~/.boot2docker/profile 69 | > ``` 70 | > 71 | > Win 72 | > 73 | > ```bash 74 | > $ ISOURL = "https://get.daocloud.io/boot2docker/boot2docker-lastest.iso" 75 | > ``` 76 | 77 | * 设置环境变量以将本机的 Docker Client 和 dev 上的 Docker Daemon 建立通信。 78 | 79 | ```bash 80 | $ eval "$(docker-machine env dev)" 81 | ``` 82 | 83 | * 查看当前所有正在运行的 Machines 84 | 85 | ```bash 86 | $ docker-machine ls 87 | NAME ACTIVE DRIVER STATE URL 88 | dev * virtualbox Running tcp://192.168.99.100:2376 89 | 90 | ``` 91 | 92 | * 启动 Machine(dev) 93 | 94 | ```bash 95 | $ docker-machine start dev 96 | Starting VM ... 97 | ``` 98 | 99 | * 获取 Machine(dev) 的 IP 100 | 101 | ```bash 102 | $ docker-machine ip dev 103 | 192.168.99.100 104 | ``` 105 | 106 | * 通过 SSH 进入 Machine(dev) 107 | 108 | ```bash 109 | $ docker-machine ssh dev 110 | Starting VM ... 111 | ``` 112 | 113 | #### 通过 Docker Compose 编排应用 114 | 115 | > 因所有官方镜像均位于境外服务器,为了确保所有示例能正常运行,示例中使用与官方镜像保持同步的 DaoCloud 境内镜像: 116 | 117 | *docker-compose.yml* 118 | 119 | ```yaml 120 | web: 121 | build: . 122 | ports: 123 | - "8000:8000" 124 | links: 125 | - mysql:mysql 126 | - redis:redis 127 | env_file: .env 128 | command: /code/manage.py runserver 0.0.0.0:8000 129 | 130 | mysql: 131 | image: daocloud.io/mysql:latest 132 | environment: 133 | - MYSQL_DATABASE=django 134 | - MYSQL_ROOT_PASSWORD=mysql 135 | ports: 136 | - "3306:3306" 137 | 138 | redis: 139 | image: daocloud.io/redis:latest 140 | ports: 141 | - "6379:6379" 142 | ``` 143 | 144 | 在这个文件中。我们定义了 3 个 Docker 微服务 `web`、`mysql`、`redis`。 145 | 146 | - 通过 `build/image`,为微服务指定了 Docker 镜像 147 | - 通过 `links`,为 `web` 关联 `mysql` 与 `redis` 服务 148 | - 通过 `ports`,指定该服务需要公开的端口 149 | - 通过 `command`,指定该服务启动时执行的命令(可覆盖 Dockerfile 里的声明) 150 | 151 | 现在万事俱备,该让我们使应用运行起来啦,构建镜像并运行服务: 152 | 153 | ```bash 154 | $ docker-compose build 155 | $ docker-compose up -d 156 | ``` 157 | 158 | 别忘记要为项目初始化数据库哦: 159 | 160 | ```bash 161 | $ docker-compose run web /usr/local/bin/python manage.py migrate 162 | ``` 163 | 164 | 这样我们的 Demo 就可以通过浏览器来访问了 165 | 166 | **!!!**如果是使用 Docker Machine 的读者,您需要用 167 | 168 | ```bash 169 | $ docker-machine ip dev 170 | ``` 171 | 172 | 获取容器实际运行环境的 IP,并访问 `:8000`。 173 | 174 | 对于 Docker 安装在本地 Linux 机器上的读者,则直接使用 `127.0.0.1:8000` 即可。不出意外的话,您应该可以看到如下页面: 175 | 176 |  177 | 178 | #### 总结 179 | 180 | * Docker Machine 安装 Docker 181 | * Docker Compose 编排服务 182 | * 在开发状态下,容器只是单纯的运行环境 183 | * 通过 `docker-compose run service xxx` 执行 `xxx` 指令 184 | -------------------------------------------------------------------------------- /chat/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DaoCloud/python-django-sample/a98114dafb74d91161c2dc771a0a0a0d950e9aab/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-sample/a98114dafb74d91161c2dc771a0a0a0d950e9aab/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 | STATIC_ROOT = os.path.join(BASE_DIR, 'static') 127 | 128 | try: 129 | from .local_settings import * 130 | except ImportError: 131 | pass 132 | -------------------------------------------------------------------------------- /config/urls.py: -------------------------------------------------------------------------------- 1 | from django.conf.urls import include, url 2 | from django.contrib import admin 3 | import settings 4 | 5 | urlpatterns = [ 6 | url(r'^admin/', include(admin.site.urls)), 7 | url(r'^', include('chat.urls')), 8 | url(r'^static/(?P.*)$','django.views.static.serve',{'document_root':settings.STATIC_ROOT}), 9 | ] 10 | -------------------------------------------------------------------------------- /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 | version: 2.0 2 | 3 | build: 4 | image: 5 | dockerfile_path: Dockerfile 6 | build_dir: / 7 | cache: true 8 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------
这个页面已经被访问了 {{counter}} 次啦!。◕‿◕。