Docker Compose 部署 Django 项目

1、目录结构如下

# tree devops_docker/ -L 2
devops_docker/
├── devops                  // 代码目录
│   ├── assets
│   ├── collected_static    // Django项目收集的静态文件目录
│   ├── devops
│   ├── manage.py
│   ├── requirements.txt
│   ├── scripts
│   ├── static
│   └── templates
├── docker-compose.yml      // docker-compose 文件
├── Dockerfile              // docker 容器文件
└── nginx_config
    └── nginx.conf          // nginx 配置文件

2、编写 devops_docker/Dockerfile,配置 Django 环境,打包项目代码,安装项目依赖,修改 settings.py 配置

# cat devops_docker/Dockerfile
FROM python:2
RUN mkdir /code
WORKDIR /code
ADD devops/ /code/
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package -r requirements.txt
RUN sed -i 's/DEBUG = True/DEBUG = False/' devops/settings.py

3、配置 nginx.conf,代理后端 Django 项目

# cat devops_docker/nginx_config/nginx.conf
server {
    listen   8888;
    server_name  _;
 
    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
     
    location /static {
        alias /devops/static/;
    }
 
    location / {
#        if ($request_uri = "/") {
#            rewrite "/" http://10.1.2.101:8888/assets/ break;
#        }
        proxy_set_header Host $host;
        proxy_pass http://web:9999;
    }
}

4、配置 docker-compose 文件,编排后端 Django 项目和 nginx 服务

# cat devops_docker/docker-compose.yml
version: '2'
 
services:
  web:
    build: .
    command: gunicorn -w 5 --timeout 60 -b  0.0.0.0:9999 devops.wsgi
    expose:
      - "9999"
  nginx:
    image: nginx:latest
    ports:
      - "8888:8888"
    volumes:
      - ./devops/collected_static:/devops/static
      - ./nginx_config:/etc/nginx/conf.d
    depends_on:
      - web

5、启动服务

# docker-compose up -d
Building web
Step 1 : FROM python:2
 ---> 0fcc7acd124b
Step 2 : RUN mkdir /code
 ---> Running in 5552e8a7671a
 ---> b4b8bbccb35e
Removing intermediate container 5552e8a7671a
Step 3 : WORKDIR /code
 ---> Running in 42d31b65774d
 ---> 44b5c17581e9
Removing intermediate container 42d31b65774d
Step 4 : ADD devops/ /code/
 ---> 3dd4ec922c41
