博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过Docker开发和部署Cookiecutter-Django
阅读量:2520 次
发布时间:2019-05-11

本文共 7057 字,大约阅读时间需要 23 分钟。

Let’s look at how to bootstrap a Django Project pre-loaded with the basic requirements needed in order to quickly get a project up and running. Further, beyond the project structure, most bootstrapped projects also take care of setting up the development and production environment settings, without troubling the user much – so we’ll look at that as well.

让我们看一下如何引导一个预加载了基本要求的Django Project,以便快速启动并运行一个项目。 此外,除了项目结构之外,大多数自举项目还负责设置开发和生产环境设置,而不会给用户带来太多麻烦,因此我们也会对此进行研究。

cookiecutter django docker

We’ll be using the popular as the bootstrapper for our Django Project along with to manage our application environment.

我们将使用流行的作为Django项目的引导程序,并使用来管理我们的应用程序环境。

Let’s begin!

让我们开始!

本地设置 (Local Setup)

Start by installing globally:

首先全局安装 :

11

Now execute the following command to generate a bootstrapped django project:

现在执行以下命令来生成一个引导的django项目:

11

This command runs cookiecutter with the repo, allowing us to enter project-specific details:

此命令使用库运行cookiecutter,使我们能够输入特定于项目的详细信息:

1122334455667788991010111112121313141415151616171718181919202021212222232324242525

项目结构 (Project Structure)

Take a quick look at the generated project structure, taking specific note of the following directories:

快速浏览一下生成的项目结构,特别注意以下目录:

  1. “config” includes all the settings for our local and production environments.
  2. “requirements” contains all the requirement files – base.txt, local.txt, production.txt, test.txt – which you can make changes to and then install via pip install -r file_name.
  3. “django_cookiecutter_docker” is the main project directory which consists of the “static”, “contrib” and “templates” directories along with the users app containing the models and boilerplate code associated with user authentication.
  1. “ config”包括我们本地和生产环境的所有设置。
  2. “需求”包含所有需求文件– base.txt,local.txt,production.txt,test.txt,您可以对其进行更改,然后通过pip install -r file_name进行pip install -r file_name
  3. “ django_cookiecutter_docker”是主要项目目录,由“ static”,“ contrib”和“ templates”目录以及包含与用户身份验证相关的模型和样板代码的users应用程序组成。

Docker安装 (Docker Setup)

Start by downloading and then installing the (v) to obtain virtualbox and the required Docker components:

首先下载并安装 (v ),以获得virtualbox和所需的Docker组件:

  • docker 1.9.1
  • docker-machine 0.5.4
  • docker-compose 1.5.2
  • 码头工人1.9.1
  • 码头工人机器0.5.4
  • 码头工人组成1.5.2

Docker机器 (Docker Machine)

Once installed, create a new Docker host within the root of the newly created Django Project:

安装后,在新创建的Django项目的根目录中创建一个新的Docker主机:

1122

NOTE: dev can be named anything you want. For example, if you have more than one development environment, you could name them djangodev1, djangodev2, and so forth.

注意dev可以命名为任何您想要的名称。 例如,如果您有多个开发环境,则可以将它们命名为djangodev1djangodev2等。

To view all Machines, run:

要查看所有计算机,请运行:

11

You can also view the IP of the dev Machine by running:

您还可以通过运行以下命令查看dev机器的IP:

11

Finally, let’s create a “/data” partition within the VM itself so that changes are persistent:

最后,让我们在VM本身中创建一个“ / data”分区,以使更改持久化:

112233

Docker撰写 (Docker Compose)

Now we can fire everything up – e.g., Django and Postgres – via Docker Compose:

现在,我们可以通过Docker Compose启动所有内容,例如Django和Postgres:

1122

Running Windows? Hit this error – Interactive mode is not yet supported on Windows? See .

运行Windows? 遇到此错误– Interactive mode is not yet supported on Windows ? 看到 。

The first build will take a while. Due to , subsequent builds will run much faster.

第一次构建将花费一些时间。 由于 ,后续构建将运行得更快。

完整性检查 (Sanity Check)

Now we can test our Django Project by applying the migrations and then running the server:

现在,我们可以通过应用迁移并运行服务器来测试Django项目:

112233

Navigate to the dev IP (port 8000) in your browser to view the Project quick start page with debugging mode on and many more development environment oriented features installed and running.

导航到浏览器中的dev IP(端口8000),以打开调试模式并安装并运行许多面向开发环境的功能,查看Project快速入门页。

Kill the server, initialize a new git repo, commit, and PUSH to GitHub.

终止服务器,初始化一个新的git repo,提交并推送到GitHub。

部署设定 (Deployment Setup)

So, we have successfully set up our Django Project locally using cookiecutter-django and served it up using the traditional manage.py command line utility via Docker.

因此,我们已经成功地使用cookiecutter-django在本地设置了Django项目,并通过Docker使用了传统的manage.py命令行实用程序来为其提供服务。

