一、需求
1. 制作一個圖像辨識的api,用到相同設置的ubuntu鏡像,但是每次制作都要更新ubuntu和下載tesseract浪費半個到一個小時下載,所以制作一個自定義ubuntu幾次鏡像大大提高開發效率。
2. 制作ubuntu過程時,可以調試tesseract是否能用,對于ubuntu需要下載很多庫時,很有幫助。
二、ubuntu鏡像制作
a.ubuntu下載
# 拉原始鏡像
docker pull ubuntu# 進入鏡像操作,添加需要的庫
docker run -it ubuntu /bin/bash
?b.第三方庫下載
# apt 換源
sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
apt-get clean
apt-get update# 時區設置
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'Asia/Shanghai' >/etc/timezone# python相關apt-get install -y python3
apt-get install -y python3-pip
pip install pillow -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pillow-PIL -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple# 新 tesseract
apt-get install tesseract-ocr -y
apt-get install -y libgl1-mesa-dev
ps:
1. requirements.txt 文件需要從外部復制進來,可以用命令
docker cp 主機路徑/test.txt 容器ID:/test/
2. ubuntu 更新換源是個大坑,很多錯誤的教程,上面命令親自測試,有效!!!
2022/02/24?好像過期了,無法換源
sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
apt-get clean
apt-get update
?參考:Docker ubuntu apt-get更換國內源解決Dockerfile構建速度過慢_weixin_33936401的博客-CSDN博客
3.鏡像下載在docker軟件選項里設置換源(windows),網上教程正確。
三、提交到新的ubuntu鏡像
生成鏡像的命令
docker commit -m "description" -a "author" <容器id> repository:tag
? reposiory:鏡像倉庫名,任取即可。
? tag:鏡像標簽名,任取即可。
能查看到鏡像:
參考:在Docker下構建一個自己的Ubuntu鏡像_NelsonCheung的博客-CSDN博客
四、dockerfile引用
from awaly/ubuntu:tesseractADD . /app
WORKDIR /appCMD ["python3", "/app/main.py"]
這樣修改code后,一分鐘內就生成新的docker