docker快速部署爬虫

Docker部署Python爬虫

场景描述:我们使用一个简单的python爬虫部署,本项目是获取百度首页代码。如何实现Docker安装部署。

第一步: Win10下创建目录文本
选择在D盘下创建docker目录,分别新建三个文件:Dockerfile,app.py,requirements.txt
Dockerfile(没有后缀):一个文本文件,包含了一条条的指令(Instruction),每一条指令构建一层,因此每一条指令的内容,就是描述该层应当如何构建。创建镜像必须文件。
打开dockerfile
将一下的文本内容从复制粘贴到Dockerfile中:

# 基于镜像基础
 FROM python:3.7
 # 设置代码文件夹工作目录 /app 
 WORKDIR /app 
# 复制当前代码文件到容器中/app
 ADD . /app 
# 安装所需的包 
RUN pip install -r requirements.txt 
# Run app.py when the container launches 
CMD ["python", "app.py"]

app.py:python项目的源代码,这里测试的单个python文件,如果是一个完整项目,可以将整个文件夹拷贝到这里。

import requests
#获取百度首页源代码
resonse = requests.get('http://www.baidu.com')
resonse.encoding='utf-8'
print(resonse.text)

requirements.txt :所需要的插件,即所需要的库文件,以python为例,其获取方法是cmd命令,进入到【D:\docker】目录,执行命令:pip freeze > requirements.txt

第二步:生成镜像。本文采用的windows环境。docker build -t test .命令中最后的点不要忘记,这里表示当前目录`
# -t 给容器取个名字,容器默认名字是none
docker build -t test .

第三步:查看镜像是否生成 docker images

第四步:运行镜像程序,这里可以看到运行结果 docker run test

出错:
D:\docker>docker build -t test .
Sending build context to Docker daemon  8.704kB
Step 1/5 : FROM python:3.6
Get https://registry-1.docker.io/v2/: net/http: TLS handshake timeout
解决方法:
修改国内阿里云镜像
镜像获取:阿里云镜像需要注册账号才能获取  镜像加速器里复制就行

$ docker-machine start default
Starting "default"...
Machine "default" is already running.

$ docker-machine ssh default
   ( '>')
  /) TC (\   Core is distributed with ABSOLUTELY NO WARRANTY.
 (/-_--_-\)           www.tinycorelinux.net

docker@default:~$/var/lib/boot2docker/
-bash: /var/lib/boot2docker: Is a directory
docker@default:~$ cd /etc/docker/
docker@default:/etc/docker$ ls
key.json
#Docker中国区官方镜像 https://registry.docker-cn.com
#http://f1361db2.m.daocloud.io
sudo sed -i “s|EXTRA_ARGS=’|EXTRA_ARGS=’–registry-mirror=https://h7oy8ket.mirror.aliyuncs.com |g” /var/lib/boot2docker/profile # 修改加速地址【注意要把加速地址改一下,阿里云的地址参考上面的官方文档获得】

cat /var/lib/boot2docker/profile

退出,重启Docker服务
exit
docker-machine restart default

又出现问题:
$ docker info
error during connect: Get https://192.168.99.100:2376/v1.37/info: dial tcp 192.168.99.100:2376: connectex: No connection could be made because the target machine actively refused it.
网上给出的解决方法是:
cd "C:\Program Files\Docker\Docker"
./DockerCli.exe -SwitchDaemon
但是我没看懂,或者说不明白怎么弄吧

这时就卸载软件,重新安装:
结果却成功了:运行docker build -t test .

出现3.6: Pulling from library/python 正在下载相关文件包。
成功了!
Successfully built 70dc4e4770fc
Successfully tagged test:latest
查看镜像:docker images

D:\docker>docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                latest              70dc4e4770fc        33 seconds ago      921MB
python              3.6                 5bf410ee7bb2        3 weeks ago         913MB

最后docker run test 成功!

源码如下:

<!DOCTYPE html>

<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

2019-12-18T13:33:32.579738223Z 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

影雀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值