步骤一:安装docker
步骤二:导出你的requirements.txt文件
步骤三:在你的python项目目录构建的dockerfile文件
# 使用基础镜像
FROM ubuntu:18.04# 更新并安装基本软件包
RUN apt-get update && apt-get install -y \
curl \
git \
&& rm -rf /var/lib/apt/lists/*# 安装Miniconda
RUN curl -o ~/miniconda.sh -O https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
chmod +x ~/miniconda.sh && \
~/miniconda.sh -b -p /opt/conda && \
rm ~/miniconda.sh && \
/opt/conda/bin/conda clean -ya# 将Conda的bin目录添加到环境变量中
ENV PATH /opt/conda/bin:$PATH
# 创建conda环境
RUN /opt/conda/bin/conda create -n myenv python=3.8 -y# 安装PyTorch和相关依赖
RUN /opt/conda/bin/conda run -n myenv conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 pytorch-cuda=11.6 -c pytorch -c nvidia# 设置工作目录并复制项目文件
WORKDIR /app
COPY . .
# 安装其他依赖
RUN /opt/conda/bin/conda run -n myenv conda install pyqt -y
RUN /opt/conda/bin/conda run -n myenv pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6 -y#RUN /opt/conda/bin/conda run -n myenv pip install pyqt5-tools
#RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
RUN apt-get update && apt-get install -y \
fontconfig \
&& rm -rf /var/lib/apt/lists/*
COPY ./fonts /usr/share/fonts/truetype/
RUN fc-cache -f -v
RUN /opt/conda/bin/conda run -n myenv pip install pillow
ENV QT_API=pyqt5
# 运行你的应用程序或服务
CMD ["/opt/conda/envs/myenv/bin/python", "./zhangtingting_17suo/Mytools/test.py"]
步骤四:执行你的镜像
sudo docker run --gpus all -it <镜像名>
可能会遇到的问题及解决方法:
问题一: ImportError: libGL.so.1: cannot open shared object file: No such file or directory
在dockerfile中添加下面命令:
RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6 -y
问题二:qtpy.QtBindingsNotFoundError: No Qt bindings could be found
解决办法:
在的dockerfile中执行下面命令(在安装requirements.txt环境之前执行)
RUN /opt/conda/bin/conda run -n myenv conda install pyqt -y
问题三:ERROR: failed to solve: process "/bin/sh -c /opt/conda/bin/conda run -n myenv conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.6 -c pytorch -c conda-forge -y" did not complete successfully: exit code: 1
解决办法;这个问题可能是由于你在镜像中安装的cuda版本与你的电脑版本不符合,需要根据你电脑的cuda版本在 Previous PyTorch Versions | PyTorch
网站中找到合适的cuda版本的安装命令,添加到dockerfile文件中
还有可能是你的执行镜像的命令不对,执行docker run命令的时候需要添加--gpus all的参数。
问题四:OSError: cannot open resource
这一般是你镜像中字体的问题,查看你的错误日志,找到你的字体路径,确保该路径可找到并且包含你要用的字体文件!
比如说下面是我的错误日志:Traceback (most recent call last):
File "./xxx/Mytools/test.py", line 721, in <module>
s.angle_detect()
File "./xxx/Mytools/test.py", line 645, in angle_detect
annotator.box_label(xyxy, label, color=colors(c, True))
File "/app/xxx/Mytools/utils/plots.py", line 151, in box_label
self.im = cv2ImgAddText(self.im, label, p1[0], p1[1] - h , textColor=(0,0,0), textSize=h)
File "/app/xxx/Mytools/utils/plots.py", line 76, in cv2ImgAddText
fontStyle = ImageFont.truetype("../segment/simsun.ttc", textSize, encoding="utf-8")
File "/opt/conda/envs/myenv/lib/python3.8/site-packages/PIL/ImageFont.py", line 834, in truetype
return freetype(font)
File "/opt/conda/envs/myenv/lib/python3.8/site-packages/PIL/ImageFont.py", line 831, in freetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "/opt/conda/envs/myenv/lib/python3.8/site-packages/PIL/ImageFont.py", line 257, in __init__
self.font = core.getfont(
OSError: cannot open resource
从报错信息上看,在我的plots.py中的 fontStyle = ImageFont.truetype("../segment/simsun.ttc", textSize, encoding="utf-8")语句报错,我的字体路径是../segment/simsun.ttc,但是该路径 并没有索引到,因此我将我的字体文件simsun.tcc复制到可以索引到的路径下,并修改该语句的指定绝对路径下!