docker版Django

原文地址:http://kekefund.com/2017/03/30/docker-django/ (本人博客)

Django的運(yùn)行是基于python的環(huán)境,加上django包。在docker中運(yùn)行django,實(shí)現(xiàn)方式是從docker下載python鏡像,然后安裝django運(yùn)行所依賴(lài)的包。

https://store.docker.com/images/python?tab=description 中介紹pull鏡像方式有一種叫python:onbuild。
這種鏡像創(chuàng)建方式根據(jù)項(xiàng)目中提供的requirements.txt文件自動(dòng)pip安裝依賴(lài)包。大多數(shù)情況,通過(guò)python:onbuild能創(chuàng)建一個(gè)滿(mǎn)足工程所需的獨(dú)立鏡像。

一、編寫(xiě)requirements.txt

下述的目錄結(jié)構(gòu)是一個(gè)Django Rest Framework例子,其中項(xiàng)目名稱(chēng)為restful,app名稱(chēng)為api。

首先我們需要把項(xiàng)目所依賴(lài)的包放到requirements.txt中:

Django==1.8
django-bootstrap-toolkit==2.15.0
django-filter==1.0.1
djangorestframework==3.5.4
djangorestframework-jwt==1.10.0
pandas==0.19.2
SQLAlchemy==1.1.4
MySQL-python==1.2.5

二、編寫(xiě)Dockerfile

本文是基于python2.7制作的,Dockerfile文件如下:

FROM python:2-onbuild
CMD [ "python", "./manage.py", "runserver", "0.0.0.0:8000"]

CMD命令執(zhí)行Django啟動(dòng)程序,0.0.0.0是對(duì)所有IP開(kāi)放,監(jiān)聽(tīng)端口8000。
需要說(shuō)明的是CMD中的每個(gè)參數(shù)得單獨(dú)分開(kāi),像這樣"runserver 0.0.0.0:8000"是運(yùn)行不成功的。

2.1 pip 國(guó)內(nèi)鏡像

先創(chuàng)建pip.conf文件,使用阿里云作為鏡像:

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

在創(chuàng)建Dockerfile:

FROM python:3.5

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY pip.conf /root/.pip/pip.conf
COPY requirements.txt /usr/src/app/
RUN pip install -r /usr/src/app/requirements.txt

RUN rm -rf /usr/src/app
COPY . /usr/src/app

CMD [ "python", "./manage.py", "runserver", "0.0.0.0:8091"]

三、構(gòu)建鏡像

  • $ docker build -t my-python-app .
[cbb@number_api]$ docker build -t number_api_django:0.3 .
Sending build context to Docker daemon 655.9 kB
Step 1/2 : FROM python:2-onbuild
# Executing 3 build triggers...
Step 1/1 : COPY requirements.txt /usr/src/app/
Step 1/1 : RUN pip install --no-cache-dir -r requirements.txt
 ---> Running in 4711187b3011
Collecting Django==1.8 (from -r requirements.txt (line 2))
  Downloading Django-1.8-py2.py3-none-any.whl (6.2MB)
Collecting django-bootstrap-toolkit==2.15.0 (from -r requirements.txt (line 3))
  Downloading django-bootstrap-toolkit-2.15.0.tar.gz
Collecting django-filter==1.0.1 (from -r requirements.txt (line 4))
  Downloading django_filter-1.0.1-py2.py3-none-any.whl (54kB)
Collecting djangorestframework==3.5.4 (from -r requirements.txt (line 5))
  Downloading djangorestframework-3.5.4-py2.py3-none-any.whl (709kB)
Collecting djangorestframework-jwt==1.10.0 (from -r requirements.txt (line 6))
  Downloading djangorestframework_jwt-1.10.0-py2.py3-none-any.whl
Collecting pandas==0.19.2 (from -r requirements.txt (line 7))
  Downloading pandas-0.19.2-cp27-cp27mu-manylinux1_x86_64.whl (17.2MB)
Collecting SQLAlchemy==1.1.4 (from -r requirements.txt (line 8))
  Downloading SQLAlchemy-1.1.4.tar.gz (5.1MB)
