目录
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能创建一个满足工程所需的独立镜像。
docekr version
docker info
docker run hello-world
下面的目录结构是一个普通的django项目案例,项目名为web01,app名为product.
注意:项目根目录创建了dockerfile,pip.conf, requirements.txt 三个文件
PyMySQL==0.9.3
redis==3.2.1
request==2019.4.13
django==2.1.8
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"]
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
PyMySQL==0.9.3
redis==3.2.1
request==2019.4.13
django==2.1.8
没有必要逐一写依赖,使用如下命令可把本地虚拟环境中的所有包生成到项目中
pip freeze > requirements.txt
# 注意后面的 . 不能省略
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
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 = ["*"]
本文链接:http://task.lmcjl.com/news/16102.html