一种将Django开发、部署和测试打包到Docker中的方法
jonah的Python项目详细描述
使用jonah,您可以完全在docker中开发django应用程序。这样,您的代码就可以在 相同的环境,无论是在您的计算机上、测试服务器上还是在部署中。约拿救了你很多 安装工作在一开始,让你跳进一个充分准备,但可配置的django docker项目。
如果您使用的是TDD,可以在构建服务器上包含Jonah,以确保在将其推送到您的 临时服务器或生产服务器
开始
您可以使用pip:
> pip install jonah
然后使用jonah <command>语法运行它。要启动新项目,请运行
> jonah init your_new_project
然后,jonah将在当前工作目录中创建一个名为your_new_project的新目录,并创建一个空目录 Django项目内部。
开发项目
要运行新的空项目,请运行jonah develop:
> cd your_new_project > jonah develop
这将构建并启动容器,然后在容器内启动django项目。如果你有任何错误 消息,检查您的计算机是否安装了Docker检查项目是否正在运行, 访问浏览器中的http://localhost/(端口80)。
项目目录中的ddp目录透明地装载到容器中,以便 您的代码直接应用于正在运行的代码。(对于某些更改,您可能需要重新加载django服务器,但是 稍后将详细介绍。)
大多数情况下,在开发django应用程序时,不需要不断地重新启动开发服务器。为了 更改模型、设置或更改admin.py,可以使用以下命令重新加载django服务器:
> jonah reload
获取外壳
现在,让我们在项目中启动一个新的应用程序。为此,可以使用jonah的shell功能。打开里面的外壳 容器是这样的:
> jonah shell
过了一会儿,您应该会看到一个类似root@a0e9d20bffdf:/code#的新提示,表明您 在容器内工作。将工作目录更改为ddp目录,并运行manage.py命令 你通常会:
> cd ddp > ./manage.py startapp my_new_app
恭喜你!您刚刚在容器中运行了Django代码建议您运行makemigrations, migrate,以及其他类似的manage.py命令。类型{tt15}$退出容器外壳并返回 到你的常规命令行。
运行测试
jonah test将运行容器中的所有测试并显示实时输出
停止容器
一旦您在一整天的高效编码之后调用它,请使用jonah stop关闭容器。 快乐的编码。:)
当你开始一个新项目时,约拿是最容易使用的。但是,它应该与任何具有 Dockerfile和一个jonah.ini文件。将Django项目迁移到Jonah的一个好方法可能是:
- 将django项目重命名为ddp
- 使用django项目的名称创建一个新的jonah项目
- 用自己的目录替换jonah中的ddp目录
- 利润
命令的完整列表
Command | Description |
---|---|
^{tt21}$ | Initialize a new jonah project in the current directory |
^{tt22}$ | Build the image. |
^{tt23}$ | Build the image from scratch instead of relying on cached layers. |
^{tt24}$ | Run dev server |
^{tt25}$ | Reload Django process on dev server |
^{tt8}$ | Get a shell on the dev server |
^{tt27}$ | Stop a previously running development server |
^{tt28}$ | Build and run Unit Tests |
^{tt29}$ | Compile internationalization Strings |
^{tt30}$ | Add git and docker tags |
^{tt31}$ | Deploy to production. This command will ask you for a tag before pushing anything to the server |
^{tt32}$ | Deploy to staging |
^{tt33}$ | Deploy as tag “master” on production server, without warning and without asking for confirmation |
^{tt34}$ | Delete exited containers, dangling images, and volumes. Good to clean up hard drive space. |
若要获取命令的完整列表,请在不带任何参数的情况下运行deploy.py。
配置
init命令将创建许多配置文件。它们的用途如下:
File | Description |
---|---|
^{tt19}$ | General configuration for jonah, most importantly the Docker image name |
^{tt38}$ | This file is in Pip-Syntax. Python packages found here will be installed into the Docker container. |
^{tt39}$ | A shell script to run your tests. In many cases, this should just contain ^{tt40}$, but maybe you want to create code coverage, or include nose, or transform unit test results to other formats for your build server to use. |
^{tt41}$ | This file is in apt-get syntax. System packages that will be installed after basic system installation is complete. |
^{tt42}$ | Config file for supervisord. By default, this runs ^{tt43}$, then starts gunicorn and the Django server. |
^{tt44}$ | Config file for nginx. Look at this if you want to e.g. setup different static file handling. |
^{tt45}$ | A shell script to run EVERY TIME the container is spun up. |
^{tt46}$ | A shell script to run ONCE after the system installation has finished. |
帮助和行为准则
我们希望鼓励您的功能请求、错误报告和请求拉取。请注意 Django Code of Conduct适用于此项目。友好、热情、体贴、尊重、小心 用你选择的话来说。如果你认为你目睹了违反COC的行为,请联系丹尼尔。
遗产
许可证
这个项目是在麻省理工学院的许可下发布的。有关详细信息,请参阅LICENSE文件。