In this section, we move on to the deployment part, where the role of a web server comes into play. We will be setting up a Docker Machine on a droplet, with Postgres as our database and as our web server.

在本节中,我们继续进行部署部分,其中将发挥Web服务器的作用。 我们将在 Droplet上设置Docker Machine,以Postgres作为我们的数据库,以作为我们的Web服务器。

Along with this, we will be making use of instead of Django’s single-threaded development server to run the server process.

同时,我们将使用而不是Django的单线程开发服务器来运行服务器进程。

为什么选择Nginx? (Why Nginx?)

Apart from being a high-performance HTTP server, which almost every good web server out in the market is, Nginx has some really good features that make it stand out from the rest – namely that it:

Nginx除了是高性能HTTP服务器(几乎市场上所有优秀的Web服务器都是这种服务器)之外,还具有一些非常好的功能,使其与众不同,即:

  • Can couple as a ,
  • Can host more than one site,
  • Has an asynchronous way of handling web requests, which means that since it doesn’t rely on threads to handle web requests, it has a higher performance while handling multiple requests.
  • 可以作为 ,
  • 可以托管多个网站,
  • 具有处理Web请求的异步方式,这意味着由于它不依赖于线程来处理Web请求,因此在处理多个请求时具有更高的性能。

为什么选择独角兽? (Why Gunicorn?)

is a Python WSGI HTTP server that can be easily customized and provides better performance in terms of reliability than Django’s single-threaded development server within production environments.

是Python WSGI HTTP服务器,可以在生产环境中轻松定制,并且在可靠性方面提供比Django单线程开发服务器更好的性能。

数字海洋设置 (Digital Ocean Setup)

We will be using a server for this tutorial. After you (if necessary), a Personal Access Token, and then run the following command:

在本教程中,我们将使用服务器。 (如有必要), 个人访问令牌,然后运行以下命令:

11223344

This should only take few minutes to provision the Digital Ocean droplet and set up a new Docker Machine called prod. While you wait, navigate to the ; you should see a new droplet being created, again, called prod.

只需花费几分钟即可配置Digital Ocean Droplet,并设置一个名为prod的新Docker Machine。 等待期间,导航至“ ;然后单击“确定”。 您应该看到又创建了一个新的液滴,称为prod

Once done, there should now be two machines running, one locally (dev) and one on Digital Ocean (prod). Run docker-machine ls to confirm:

完成后,现在应该有两台计算机在运行,一台在本地( dev ),一台在Digital Ocean( prod )。 运行docker-machine ls确认:

112233

Set prod as the active machine and then load the Docker environment into the shell:

prod设置为活动机器,然后将Docker环境加载到shell中:

11

Docker Compose(参加2) (Docker Compose (take 2))

Start by renaming env.example to .env. Update the DJANGO_ALLOWED_HOSTS variable to match the Digital Ocean IP address – i.e., DJANGO_ALLOWED_HOSTS=159.203.77.132. Keep the remaining defaults for now.

首先将env.example重命名为.env。 更新DJANGO_ALLOWED_HOSTS变量以匹配数字海洋IP地址,即DJANGO_ALLOWED_HOSTS=159.203.77.132 。 现在保留其余默认值。

Now we can create the build and then fire up the services in the cloud:

现在,我们可以创建构建,然后在云中启动服务:

1122

健全性检查(取2) (Sanity Check (take 2))

Apply all the migrations:

应用所有迁移:

1122

That’s it!

而已!

Now just visit your server’s IP address, associated with the Digital Ocean droplet, and view it in the browser.

现在,只需访问与Digital Ocean Drop关联的服务器IP地址,然后在浏览器中查看它即可。

You should be good to go.

你应该很好。

翻译自:

转载地址:http://swhwd.baihongyu.com/

你可能感兴趣的文章
Jquery便利对象
查看>>
MVC: Connection String
查看>>
idea常用设置汇总
查看>>
Node.SelectNodes
查看>>
Lambda表达式语法进一步巩固
查看>>
Vue基础安装(精华)
查看>>
Git 提交修改内容和查看被修改的内容
查看>>
PAT - 1008. 数组元素循环右移问题 (20)
查看>>
请求出现 Nginx 413 Request Entity Too Large错误的解决方法
查看>>
配置php_memcache访问网站的步骤
查看>>
hibernate的id生成策略
查看>>
树莓派3B+学习笔记:5、安装vim
查看>>
[Spfa][bfs] Jzoj P5781 秘密通道
查看>>
企业帐号进行IPA的打包、分发、下载安装的详细流程(转载)
查看>>
《项目架构那点儿事》——快速构建Junit用例
查看>>
{"errmsg":"invalid weapp pagepath hint: [IunP8a07243949]","errcode":40165}微信的坑
查看>>
DB2V9.5数据库使用pdf
查看>>
Java Bigdecimal使用
查看>>
SQL注入之绕过WAF和Filter
查看>>
jquery validate使用方法
查看>>