目录
完整的Dockerfile在最后自行参考,Chromedriver按需要配置。
NLTK官网
Github地址
GitHub - nltk/nltk_data: NLTK Data
命令行安装
此处提到命令下载:
python -m nltk.downloader all
sudo python -m nltk.downloader -d /usr/local/share/nltk_data all
配置到Dockerfile中之后运行报错
docker build -t python:nltk .
Cannot assign requested address 无法分配请求地址
手动安装
查看手动下载地址
https://www.nltk.org/nltk_data/
根据官网描述,下载后zip文件需要解压到对应的目录下。例如:https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/packages/corpora/brown.zip
解压到 nltk_data/corpora/brown
目录。
尝试使用下载器下载链接
https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/packages/corpora/brown.zip
下载器报错
无法分配请求的地址,该地址无效
报错信息结论
说明域名无法访问,此时我们使用ping命令测试一下连通性
由此可见域名解析出了问题,需要解决DNS解析问题之后就可以正常访问了。
解决DNS解析问题
查看需要安装包的下载链接
查看下载链接域名信息
https://www.ipaddress.com/site/raw.githubusercontent.com
找到域名解析的IPV4地址
再用ping命令测试一下
修改HOSTS文件
说明ip没问题,最后以管理员身份打开HOSTS文件
Windows: C:\Windows\System32\drivers\etc\HOSTS
Linux: /etc/hosts
添加以下配置
185.199.108.133 raw.githubusercontent.com
185.199.109.133 raw.githubusercontent.com
185.199.110.133 raw.githubusercontent.com
185.199.111.133 raw.githubusercontent.com
再ping一次域名
再次执行Docker命令
docker build -t python:nltk .
建议指定类型安装NLTK Data可以减少image大小,避免安装失败。
# 建议安装指定nltk data
RUN python3 -W ignore -m nltk.downloader -d /root/nltk.data punkt
RUN python3 -W ignore -m nltk.downloader -d /root/nltk.data stopwords
完整Dockerfile
FROM python:3.9
RUN cd && mkdir .pip && mkdir .ssh
COPY ./pip.ini /root/.pip/pip.conf
# 配置时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
RUN sed -i s@/security.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
RUN sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list
# 配置Google sign key
RUN wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
# 准备工作
RUN apt-get clean
RUN apt-get update
RUN apt-get install git openssl
RUN apt-get clean
RUN apt-get install -y google-chrome-stable
# 下载chromedriver
RUN apt-get install -yqq unzip
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
# 解压
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
RUN rm -rf /tmp/chromedriver.zip
RUN apt remove -y unzip
RUN chmod 777 -R /opt/
# 安装NLTK
RUN pip3 install --upgrade pip && pip3 install wheel && pip3 install nltk
# 下载nltk data(以下几种方式都可以,-d参数后面必须跟目录)
# RUN python3 -m nltk.downloader all
# RUN python3 -m nltk.downloader -d /root/nltk.data all
# RUN python3 -c "import nltk; nltk.download('all')"
# 忽略Warning日志
# RUN python3 -W ignore -m nltk.downloader -d /root/nltk.data all
# 安装指定nltk data
RUN python3 -W ignore -m nltk.downloader -d /root/nltk.data punkt
RUN python3 -W ignore -m nltk.downloader -d /root/nltk.data stopwords