From 9c39e3dc7502eabd507ac0e41db8531d89d0cc28 Mon Sep 17 00:00:00 2001 From: Renzhi Date: Fri, 20 Dec 2019 22:15:29 +0800 Subject: [PATCH 1/6] =?UTF-8?q?fix:=E5=88=A9=E7=94=A8docker=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=8A=A0=E9=80=9F=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 5 +++++ Dockerfile37 | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.dockerignore b/.dockerignore index 9e3882d..e3fc22b 100755 --- a/.dockerignore +++ b/.dockerignore @@ -5,5 +5,10 @@ **/*~ **/.DS_Store **/Thumbs.db +*.png .idea/ .git/ +.github/ +*.md +UnitTest/ +uml/ diff --git a/Dockerfile37 b/Dockerfile37 index d00eeab..8a6770a 100644 --- a/Dockerfile37 +++ b/Dockerfile37 @@ -9,7 +9,6 @@ echo 'deb http://mirrors.163.com/debian-security/ jessie/updates main non-free c RUN apt-get -y update -ADD . /usr/src/app WORKDIR /usr/src/app RUN wget -c https://dl.lancdn.com/landian/soft/chrome/m/77.0.3865.120_amd64.deb && \ @@ -28,11 +27,14 @@ unzip chromedriver_linux64.zip ENV DEBIAN_FRONTEND noninteractive ENV TZ Asia/Shanghai +COPY requirements.txt /usr/src/app/ ## install python requirements RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider --no-cache-dir -r requirements.txt RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone +COPY . /usr/src/app + ## install ntpdate, not accept but saving code #RUN echo 'deb http://mirrors.163.com/debian/ jessie main non-free contrib \ # deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib \ @@ -43,5 +45,5 @@ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone #EXPOSE 5010 -CMD [ "python", "run.py" ] +CMD [ "python", "run.py", "r" ] #ENTRYPOINT [ "python", "run.py" ] From 33a6d7be73adaa762ce936ca0d66d151a7e0ebef Mon Sep 17 00:00:00 2001 From: Renzhi Date: Fri, 20 Dec 2019 22:56:29 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=9C=A8docker=E4=B8=AD?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E7=9A=84=E6=AD=A5=E9=AA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 19 ++++++++++--------- docker-compose.yml | 32 +++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 8dd28cb..7fa31e0 100755 --- a/README.md +++ b/README.md @@ -62,15 +62,16 @@ positional arguments: operate r: 运行抢票程序, c: 过滤cdn, t: 测试邮箱和server酱,server酱 ``` - - 如果你的服务器安装了docker与docker-compose, 那么就可以通过`docker-compose`进行启动,`docker.sh`脚本对此进行了封装,可以通过如下命令进行启动 - - 1、`sudo ./docker.sh run` #创建一个镜像并启动容器,如果镜像已经创建过了会直接启动容器。 - - 2、`sudo ./docker.sh restart` #修改配置文件后,通过此名命令可重新加载容器运行 - - 3、`sudo ./docker.sh rm` #删除容器 - - 4、`sudo ./docker.sh drun` #后台运行容器 - - 5、`sudo ./docker.sh logs` #在后台运行时,通过此命令查看运行的内容 - - 注: 针对没有docker环境的同学提供了docker安装脚本(**centos7**) - - `sudo ./docker_install_centos.sh` - - ~~注: 若只有docker没有docker-compose. 可通过`pip install docker-compose`进行下载~~ + - 如果你的服务器安装了docker与docker-compose, 那么你可以忽略上面的所有步骤,直接按以下步骤操作,即可开始抢票: + - 前提条件: + - 请确认你安装的docker版本为18.09及以上: `docker -v` + - 请确认你安装的docker-compose版本为1.23.2及以上: `docker-compose -v` + - 请根据自己需要修改好配置文件:`TickerConfig.py` + - 请修改配置文件`TickerConfig.py`中的变量`AUTO_CODE_TYPE`和`HOST`,`AUTO_CODE_TYPE`改为`3`, HOST改为`"captcha:80"`(这里很重要,这是本地打码服务器的配置) + - 运行命令: + - 开始抢票:`docker-compose up --build -d` + - 停止抢票:`docker-compose down` + - 查看抢票log: `docker logs --follow ticket` #### 目录对应说明 - agency - cdn代理 diff --git a/docker-compose.yml b/docker-compose.yml index 9179411..0edc397 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,14 +1,28 @@ version: "3" services: + #抢票服务 ticket: - image: 12306_ticket - build: . - # ports: - volumes: - - ./:/usr/src/app - # - /etc/localtime:/etc/localtime:ro - container_name: 12306ticket - privileged: true - + build: + context: . + dockerfile: ./Dockerfile37 + image: ticket:v1.2.004 + container_name: ticket + depends_on: + - captcha + networks: + - 12306network + restart: on-failure + #打码服务器 + captcha: + image: yinaoxiong/12306_code_server:amd64 #请根据需要修改image + environment: + - WORKERS=1 #gunicorn works 默认为1可以根据服务器配置自行调整 + container_name: captcha + networks: + - 12306network + restart: unless-stopped +networks: + 12306network: + driver: bridge From b266700fcdbe08bdf0655fe5b792b96684cdb7f8 Mon Sep 17 00:00:00 2001 From: Renzhi Date: Sat, 21 Dec 2019 00:50:19 +0800 Subject: [PATCH 3/6] fix:python output non-buffered --- docker-compose.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 0edc397..d84b20a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,6 +7,8 @@ services: context: . dockerfile: ./Dockerfile37 image: ticket:v1.2.004 + environment: + - PYTHONUNBUFFERED=1 container_name: ticket depends_on: - captcha From e907e968f2141b3e2fd958c65c12cb7d89e28329 Mon Sep 17 00:00:00 2001 From: Renzhi Date: Sat, 21 Dec 2019 00:52:29 +0800 Subject: [PATCH 4/6] update: do update cdn list before run --- Dockerfile37 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile37 b/Dockerfile37 index 8a6770a..5c0930f 100644 --- a/Dockerfile37 +++ b/Dockerfile37 @@ -45,5 +45,5 @@ COPY . /usr/src/app #EXPOSE 5010 -CMD [ "python", "run.py", "r" ] +CMD [ "sh", "-c", "python run.py c && python run.py r" ] #ENTRYPOINT [ "python", "run.py" ] From 93c6db471ce520f383bd854a971d76386911a896 Mon Sep 17 00:00:00 2001 From: Renzhi Date: Thu, 26 Dec 2019 16:27:37 +0800 Subject: [PATCH 5/6] Optimization: optimize docker images size from over 2.2GB to about 760MB --- .dockerignore | 1 + Dockerfile37 | 74 ++++++++++++++++++++------------------- README.md | 2 +- docker-compose.yml | 2 ++ docker.sh | 37 -------------------- inter/GetRandCode.py | 3 +- requirements-docker37.txt | 7 ++++ 7 files changed, 51 insertions(+), 75 deletions(-) delete mode 100755 docker.sh create mode 100755 requirements-docker37.txt diff --git a/.dockerignore b/.dockerignore index e3fc22b..e18c7ff 100755 --- a/.dockerignore +++ b/.dockerignore @@ -12,3 +12,4 @@ *.md UnitTest/ uml/ +*.h5 diff --git a/Dockerfile37 b/Dockerfile37 index 5c0930f..3018e53 100644 --- a/Dockerfile37 +++ b/Dockerfile37 @@ -1,49 +1,51 @@ -FROM python:3.7 +FROM python:3.7-slim-buster ARG CDV=77.0.3865.40 -RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \ -echo 'deb http://mirrors.163.com/debian/ jessie main non-free contrib' > /etc/apt/sources.list && \ -echo 'deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib' >> /etc/apt/sources.list && \ -echo 'deb http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib' >> /etc/apt/sources.list +RUN sed -i 's/deb.debian.org/ftp.cn.debian.org/g' /etc/apt/sources.list -RUN apt-get -y update +RUN apt-get -y update && apt-get install -y \ + fonts-liberation \ + libappindicator3-1 \ + libasound2 \ + libatk-bridge2.0-0 \ + libatk1.0-0 \ + libatspi2.0-0 \ + libcups2 \ + libdbus-1-3 \ + libgtk-3-0 \ + libnspr4 \ + libnss3 \ + libx11-xcb1 \ + libxcomposite1 \ + libxcursor1 \ + libxdamage1 \ + libxfixes3 \ + libxi6 \ + libxrandr2 \ + libxss1 \ + libxtst6 \ + lsb-release \ + unzip \ + wget \ + xdg-utils \ + && apt-get clean && rm -rf /var/lib/apt/lists/* + +ENV TZ Asia/Shanghai +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone WORKDIR /usr/src/app -RUN wget -c https://dl.lancdn.com/landian/soft/chrome/m/77.0.3865.120_amd64.deb && \ -apt-get install -y adwaita-icon-theme at-spi2-core dbus dconf-gsettings-backend dconf-service fonts-liberation glib-networking glib-networking-common glib-networking-services gsettings-desktop-schemas libappindicator3-1 libasound2 libasound2-data libatk-bridge2.0-0 libatk1.0-0 libatk1.0-data libatspi2.0-0 libavahi-client3 libavahi-common-data libavahi-common3 libcolord2 libcups2 libdbus-1-3 libdbusmenu-glib4 libdbusmenu-gtk3-4 libdconf1 libdrm2 libfile-basedir-perl libfile-desktopentry-perl libfile-mimeinfo-perl libfontenc1 libgl1-mesa-glx libglapi-mesa libgnutls-deb0-28 libgtk-3-0 libgtk-3-bin libgtk-3-common libhogweed2 libindicator3-7 libjson-glib-1.0-0 libjson-glib-1.0-common libnettle4 libnspr4 libnss3 libproxy1 librest-0.7-0 libsoup-gnome2.4-1 libsoup2.4-1 libwayland-client0 libwayland-cursor0 libx11-protocol-perl libx11-xcb1 libxaw7 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-shape0 libxcb-sync1 libxcomposite1 libxcursor1 libxdamage1 libxfixes3 libxi6 libxinerama1 libxkbcommon0 libxmu6 libxmuu1 libxpm4 libxrandr2 libxshmfence1 libxtst6 libxv1 libxxf86dga1 libxxf86vm1 multiarch-support x11-utils x11-xserver-utils xdg-utils xkb-data libgconf2-4 lsb-release && \ -dpkg -i 77.0.3865.120_amd64.deb && \ -wget -c http://npm.taobao.org/mirrors/chromedriver/LATEST_RELEASE +RUN wget -q https://dl.lancdn.com/landian/soft/chrome/m/77.0.3865.120_amd64.deb && \ + dpkg -i 77.0.3865.120_amd64.deb && rm -f 77.0.3865.120_amd64.deb - -RUN wget -c https://npm.taobao.org/mirrors/chromedriver/$CDV/chromedriver_linux64.zip && \ -apt-get install -y unzip && \ -unzip chromedriver_linux64.zip - - - - -ENV DEBIAN_FRONTEND noninteractive -ENV TZ Asia/Shanghai - -COPY requirements.txt /usr/src/app/ +RUN wget -q https://npm.taobao.org/mirrors/chromedriver/$CDV/chromedriver_linux64.zip && \ + unzip chromedriver_linux64.zip && rm -f chromedriver_linux64.zip ## install python requirements -RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider --no-cache-dir -r requirements.txt -RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone +COPY requirements-docker37.txt ./ +RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --no-cache-dir -r requirements-docker37.txt -COPY . /usr/src/app - -## install ntpdate, not accept but saving code -#RUN echo 'deb http://mirrors.163.com/debian/ jessie main non-free contrib \ -# deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib \ -# deb http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib' > /etc/apt/sources.list \ -# && apt-get update\ -# && apt-get install ntpdate -y \ - - -#EXPOSE 5010 +COPY . . CMD [ "sh", "-c", "python run.py c && python run.py r" ] -#ENTRYPOINT [ "python", "run.py" ] diff --git a/README.md b/README.md index 7fa31e0..ef646fa 100755 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ positional arguments: operate r: 运行抢票程序, c: 过滤cdn, t: 测试邮箱和server酱,server酱 ``` - - 如果你的服务器安装了docker与docker-compose, 那么你可以忽略上面的所有步骤,直接按以下步骤操作,即可开始抢票: + - 如果你的服务器安装了docker与docker-compose, 那么你可以忽略上面的**所有**步骤,直接按以下步骤操作,即可开始抢票: - 前提条件: - 请确认你安装的docker版本为18.09及以上: `docker -v` - 请确认你安装的docker-compose版本为1.23.2及以上: `docker-compose -v` diff --git a/docker-compose.yml b/docker-compose.yml index d84b20a..502059c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,7 @@ services: image: ticket:v1.2.004 environment: - PYTHONUNBUFFERED=1 + - CAPTCHALOCAL=1 container_name: ticket depends_on: - captcha @@ -20,6 +21,7 @@ services: image: yinaoxiong/12306_code_server:amd64 #请根据需要修改image environment: - WORKERS=1 #gunicorn works 默认为1可以根据服务器配置自行调整 + - PYTHONUNBUFFERED=1 container_name: captcha networks: - 12306network diff --git a/docker.sh b/docker.sh deleted file mode 100755 index 48e32fb..0000000 --- a/docker.sh +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/bash -#author: tan -#date: 2019-01-07 -# a script to control docker's container create restart reload and rm function - -function checkSudo (){ - if [ $UID -ne 0 ];then - echo -e 'it must be root!' - echo -e 'usage ./docker.sh {run|restart|rm|logs}' - exit 1 - fi -} - -checkSudo - -if [ $# -eq 1 ];then - - case $1 in - "run") - docker-compose up;; - - "restart") - docker-compose up --no-recreate;; - - "rm") - docker-compose rm -f;; - - "drun") - docker-compose up -d;; - "logs") - docker-compose logs;; - - *) - echo -e 'usage ./docker.sh {run|restart|rm|logs}';; - esac - -fi diff --git a/inter/GetRandCode.py b/inter/GetRandCode.py index 43a04c5..36d70ef 100644 --- a/inter/GetRandCode.py +++ b/inter/GetRandCode.py @@ -5,6 +5,7 @@ from config.urlConf import urls from myUrllib.httpUtils import HTTPClient from verify.localVerifyCode import Verify import TickerConfig +import os if TickerConfig.AUTO_CODE_TYPE == 2: @@ -25,7 +26,7 @@ def getRandCode(is_auto_code, auto_code_type, result): Result = v.verify(result) return codexy(Ofset=Result, is_raw_input=False) elif auto_code_type == 3: - print("您已设置使用云打码,但是服务器资源有限,请尽快改为本地打码") + 'CAPTCHALOCAL' not in os.environ and print("您已设置使用云打码,但是服务器资源有限,请尽快改为本地打码") http = HTTPClient(0) Result = http.send(urls.get("autoVerifyImage"), {"imageFile": result}) if Result and Result.get("code") is 0: diff --git a/requirements-docker37.txt b/requirements-docker37.txt new file mode 100755 index 0000000..fc6babf --- /dev/null +++ b/requirements-docker37.txt @@ -0,0 +1,7 @@ +bs4==0.0.1 +requests==2.18.4 +Pillow +wrapcache==1.0.8 +ntplib==0.3.3 +selenium==3.11.0 +fake-useragent==0.1.11 \ No newline at end of file From e315abb586338b20e4a0f4097e85bb6c8e640797 Mon Sep 17 00:00:00 2001 From: Renzhi Date: Thu, 26 Dec 2019 17:28:15 +0800 Subject: [PATCH 6/6] code style meet standard --- inter/GetRandCode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inter/GetRandCode.py b/inter/GetRandCode.py index 36d70ef..4d4034f 100644 --- a/inter/GetRandCode.py +++ b/inter/GetRandCode.py @@ -26,7 +26,7 @@ def getRandCode(is_auto_code, auto_code_type, result): Result = v.verify(result) return codexy(Ofset=Result, is_raw_input=False) elif auto_code_type == 3: - 'CAPTCHALOCAL' not in os.environ and print("您已设置使用云打码,但是服务器资源有限,请尽快改为本地打码") + print("您已设置使用云打码,但是服务器资源有限,请尽快改为本地打码" if "CAPTCHALOCAL" not in os.environ else "已设置本地打码服务器") http = HTTPClient(0) Result = http.send(urls.get("autoVerifyImage"), {"imageFile": result}) if Result and Result.get("code") is 0: