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 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章