Collecting MySQL-python==1.2.5 (from -r requirements.txt (line 9))
  Downloading MySQL-python-1.2.5.zip (108kB)
Collecting PyJWT<2.0.0,>=1.4.0 (from djangorestframework-jwt==1.10.0->-r requirements.txt (line 6))
  Downloading PyJWT-1.4.2-py2.py3-none-any.whl
Collecting pytz>=2011k (from pandas==0.19.2->-r requirements.txt (line 7))
  Downloading pytz-2016.10-py2.py3-none-any.whl (483kB)
Collecting numpy>=1.7.0 (from pandas==0.19.2->-r requirements.txt (line 7))
  Downloading numpy-1.12.1-cp27-cp27mu-manylinux1_x86_64.whl (16.5MB)
Collecting python-dateutil (from pandas==0.19.2->-r requirements.txt (line 7))
  Downloading python_dateutil-2.6.0-py2.py3-none-any.whl (194kB)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python2.7/site-packages (from python-dateutil->pandas==0.19.2->-r requirements.txt (line 7))
Installing collected packages: Django, django-bootstrap-toolkit, django-filter, djangorestframework, PyJWT, djangorestframework-jwt, pytz, numpy, python-dateutil, pandas, SQLAlchemy, MySQL-python
  Running setup.py install for django-bootstrap-toolkit: started
    Running setup.py install for django-bootstrap-toolkit: finished with status 'done'
  Running setup.py install for SQLAlchemy: started
    Running setup.py install for SQLAlchemy: finished with status 'done'
  Running setup.py install for MySQL-python: started
    Running setup.py install for MySQL-python: finished with status 'done'
Successfully installed Django-1.8 MySQL-python-1.2.5 PyJWT-1.4.2 SQLAlchemy-1.1.4 django-bootstrap-toolkit-2.15.0 django-filter-1.0.1 djangorestframework-3.5.4 djangorestframework-jwt-1.10.0 numpy-1.12.1 pandas-0.19.2 python-dateutil-2.6.0 pytz-2016.10
Step 1/1 : COPY . /usr/src/app
 ---> 712a54b6b923
Removing intermediate container df33c056f7c0
Removing intermediate container 4711187b3011
Removing intermediate container 6220af43bf96
Step 2/2 : CMD python ./manage.py runserver 0.0.0.0:8000
 ---> Running in 53c0cf32d840
 ---> 17c97bc704d9
Removing intermediate container 53c0cf32d840
Successfully built 17c97bc704d9

[cbb@number_api]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
number_api_django   0.3                 17c97bc704d9        23 seconds ago      868 MB

這樣就成功創(chuàng)建了鏡像number_api_django:0.3

四、運(yùn)行容器

  • docker run
[cbb@number_api]$ docker run -it --rm -p 8080:8000 --name api1 number_api_django:0.3
Performing system checks...

System check identified no issues (0 silenced).
March 30, 2017 - 07:34:03
Django version 1.8, using settings 'restful.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

這樣就啟動(dòng)了django程序。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個(gè) Awesome - XXX 系列...
    aimaile閱讀 26,823評(píng)論 6 427
  • Docker — 云時(shí)代的程序分發(fā)方式 要說(shuō)最近一年云計(jì)算業(yè)界有什么大事件?Google Compute Engi...
    ahohoho閱讀 15,828評(píng)論 15 147
  • docker基本概念 1. Image Definition 鏡像 Image 就是一堆只讀層 read-only...
    慢清塵閱讀 8,998評(píng)論 1 21
  • 今天要開(kāi)始日常吐槽咯! 首先,就是今天早上看書(shū)看到一半,然后突然想起來(lái)要更新一下我的小青春,然后。就覺(jué)得寫(xiě)了那么多...
    黑煤餡的小姐姐閱讀 204評(píng)論 0 0
  • 送你,美麗的衣裳;看你,對(duì)鏡貼花黃。
    黃貍閱讀 177評(píng)論 0 0

友情鏈接更多精彩內(nèi)容