Docker第三章:Docker版Django項目發布
Django的運行是基於python的環境,加上django包。在docker中運行django,實現方式是從docker下載python鏡像,然后安裝django運行所依賴的包。
在docker倉庫:https://store.docker.com/images/python?tab=description
中介紹pull鏡像方式中有一種叫python:onbuild。 這種鏡像創建方式根據項目中提供的
requirements.txt文件自動pip安裝依賴包。大多數情況,通過python:onbuild能創建一個滿足工程所需的獨立鏡像。
docker版django項目發布過程
- 查看Docker安裝信息
- 查看Docker版本,命令:
docekr version
- 查看Docker運行信息,命名
docker info
- 檢測安裝是否正確,命令
docker run hello-world
- 查看Docker版本,命令:
- 新建django項目web01
下面的目錄結構是一個普通的django項目案例,項目名為web01,app名為product.
注意:項目根目錄創建了dockerfile,pip.conf, requirements.txt 三個文件
- 添加項目依賴
首先我們需要把項目所依賴的包放到requirements.txt中:
PyMySQL==0.9.3
redis==3.2.1
request==2019.4.13
django==2.1.8
- 編寫Dockerfile文件
這種鏡像創建方式會根據項目中提供的requirements.txt文件自動pip安裝依賴包
FROM python:3.6.6
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:8000"]
說明:
FROM python3.6.6 指定Python版本
"0.0.0.0:8000" 指定開啟容器中8000端口,下面開啟容器就要映射到這個端口
- pip.conf
這里是為了使用鏡像 pip install 相關依賴,速度快的起飛
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
- requirements.txt
PyMySQL==0.9.3
redis==3.2.1
request==2019.4.13
django==2.1.8
沒有必要逐一寫依賴,使用如下命令可把本地虛擬環境中的所有包生成到項目中
pip freeze > requirements.txt
- 項目上傳到雲服務器(centos7)
不用再自己手動創建虛擬環境,因為docker會自動去下載依賴.
- 構建鏡像(默認從docker國外的遠程庫下載,速度很慢!一定先替換為國內的阿里軟件源! 替換步驟參考文章最后!)
# 注意后面的 . 不能省略
docker build -t my-django-web01:1.0 .
創建過程中會自動下載python3.6.6, 和所有依賴庫,而且速度非常快
[root@zzy web01]# docker build -t my-django-web01 .
Sending build context to Docker daemon 54.78kB
Step 1/9 : FROM python:3.6.6
---> 8256ec07b2ad
Step 2/9 : RUN mkdir -p /usr/src/app
---> Running in 87f01d9ff80c
Removing intermediate container 87f01d9ff80c
---> c94c6c192cda
Step 3/9 : WORKDIR /usr/src/app
---> Running in 67d9c429a314
Removing intermediate container 67d9c429a314
---> f17e814ab74a
Step 4/9 : COPY pip.conf /root/.pip/pip.conf
---> 680baa5b9db7
Step 5/9 : COPY requirements.txt /usr/src/app/
---> 14c058045c73
Step 6/9 : RUN pip install -r /usr/src/app/requirements.txt
---> Running in 63e1f177ec58
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting PyMySQL==0.9.3 (from -r /usr/src/app/requirements.txt (line 1))
Downloading http://mirrors.aliyun.com/pypi/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)
Collecting redis==3.2.1 (from -r /usr/src/app/requirements.txt (line 2))
Downloading http://mirrors.aliyun.com/pypi/packages/ac/a7/cff10cc5f1180834a3ed564d148fb4329c989cbb1f2e196fc9a10fa07072/redis-3.2.1-py2.py3-none-any.whl (65kB)
Collecting request==2019.4.13 (from -r /usr/src/app/requirements.txt (line 3))
Downloading http://mirrors.aliyun.com/pypi/packages/f1/27/7cbde262d854aedf217061a97020d66a63163c5c04e0ec02ff98c5d8f44e/request-2019.4.13.tar.gz
Collecting django==2.1.8 (from -r /usr/src/app/requirements.txt (line 4))
Downloading http://mirrors.aliyun.com/pypi/packages/a9/e4/fb8f473fe8ee659859cb712e25222243bbd55ece7c319301eeb60ccddc46/Django-2.1.8-py3-none-any.whl (7.3MB)
Collecting get (from request==2019.4.13->-r /usr/src/app/requirements.txt (line 3))
Downloading http://mirrors.aliyun.com/pypi/packages/3f/ef/bb46f77f7220ac1b7edba0c76d810c89fddb24ddd8c08f337b9b4a618db7/get-2019.4.13.tar.gz
Collecting post (from request==2019.4.13->-r /usr/src/app/requirements.txt (line 3))
Downloading http://mirrors.aliyun.com/pypi/packages/0f/05/bd79da5849ea6a92485ed7029ef97b1b75e55c26bc0ed3a7ec769af666f3/post-2019.4.13.tar.gz
Requirement already satisfied: setuptools in /usr/local/lib/python3.6/site-packages (from request==2019.4.13->-r /usr/src/app/requirements.txt (line 3)) (40.4.3)
Collecting pytz (from django==2.1.8->-r /usr/src/app/requirements.txt (line 4))
Downloading http://mirrors.aliyun.com/pypi/packages/87/76/46d697698a143e05f77bec5a526bf4e56a0be61d63425b68f4ba553b51f2/pytz-2019.2-py2.py3-none-any.whl (508kB)
Collecting query_string (from get->request==2019.4.13->-r /usr/src/app/requirements.txt (line 3))
Downloading http://mirrors.aliyun.com/pypi/packages/12/3c/412a45daf5bea9b1d06d7de41787ec4168001dfa418db7ec8723356b119f/query-string-2019.4.13.tar.gz
Collecting public (from query_string->get->request==2019.4.13->-r /usr/src/app/requirements.txt (line 3))
Downloading http://mirrors.aliyun.com/pypi/packages/54/4d/b40004cc6c07665e48af22cfe1e631f219bf4282e15fa76a5b6364f6885c/public-2019.4.13.tar.gz
Building wheels for collected packages: request, get, post, query-string, public
Running setup.py bdist_wheel for request: started
Running setup.py bdist_wheel for request: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/ad/4b/66/7bf43f82bfa3851488303afacbf3395920c7ffbc58590ee821
Running setup.py bdist_wheel for get: started
Running setup.py bdist_wheel for get: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/18/a8/59/e2f913e2c643fdf74a309385c7580eab49fcfb3ce3ad7cb1ef
Running setup.py bdist_wheel for post: started
Running setup.py bdist_wheel for post: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/24/1b/01/996d66d3933da0e348e148d7acb8b45819f826a984f63e9ff6
Running setup.py bdist_wheel for query-string: started
Running setup.py bdist_wheel for query-string: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/33/a0/63/f6ce72127a7f2ce4829359e42ee446d32ea5cce39174f0c565
Running setup.py bdist_wheel for public: started
Running setup.py bdist_wheel for public: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/d7/42/2a/fff89dc52788c2a6df5ec485e11ed38b5241a36b6e56fd4661
Successfully built request get post query-string public
Installing collected packages: PyMySQL, redis, public, query-string, get, post, request, pytz, django
Successfully installed PyMySQL-0.9.3 django-2.1.8 get-2019.4.13 post-2019.4.13 public-2019.4.13 pytz-2019.2 query-string-2019.4.13 redis-3.2.1 request-2019.4.13
You are using pip version 18.1, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Removing intermediate container 63e1f177ec58
---> 26f8e1d73b3d
Step 7/9 : RUN rm -rf /usr/src/app
---> Running in 942bac99b204
Removing intermediate container 942bac99b204
---> 34d443f6a5c8
Step 8/9 : COPY . /usr/src/app
---> a43a3648e6fd
Step 9/9 : CMD [ "python", "./manage.py", "runserver", "0.0.0.0:8000"]
---> Running in 42ed6ab00275
Removing intermediate container 42ed6ab00275
---> 334c7d9888bf
Successfully built 334c7d9888bf
Successfully tagged my-django-web01:latest
[root@zzy web01]#
- 開啟容器
# 或者 http://120.79.197.130:8000(端口號隨意映射,但要注意開啟對應安全組)
docker run -it --rm -p 8000:8000 --name djweb01 my-django-web01:1.0
推送鏡像到阿里雲
官網參考: https://cr.console.aliyun.com/repository/cn-zhangjiakou/zhouzhengyang/myredis/details
- 在阿里雲上創建鏡像倉庫
我這里先創建了一個命名空間為xxx
,然后再這個命名空間下創建鏡像倉庫my-django-web01
創建后,獲得公網地址:registry.cn-zhangjiakou.aliyuncs.com/xxx/my-django-web01
,按照操作指南做即可!
- 阿里官網步驟
https://cr.console.aliyun.com/repository/cn-zhangjiakou/xx/myredis/details
- 開通說明步驟
https://www.cnblogs.com/guanfuchang/p/10831383.html
# 1) 登錄阿里雲
docker login --username=[您當前阿里雲用戶名] registry.cn-hangzhou.aliyuncs.com
sudo docker login --username=xxxx@163.com registry.cn-zhangjiakou.aliyuncs.com
# 2) 給本地鏡像加標簽
sudo docker tag my-django-web01:1.0 registry.cn-zhangjiakou.aliyuncs.com/xxx/my-django-web01:1.0
# 3) 推送到阿里遠程庫
sudo docker push registry.cn-zhangjiakou.aliyuncs.com/xxx/my-django-web01:1.0
# 4) 分享給別人下載
docker pull registry.cn-zhangjiakou.aliyuncs.com/xxx/my-django-web01:1.0
騰訊雲鏡像加速
使用 dockerfile buid 鏡像的時候,鏡像大小動不動就 800+ M,漫長的等待,使筆者切身感受到使用國內鏡像下載相關依賴的重要性。 雲服務器主要為騰訊雲、阿里雲,這里介紹兩種方案
使用的 centos 服務器 這是最簡單的,執行如下命令即可
#使用騰訊雲dockerhub加速器適用於 Centos7 版本。
#修改 Docker 配置文件
vi /etc/sysconfig/docker
OPTIONS='--registry-mirror=https://mirror.ccs.tencentyun.com'
systemctl restart docker
阿里雲鏡像加速
獲取加速地址
- 阿里雲開發者平台
https://promotion.aliyun.com/ntms/act/kubernetes.html
- 點擊管理中心
參考https://cr.console.aliyun.com/cn-zhangjiakou/instances/mirrors
- 修改centos的配置
cd /etc/docker/
vi daemon.json
{
"registry-mirrors": ["https://xxxxxx.mirror.aliyuncs.com"] # 填寫上面獲取的地址
}
#重新加載daemon
systemctl daemon-reload
#重啟docker生效
systemctl restart docker
問題
無法訪問
- 修改
settings.py
,允許其他 ip 訪問
ALLOWED_HOSTS = ["*"]
- 配置安全組
思考?
- 當我們輸入網址到訪問到docker容器中的項目,能說出解析過程么?
- 平常我們項目發布環境是django+uwsgi+nginx docker應該如何操作?
- 你能說出dockerfile的作用么?