From b9247c16df88039378e1ac3fe524f7ca367a7f99 Mon Sep 17 00:00:00 2001 From: Kang HuaiShuai Date: Wed, 3 Jul 2019 11:47:02 +0800 Subject: [PATCH] Update compose django, Fixed #405 Signed-off-by: Kang HuaiShuai --- compose/demo/django/.gitignore | 1 + compose/demo/django/docker-compose.yml | 2 +- compose/demo/django/manage.py | 22 ---------------------- compose/demo/django/requirements.txt | 4 ++-- compose/django.md | 16 ++++++++-------- 5 files changed, 12 insertions(+), 33 deletions(-) delete mode 100755 compose/demo/django/manage.py diff --git a/compose/demo/django/.gitignore b/compose/demo/django/.gitignore index b96b637..dfb0c89 100644 --- a/compose/demo/django/.gitignore +++ b/compose/demo/django/.gitignore @@ -1 +1,2 @@ django_example +manage.py diff --git a/compose/demo/django/docker-compose.yml b/compose/demo/django/docker-compose.yml index 2648aff..d3a5035 100644 --- a/compose/demo/django/docker-compose.yml +++ b/compose/demo/django/docker-compose.yml @@ -6,7 +6,7 @@ services: web: build: . - command: python3 manage.py runserver 0.0.0.0:8000 + command: python manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: diff --git a/compose/demo/django/manage.py b/compose/demo/django/manage.py deleted file mode 100755 index cb19182..0000000 --- a/compose/demo/django/manage.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env python -import os -import sys - -if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_example.settings") - try: - from django.core.management import execute_from_command_line - except ImportError: - # The above import may fail for some other reason. Ensure that the - # issue is really that Django is missing to avoid masking other - # exceptions on Python 2. - try: - import django - except ImportError: - raise ImportError( - "Couldn't import Django. Are you sure it's installed and " - "available on your PYTHONPATH environment variable? Did you " - "forget to activate a virtual environment?" - ) - raise - execute_from_command_line(sys.argv) diff --git a/compose/demo/django/requirements.txt b/compose/demo/django/requirements.txt index 71d0fbc..db36fdb 100644 --- a/compose/demo/django/requirements.txt +++ b/compose/demo/django/requirements.txt @@ -1,2 +1,2 @@ -Django>=1.8,<2.0 -psycopg2 +Django>=2.0,<3.0 +psycopg2>=2.7,<3.0 diff --git a/compose/django.md b/compose/django.md index 7cee4d2..a4acfa0 100644 --- a/compose/django.md +++ b/compose/django.md @@ -13,9 +13,9 @@ FROM python:3 ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code -ADD requirements.txt /code/ +COPY requirements.txt /code/ RUN pip install -r requirements.txt -ADD . /code/ +COPY . /code/ ``` 以上内容指定应用将使用安装了 Python 以及必要依赖包的镜像。更多关于如何编写 `Dockerfile` 文件的信息可以查看 [镜像创建](../image/create.md#利用 Dockerfile 来创建镜像) 和 [ Dockerfile 使用](../dockerfile/README.md)。 @@ -23,8 +23,8 @@ ADD . /code/ 第二步,在 `requirements.txt` 文件里面写明需要安装的具体依赖包名。 ```bash -Django>=1.8,<2.0 -psycopg2 +Django>=2.0,<3.0 +psycopg2>=2.7,<3.0 ``` 第三步,`docker-compose.yml` 文件将把所有的东西关联起来。它描述了应用的构成(一个 web 服务和一个数据库)、使用的 Docker 镜像、镜像之间的连接、挂载到容器的卷,以及服务开放的端口。 @@ -38,7 +38,7 @@ services: web: build: . - command: python3 manage.py runserver 0.0.0.0:8000 + command: python manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: @@ -47,15 +47,15 @@ services: - db ``` -查看 [`docker-compose.yml` 章节](yml_ref.md) 了解更多详细的工作机制。 +查看 [`docker-compose.yml` 章节](compose_file.md) 了解更多详细的工作机制。 现在我们就可以使用 `docker-compose run` 命令启动一个 `Django` 应用了。 ```bash -$ docker-compose run web django-admin.py startproject django_example . +$ docker-compose run web django-admin startproject django_example . ``` -Compose 会先使用 `Dockerfile` 为 web 服务创建一个镜像,接着使用这个镜像在容器里运行 `django-admin.py startproject django_example` 指令。 +由于 web 服务所使用的镜像并不存在,所以 Compose 会首先使用 `Dockerfile` 为 web 服务构建一个镜像,接着使用这个镜像在容器里运行 `django-admin startproject django_example` 指令。 这将在当前目录生成一个 `Django` 应用。