技术控

    今日:122| 主题:49179
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Using Docker to develop and deploy Django apps

[复制链接]
我不是缺你不可 发表于 2016-10-4 01:21:18
85 7

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Finally, Django, with Docker,    on production!  
  I finally managed to deploy Django in a Docker container on production! I’ve been trying to switch to a full Docker development/production model since Docker came out, but only recently did the ecosystem mature enough to allow me to easily use Docker both for development (where it excels, in my opinion) and on production (where it’s pretty okay and quite useful).
  In this post, I will quickly give you all the relevant details and files you need to go from a newly checked-out repository to a full development environment in one command, and to deploy that service to production. As a bonus, I’ll show you how to use    Gitlab(which is awesome) to build your containers and store them in the Gitlab registry.  
  Let’s begin!
  Development

  First of all, let’s start with the
  1. docker-compose.yml
复制代码
. In case you don’t know,
  1. docker-compose
复制代码
is a way to run multiple containers at once, easily connecting them to each other. This is a godsend when doing development, but not that useful in production (where you usually want to deploy services manually).
  To make development easier, we’ll write a
  1. docker-compose.yml
复制代码
to set up and run the essentials: Postgres, Django’s dev server, and Caddy (just to proxy port 8000 to 80, you can remove it if you like port 8000).
  We have to do some contortions with the Django devserver, because Docker doesn’t care if Postgres is ready before starting the server, so Django sees that it can’t contact the database and quits. So, we just wait until port 5432 is ready before starting the devserver.
  To connect to Postgres, just set the database hostname to
  1. db
复制代码
, the user and database to
  1. postgres
复制代码
, and the password to
  1. password
复制代码
. That’s pretty much all the settings you need for this. I’ve also helpfully set the
  1. IN_DOCKER
复制代码
environment variable so your settings file can know whether it’s running in Docker or not.
  Generally, with Docker, you have to rely heavily on environment variables for configuration, rather than, say, a
  1. local_settings.py
复制代码
file. That’s not necessarily a bad thing, as environment variables can be pretty handy as well.
  Here’s the complete
  1. docker-compose.yml
复制代码
:
  1. version: '2'
  2. services:
  3.   db:
  4.     image: postgres
  5.     environment:
  6.       POSTGRES_PASSWORD: password
  7.     volumes:
  8.       - ./dbdata:/var/lib/postgresql/data
  9.   web:
  10.     # Docker hack to wait until Postgres is up, then run stuff.
  11.     command: bash -c "while ! nc -w 1 -z db 5432; do sleep 0.1; done; ./manage.py migrate; while :; do ./manage.py runserver_plus 0.0.0.0:8000; sleep 1; done"
  12.     image: django
  13.     build: .
  14.     volumes:
  15.       - .:/code
  16.     depends_on:
  17.       - db
  18.     environment:
  19.       IN_DOCKER: 1
  20.   webserver:
  21.     image: jumanjiman/caddy
  22.     depends_on:
  23.       - web
  24.     ports:
  25.       - "80:8000"
  26.     command: -port 8000 -host 0.0.0.0 "proxy / web:8000 { }"
复制代码
With this one file, setting up a new developer on the team consists of:
  1. docker-compose0
复制代码
That’s it. They have a complete development environment that mirrors production on their local computer, with one command. That environment also handles hot reloads, as usual, and will persist the database data under a directory of the repo.
  To start the entire stack up, run
  1. docker-compose1
复制代码
and open http://localhost/, you should see your app’s front page.
  If you need to run a
  1. docker-compose2
复制代码
command, you can do it like so:
  1. docker-compose3
复制代码
Done and done! Complete isolation with no extra RAM or CPU usage!
  Production

  Since the dev setup is done, we’ll start with the
  1. docker-compose4
复制代码
. Its job is to list all the commands needed to get a container from a newly-installed Linux instance all the way to running your application. The Dockerfile is completely separate from
  1. docker-compose.yml