Removing intermediate container dfa3f1a97100
Step 5 : RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package -r requirements.txt
 ---> Running in 39ab24bff063
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting some-package
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e7/a2/d318a685319c3801db1ae0002fc8e095663a55546c62a6e30d9d0fc3289b/some-package-0.1.zip
Collecting ansible==2.5.3 (from -r requirements.txt (line 1))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/20/c1/2a1a6104f7660b5d6b9876ea351f24212c19ce17c03771baf94f1c81d13b/ansible-2.5.3.tar.gz (10.1MB)
Collecting asn1crypto==0.24.0 (from -r requirements.txt (line 2))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ea/cd/35485615f45f30a510576f1a56d1e0a7ad7bd8ab5ed7cdc600ef7cd06222/asn1crypto-0.24.0-py2.py3-none-any.whl (101kB)
Collecting bcrypt==3.1.4 (from -r requirements.txt (line 3))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2e/5a/2abeae20ce294fe6bf63da0e0b5a885c788e1360bbd124edcc0429678a59/bcrypt-3.1.4-cp27-cp27mu-manylinux1_x86_64.whl (57kB)
Collecting certifi==2018.4.16 (from -r requirements.txt (line 4))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/7c/e6/92ad559b7192d846975fc916b65f667c7b8c3a32bea7372340bfe9a15fa5/certifi-2018.4.16-py2.py3-none-any.whl (150kB)
Collecting cffi==1.11.5 (from -r requirements.txt (line 5))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/14/dd/3e7a1e1280e7d767bd3fa15791759c91ec19058ebe31217fe66f3e9a8c49/cffi-1.11.5-cp27-cp27mu-manylinux1_x86_64.whl (407kB)
Collecting chardet==3.0.4 (from -r requirements.txt (line 6))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
Collecting cryptography==2.2.2 (from -r requirements.txt (line 7))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/dd/c2/3a5bfefb25690725824ade71e6b65449f0a9f4b29702cce10560f786ebf6/cryptography-2.2.2-cp27-cp27mu-manylinux1_x86_64.whl (2.2MB)
Collecting Django==1.11.13 (from -r requirements.txt (line 8))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/25/4d/c8228419346a0e84aec202a43e181afc6572b861d38f8a0306dbce6abef0/Django-1.11.13-py2.py3-none-any.whl (6.9MB)
Collecting enum34==1.1.6 (from -r requirements.txt (line 9))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c5/db/e56e6b4bbac7c4a06de1c50de6fe1ef3810018ae11732a50f15f62c7d050/enum34-1.1.6-py2-none-any.whl
Collecting gunicorn==19.9.0 (from -r requirements.txt (line 10))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/8c/da/b8dd8deb741bff556db53902d4706774c8e1e67265f69528c14c003644e6/gunicorn-19.9.0-py2.py3-none-any.whl (112kB)
Collecting idna==2.6 (from -r requirements.txt (line 11))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/27/cc/6dd9a3869f15c2edfab863b992838277279ce92663d334df9ecf5106f5c6/idna-2.6-py2.py3-none-any.whl (56kB)
Collecting ipaddress==1.0.22 (from -r requirements.txt (line 12))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fc/d0/7fc3a811e011d4b388be48a0e381db8d990042df54aa4ef4599a31d39853/ipaddress-1.0.22-py2.py3-none-any.whl
Collecting Jinja2==2.10 (from -r requirements.txt (line 13))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
Collecting MarkupSafe==1.0 (from -r requirements.txt (line 14))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz
Collecting MySQL-python==1.2.5 (from -r requirements.txt (line 15))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip (108kB)
Collecting paramiko==2.4.1 (from -r requirements.txt (line 16))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/3e/db/cb7b6656e0e7387637ce850689084dc0b94b44df31cc52e5fc5c2c4fd2c1/paramiko-2.4.1-py2.py3-none-any.whl (194kB)
Collecting pika==0.11.2 (from -r requirements.txt (line 17))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/76/b4/0ada6918ac79ce4d8d45d0ba436ee21a1ef685047fb1dde4d2de0178b327/pika-0.11.2-py2.py3-none-any.whl (107kB)
Collecting pyasn1==0.4.2 (from -r requirements.txt (line 18))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ba/fe/02e3e2ee243966b143657fb8bd6bc97595841163b6d8c26820944acaec4d/pyasn1-0.4.2-py2.py3-none-any.whl (71kB)
Collecting pycparser==2.18 (from -r requirements.txt (line 19))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/8c/2d/aad7f16146f4197a11f8e91fb81df177adcc2073d36a17b1491fd09df6ed/pycparser-2.18.tar.gz (245kB)
Collecting PyNaCl==1.2.1 (from -r requirements.txt (line 20))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/80/3d/d709b9fbd69e21dd3a4d34eb690c5484094699e03b7447bc7eb173cfd7b6/PyNaCl-1.2.1-cp27-cp27mu-manylinux1_x86_64.whl (696kB)
Collecting pytz==2018.4 (from -r requirements.txt (line 21))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/dc/83/15f7833b70d3e067ca91467ca245bae0f6fe56ddc7451aa0dc5606b120f2/pytz-2018.4-py2.py3-none-any.whl (510kB)
Collecting PyYAML==3.12 (from -r requirements.txt (line 22))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4a/85/db5a2df477072b2902b0eb892feb37d88ac635d36245a72a6a69b23b383a/PyYAML-3.12.tar.gz (253kB)
Collecting requests==2.18.4 (from -r requirements.txt (line 23))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/49/df/50aa1999ab9bde74656c2919d9c0c085fd2b3775fd3eca826012bef76d8c/requests-2.18.4-py2.py3-none-any.whl (88kB)
Collecting six==1.11.0 (from -r requirements.txt (line 24))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting unicodecsv==0.14.1 (from -r requirements.txt (line 25))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/6f/a4/691ab63b17505a26096608cc309960b5a6bdf39e4ba1a793d5f9b1a53270/unicodecsv-0.14.1.tar.gz
Collecting urllib3==1.22 (from -r requirements.txt (line 26))
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/63/cb/6965947c13a94236f6d4b8223e21beb4d576dc72e8130bd7880f600839b8/urllib3-1.22-py2.py3-none-any.whl (132kB)
Requirement already satisfied: setuptools in /usr/local/lib/python2.7/site-packages (from ansible==2.5.3->-r requirements.txt (line 1)) (39.2.0)
Building wheels for collected packages: some-package, ansible, MarkupSafe, MySQL-python, pycparser, PyYAML, unicodecsv
  Running setup.py bdist_wheel for some-package: started
  Running setup.py bdist_wheel for some-package: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/e5/93/b8/d80471dd36383e2953d8af6df65b9fbd85f7dd3f4d033d0e25
  Running setup.py bdist_wheel for ansible: started
  Running setup.py bdist_wheel for ansible: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/72/86/9f/2ddbb0902d3cd4c28e30737fd34ec32a32909db7e9b4811916
  Running setup.py bdist_wheel for MarkupSafe: started
  Running setup.py bdist_wheel for MarkupSafe: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/d7/a0/93/442333b07631d4167735a3876491458a2c2c959370dce71469
  Running setup.py bdist_wheel for MySQL-python: started
  Running setup.py bdist_wheel for MySQL-python: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/c5/cb/2c/b59c12cfd0dab1ad36ea6d1d614a98173b8cd494f2431ffdf4
  Running setup.py bdist_wheel for pycparser: started
  Running setup.py bdist_wheel for pycparser: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/4f/d6/ac/c1762c553b1370c3a519bab3e40e63ecae282f4004ccf1d09a
  Running setup.py bdist_wheel for PyYAML: started
  Running setup.py bdist_wheel for PyYAML: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/37/e6/d6/ca1ba5e6d86dac4173f4df6a52e9e2a7efee26d9763bdb01ef
  Running setup.py bdist_wheel for unicodecsv: started
  Running setup.py bdist_wheel for unicodecsv: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/0f/63/bb/20a2176f61aa991c789408261815d1f5be04e9bc93ef85a036
