博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Docker Compose和Machine进行Django开发
阅读量:2519 次
发布时间:2019-05-11

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

django docker

is a containerization tool used for spinning up isolated, reproducible application environments. This piece details how to containerize a Django Project, Postgres, and Redis for local development along with delivering the stack to the cloud via and .

是一种容器化工具,用于扩展隔离的,可再现的应用程序环境。 这一部分详细介绍了如何将Django Project,Postgres和Redis容器化以进行本地开发,以及如何通过和将堆栈交付到云中。

In the end, the stack will include a separate container for each service:

最后,堆栈将为每个服务包括一个单独的容器:

  • 1 web/Django container
  • 1 nginx container
  • 1 Postgres container
  • 1 Redis container
  • 1 data container
  • 1个Web / Django容器
  • 1个Nginx容器
  • 1个Postgres容器
  • 1个Redis容器
  • 1个数据容器

container stack diagram

Updates:

更新:

  • 12/27/2015: Updated to the latest versions of Docker – Docker client (v1.9.1), Docker compose (v1.5.2), and Docker Machine (v0.5.4) – and Python (v3.5)
  • 2015年12月27日:已更新至最新版本的Docker – Docker客户端(v1.9.1),Docker compose(v1.5.2)和Docker Machine(v0.5.4)–和Python(v3.5)

Interested in creating a similar environment for Flask? Check out blog post.

有兴趣为Flask创建类似的环境吗? 查看博客文章。

本地设置 (Local Setup)

Along with Docker (v1.9.1) we will be using –

与Docker(v1.9.1)一起,我们将使用–

  • (v1.5.2) for orchestrating a multi-container application into a single app, and
  • (v0.5.4) for creating Docker hosts both locally and in the cloud.
  • (v1.5.2)用于将多容器应用程序编排为单个应用程序,以及
  • (v0.5.4),用于在本地和云中创建Docker主机。

Follow the directions and to install Docker Compose and Machine, respectively.

按照和的指示分别安装Docker Compose和Machine。

Running either Mac OS X or Windows, then your best bet is to install .

运行Mac OS X或Windows,最好的选择是安装 。

Test out the installs:

测试安装:

11223344

Next clone the project from the or create your own project based on the project structure found on the repo:

接下来,从克隆项目,或者根据的项目结构创建自己的项目:

11223344556677889910101111121213131414151516161717181819192020212122222323242425252626272728282929

We’re now ready to get the containers up and running…

我们现在准备启动容器并运行它们……

Docker机器 (Docker Machine)

To start Docker Machine, simply navigate to the project root and then run:

要启动Docker Machine,只需导航到项目根目录然后运行:

11223344556677889910101111121213131414151516161717

The create command set up a new “Machine” (called dev) for Docker development. In essence, it started a VM with Docker running. Now just point Docker at the dev machine:

create命令为Docker开发设置了一个新的“机器”(称为dev)。 本质上,它在运行Docker的情况下启动了VM。 现在,将Docker指向开发机器:

11

Run the following command to view the currently running Machines:

运行以下命令以查看当前正在运行的计算机:

112233

Next, let’s fire up the containers with Docker Compose and get Django, Postgres, and Redis up and running.

接下来,让我们使用Docker Compose启动容器,并启动并运行Django,Postgres和Redis。

Docker撰写 (Docker Compose)

Let’s take a look at the docker-compose.yml file:

让我们看一下docker-compose.yml文件:

1122334455667788991010111112121313141415151616171718181919202021212222232324242525262627272828292930303131323233333434353536363737383839394040414142424343444445454646

Here, we’re defining five services – web, nginx, postgres, redis, and data.

在这里,我们定义了五种服务-Web,nginx,postgres,redis和数据。

  1. First, the web service is built via the instructions in the Dockerfile within the “web” directory – where the Python environment is setup, requirements are installed, and the Django application is fired up on port 8000. That port is then forwarded to port 80 on the host environment – e.g., the Docker Machine. This service also adds environment variables to the container that are defined in the .env file.
  2. The nginx service is used for reverse proxy to forward requests either to Django or the static file directory.
  3. Next, the postgres service is built from the the official from , which installs Postgres and runs the server on the default port 5432.
  4. Likewise, the redis service uses the official to install, well, Redis and then the service is ran on port 6379.
  5. Finally, notice how there is a separate container that’s used to store the database data – called data. This helps ensure that the data persists even if the Postgres container is completely destroyed.
  1. 首先,通过“ web”目录中Dockerfile中的指令构建Web服务-在该目录中设置Python环境,安装需求,并在8000端口上启动Django应用程序。然后将该端口转发至端口80在主机环境(例如Docker Machine)上。 此服务还将环境变量添加到.env文件中定义的容器中。
  2. Nginx服务用于反向代理,以将请求转发到Django或静态文件目录。
  3. 接下来,使用来自的官方构建postgres服务,该会安装Postgres并在默认端口5432上运行服务器。
  4. 同样,redis服务使用正式的进行安装,好吧,Redis,然后在端口6379上运行该服务。
  5. 最后,请注意如何使用一个单独的容器来存储数据库数据,即数据。 这有助于确保即使Postgres容器被完全破坏了,数据也仍然存在。

Now, to get the containers running, build the images and then start the services:

现在,要使容器运行,请构建映像,然后启动服务:

