本文共 7872 字,大约阅读时间需要 26 分钟。
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:
最后,堆栈将为每个服务包括一个单独的容器:
Updates:
更新:
Interested in creating a similar environment for Flask? Check out blog post.
有兴趣为Flask创建类似的环境吗? 查看博客文章。
Along with Docker (v1.9.1) we will be using –
与Docker(v1.9.1)一起,我们将使用–
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…
我们现在准备启动容器并运行它们……
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。
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和数据。
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:
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
停止进程,让我们在云端启动应用程序!
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地址,然后在浏览器中查看它。 如果一切顺利,您应该看到自己的应用程序正在运行。
翻译自:
转载地址:http://neqwd.baihongyu.com/