美文网首页
Docker Compose 部署 Django 项目

Docker Compose 部署 Django 项目

作者: awker | 来源:发表于2018-07-08 06:09 被阅读0次

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

相关文章

网友评论

      本文标题:Docker Compose 部署 Django 项目

      本文链接:https://www.haomeiwen.com/subject/fibnuftx.html