原文地址: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)行所依賴的包呛伴。
在https://store.docker.com/images/python?tab=description 中介紹pull鏡像方式有一種叫python:onbuild。
這種鏡像創(chuàng)建方式根據(jù)項(xiàng)目中提供的requirements.txt文件自動(dòng)pip安裝依賴包。大多數(shù)情況潮改,通過(guò)python:onbuild能創(chuàng)建一個(gè)滿足工程所需的獨(dú)立鏡像。
一腹暖、編寫(xiě)requirements.txt
下述的目錄結(jié)構(gòu)是一個(gè)Django Rest Framework例子汇在,其中項(xiàng)目名稱為restful,app名稱為api脏答。
首先我們需要把項(xiàng)目所依賴的包放到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程序。