复制代码
in this setup, since the latter is for setting up a local analog to the production server, and the former is for detailing what needs to be done to a blank OS to run the app. docker-compose does not use the Dockerfile.
  This is the
  1. docker-compose4
复制代码
, I will include comments in the file itself so you can follow along.
  1. docker-compose7
复制代码
It’s pretty straightforward, except for that last line. Why do we need a script? Why not just
  1. docker-compose8
复制代码
? What’s in that file? The questions keep mounting.
  It’s just because I want to
  1. docker-compose9
复制代码
on the server every time before a run. This is what the
  1. docker-compose.yml0
复制代码
script looks like:
  1. docker-compose.yml1
复制代码
Pretty simple! I use uWSGI to run Django, all you need is the appropriate configuration. I like to stick it in an .ini file, it’s too trivial to post. Adjust to taste.
  Using Gitlab to build your images

  As I said earlier, Gitlab is amazing. It can run pretty much anything in its CI stage, including building your Docker images. It also has an integrated Docker registry, which means that, every time you push your code to the repo, Gitlab can automatically build a container so you can go to a newly-provisioned, fresh server that has Docker installed and do:
  1. docker-compose.yml2
复制代码
  1. docker-compose.yml3
复制代码
will use the host’s networking and save you a lot of trouble forwarding ports. It’s less secure, because everything inside the container will be running on the host’s network space, but, since the dev server only listens to localhost anyway (and would run on the host’s net space without Docker), I’m fine with that.
  For our case, the four CI stages that will run on Gitlab are:
  
       
  • Run a static check using     
    1. docker-compose.yml4
    复制代码
    (which you should always do).   
  • Run your tests (which you should always do as well).   
  • Build your Docker image and copy it to the registry.   
  • Deploy everything to production (in my case, this happens by triggering a      Captain WebhookURL.  
  Here’s the
  1. docker-compose.yml5
复制代码
that will build your images:
  1. docker-compose.yml6
复制代码
That’s it!
  Epilogue

  Pretty much all you need to run Docker    bothlocally, for development, and on production, is in those two files. If you want to use Gitlab’s fantastic integration with everything, you have that third file, for no extra charge.  
  If you know of something I can install that will handle starting/restarting/updating my containers on the server, please let me know! I hear there are various solutions, like Kubernetes, but ideally I’d prefer something more lightweight. My ideal scenario is one where I can have a service or some software I can deploy containers to, and which will abstract all the service running and container updating away.
  If you’re aware of something that will do the job, or if you have any questions or feedback, leave a comment here or    tweet at me. Thanks!
友荐云推荐




上一篇:Extending an Exadata Eighth Rack to a Quarter Rack
下一篇:How it feels to learn JavaScript in 2016
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

董习丽 发表于 2016-10-4 02:09:16
占位编辑
回复 支持 反对

使用道具 举报

任性的小情绪 发表于 2016-10-5 12:18:39
因为谦卑,所以高贵.因为懂得,所以慈悲;因为陌生,所以勇敢,因为距离,所以美丽.
回复 支持 反对

使用道具 举报

杜久超 发表于 2016-10-5 16:39:59
我不是缺你不可表述之事与本人无关,只是本着“看贴回贴,繁荣网络、利人利己”的原则
回复 支持 反对

使用道具 举报

兰孟杰 发表于 2016-10-6 02:37:49
我不是缺你不可的帖的确不错!我代表江湖上所有的兄弟姐妹给你顶上去!
回复 支持 反对

使用道具 举报

gxdy2744 发表于 2016-10-11 11:12:21
火前留名,前排占座,此楼出租,欢迎议价。
回复 支持 反对

使用道具 举报

千雁 发表于 2016-10-14 07:43:42
楼下的接上
回复 支持 反对

使用道具 举报

筱筱 发表于 2016-10-25 16:40:15
我不是缺你不可最近很有基情
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表