1122

Grab a cup of coffee. Or go for a long walk. This will take a while the first time you run it. Subsequent builds run much quicker since Docker the results from the first build.

拿一杯咖啡。 或走很长一段路。 第一次运行需要一段时间。 由于Docker 了第一个构建的结果,因此后续的构建运行得更快。

Once the services are running, we need to create the database migrations:

服务运行后,我们需要创建数据库迁移:

11

Grab the IP associated with Docker Machine – docker-machine ip dev – and then navigate to that IP in your browser:

获取与Docker Machine关联的IP( docker-machine ip dev ),然后在浏览器中导航到该IP:

flask running on docker

Nice!

真好!

Try refreshing. You should see the counter update. Essentially, we’re using the to increment after each handled request. Check out the code in web/docker_django/apps/todo/views.py for more info.

尝试刷新。 您应该看到计数器更新。 本质上,我们在每个已处理的请求之后使用进行递增。 在web / docker_django / apps / todo / views.py中查看代码以获取更多信息。

Again, this created five services, all running in different containers:

再次,这创建了五个服务,它们全部运行在不同的容器中:

1122334455667788

To see which environment variables are available to the web service, run:

要查看Web服务可用的环境变量,请运行:

11

To view the logs:

要查看日志:

11

You can also enter the Postgres Shell – since we forwarded the port to the host environment in the docker-compose.yml file – to add users/roles as well as databases via:

您还可以输入Postgres Shell(由于我们已将端口转发到docker-compose.yml文件中的主机环境中),因此可以通过以下方式添加用户/角色和数据库:

11

Ready to deploy? Stop the processes via docker-compose stop and let’s get the app up in the cloud!

准备部署? 通过docker-compose stop停止进程,让我们在云端启动应用程序!

部署方式 (Deployment)

So, with our app running locally, we can now push this exact same environment to a cloud hosting provider with Docker Machine. Let’s deploy to a box.

因此,随着我们的应用程序在本地运行,我们现在可以使用Docker Machine将这个完全相同的环境推送到云托管提供商。 让我们部署到盒子中。

After you for Digital Ocean, generate a , and then run the following command:

在 Digital Ocean之后,生成一个 ,然后运行以下命令:

11223344

This will take a few minutes to provision the droplet and setup a new Docker Machine called production:

这将需要几分钟来配置小滴并设置一个名为production的新Docker Machine:

11223344556677889910101111121213131414151516161717

Now we have two Machines running, one locally and one on Digital Ocean:

现在我们有两台机器在运行,一台在本地,一台在Digital Ocean上:

11223344

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

将生产设置为活动计算机,并将Docker环境加载到外壳中:

11

Finally, let’s build the Django app again in the cloud. This time we need to use a slightly different Docker Compose file that does not mount a in the container. Why? Well, the volume is perfect for local development since we can update our local code in the “web” directory and the changes will immediately take affect in the container. In production, there’s no need for this, obviously.

最后,让我们再次在云中构建Django应用。 这次,我们需要使用稍微不同的Docker Compose文件,该文件不会在容器中装载 。 为什么? 好吧,该卷非常适合本地开发,因为我们可以在“ web”目录中更新本地代码,并且更改将立即在容器中生效。 在生产中,显然没有必要。

112233

Did you notice how we specified a different config file for production? What if you wanted to also run ? See this .

您是否注意到我们如何为生产指定其他配置文件? 如果您还想运行怎么办? 看到这个 。

Grab the IP address associated with that Digital Ocean account and view it in the browser. If all went well, you should see your app running, as it should.

获取与该Digital Ocean帐户关联的IP地址,然后在浏览器中查看它。 如果一切顺利,您应该看到自己的应用程序正在运行。

结论 (Conclusion)

  • Grab the code from the (star it too… my self-esteem depends on it!).
  • Comment below with questions.
  • Need a challenge? Try using to clean up the repetitive code in the two Docker Compose configuration files. Keep it DRY!
  • Have a great day!
  • 从仓库中获取代码(也给它星标...我的自尊依赖于它!)。
  • 在下面评论问题。
  • 需要挑战吗? 尝试使用清理两个Docker Compose配置文件中的重复代码。 保持干燥!
  • 祝你有美好的一天!

翻译自:

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

你可能感兴趣的文章
C#——winform
查看>>
CSS3 transform制作的漂亮的滚动式导航
查看>>
《小强升职记——时间管理故事书》读书笔记
查看>>
Alpha 冲刺(3/10)
查看>>
Kaldi中的Chain模型
查看>>
spring中的ResourceBundleMessageSource使用和测试示例
查看>>
css规范 - bem
查看>>
电梯调度程序的UI设计
查看>>
转自 zera php中extends和implements的区别
查看>>
Array.of使用实例
查看>>
【Luogu】P2498拯救小云公主(spfa)
查看>>
如何获取网站icon
查看>>
几种排序写法
查看>>
java 多线程的应用场景
查看>>
dell support
查看>>
转:Maven项目编译后classes文件中没有dao的xml文件以及没有resources中的配置文件的问题解决...
查看>>
MTK android 设置里 "关于手机" 信息参数修改
查看>>
单变量微积分笔记6——线性近似和二阶近似
查看>>
补几天前的读书笔记
查看>>
HDU 1829/POJ 2492 A Bug's Life
查看>>