碎碎念
新公司的第一個dockerfile就撞牆,
莫名的錯誤,裝了還是過不了,
寫這個程式的說也沒用過,他們都是用本機跑AI。
只好自己亂搞了…
正文
debug方式,都是直接去翻python的程式碼看裡面的內容。
這兩個大部分都用 ctypes.util.find_library 去找library。
然後程式內都有寫額外條件,當find_library找不到時,
可以從環境變數取值。
所以下面都是使用環境變數的方式,讓package能夠取得library。
python debug時的備忘事項
- python3 package 的位置在 /usr/local/lib/python3.10/site-packages
- find_library,根據同事的說法,應該是抓path的路徑下去做搜尋
然後開始看code吧。
alpine linux安裝enchant
解決 ‘enchant’ C library was not found
ImportError: The 'enchant' C library was not found and maybe needs to be installed.
See https://pyenchant.github.io/pyenchant/install.html
for details
其實有安裝 py3-enchant 就好了,
後續是因為找不到library才會噴錯。
apk add py3-enchant aspell aspell-en
所以設定一下環境變數,收工。
下面程式是dockerfile用的,如果要測試就把env改成export吧
ENV PYENCHANT_LIBRARY_PATH=/usr/lib/libenchant-2.so.2
ref.
Cannot find Enchant C library on Apple Silicon
alpine linux 安裝opencc
解決 libopencc.so.1 : No such file
Traceback (most recent call last):
File "/usr/local/app/Speech_control.py", line 3, in <module>
from opencc import OpenCC
File "/usr/local/lib/python3.10/site-packages/opencc.py", line 24, in <module>
libopencc = CDLL('libopencc.so.1', use_errno=True)
File "/usr/local/lib/python3.10/ctypes/__init__.py", line 374, in __init__
self._handle = _dlopen(self._name, mode)
OSError: Error loading shared library libopencc.so.1: No such file or directory
因為opencc在 repository的testing上面,
用下面的方法裝
apk add opencc --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/
好了後設定環境變數(這是dockerfile用的)
ENV LIBOPENCC=/usr/lib/libopencc.so.1.1
完整dockerfile
這個alpine linux , build出來 181.73MB
FROM python:alpine3.16
ENV PATH "$PATH:/usr/local/bin"
WORKDIR /usr/local/app
RUN apk update && apk add py3-enchant aspell aspell-en && apk add opencc --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/
ENV LIBOPENCC=/usr/lib/libopencc.so.1.1
ENV PYENCHANT_LIBRARY_PATH=/usr/lib/libenchant-2.so.2
COPY ./Chatbot_Speech /usr/local/app
RUN pip3 install --upgrade pip
RUN pip3 install --no-cache-dir -r requirements.txt
ENTRYPOINT ["python", "Speech.py"]
用ubuntu ,build 出來 511.74MB,
本來是6xx,後來清了一下cache,縮減一下步驟後,才變成511MB
FROM ubuntu:20.04
ENV PATH "$PATH:/usr/local/bin"
WORKDIR /usr/local/app
COPY ./Chatbot_Speech /usr/local/app
RUN apt-get update && apt-get install -y enchant && apt-get install -y python3.6 python3-pip && apt-get clean && rm -rf /var/lib/apt/lists/*
RUN pip3 install --upgrade pip
RUN pip3 install --no-cache-dir -r requirements.txt
ENTRYPOINT ["python3", "Speech.py"]
ref.
- 最佳化 Dockerfile - 精簡 image
0 意見:
張貼留言