Successfully built some-package ansible MarkupSafe MySQL-python pycparser PyYAML unicodecsv
Installing collected packages: some-package, MarkupSafe, Jinja2, PyYAML, pyasn1, six, pycparser, cffi, bcrypt, enum34, asn1crypto, idna, ipaddress, cryptography, PyNaCl, paramiko, ansible, certifi, chardet, pytz, Django, gunicorn, MySQL-python, pika, urllib3, requests, unicodecsv
Successfully installed Django-1.11.13 Jinja2-2.10 MarkupSafe-1.0 MySQL-python-1.2.5 PyNaCl-1.2.1 PyYAML-3.12 ansible-2.5.3 asn1crypto-0.24.0 bcrypt-3.1.4 certifi-2018.4.16 cffi-1.11.5 chardet-3.0.4 cryptography-2.2.2 enum34-1.1.6 gunicorn-19.9.0 idna-2.6 ipaddress-1.0.22 paramiko-2.4.1 pika-0.11.2 pyasn1-0.4.2 pycparser-2.18 pytz-2018.4 requests-2.18.4 six-1.11.0 some-package-0.1 unicodecsv-0.14.1 urllib3-1.22
 ---> 5ab13bc86bc7
Removing intermediate container 39ab24bff063
Step 6 : RUN sed -i 's/DEBUG = True/DEBUG = False/' devops/settings.py
 ---> Running in 39d40d37f723
 ---> 02a453f27b01
Removing intermediate container 39d40d37f723
Successfully built 02a453f27b01
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating devopsdocker_web_1 ...
Creating devopsdocker_web_1 ... done
Creating devopsdocker_nginx_1 ...
Creating devopsdocker_nginx_1 ... done

6、查看服务状态

# docker-compose ps
        Name                      Command               State               Ports            
----------------------------------------------------------------------------------------------
devopsdocker_nginx_1   nginx -g daemon off;             Up      80/tcp, 0.0.0.0:8888->8888/tcp
devopsdocker_web_1     gunicorn -w 5 --timeout 60 ...   Up      9999/tcp
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,444评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,421评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,036评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,363评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,460评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,502评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,511评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,280评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,736评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,014评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,190评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,848评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,531评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,159评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,411评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,067评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,078评论 2 352

推荐阅读更多精彩内容