采用docker部署含有qt界面的Python项目教程

1、创建一个简单的qt界面的Python项目,参考一下链接方法创建!

pyinstaller系列之九:构建GUI的docker镜像,docker运行GUI程序。_pyinstaller dockerfile-CSDN博客

2、参考下面示例修改dockerfile文件:

FROM ubuntu:18.04 #此项目建立在ubuntu18:04的基本环境中

ENV DEBIAN_FRONTEND=noninteractive ## 设置为”noninteractive”,可以直接运行命令,无需向用户请求输入

#在Ubuntu系统中安装基本的环境依赖
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip \
    python3-pyqt5 \
    python3-pyqt5.qtwebengine \
    language-pack-zh* \
    ttf-wqy-microhei \
    x11-apps

#删掉不必要的东西减小镜像大小

RUN rm -rf /var/lib/apt/lists/*

RUN python3 -m pip install -U pip setuptools wheel

#创建工作文件夹
RUN mkdir /root/workdir

#指定工作目录,就是你的镜像运行在什么样的基础目录中
WORKDIR /root/workdir

#将Python代码从宿主机中拷贝到docker镜像中

COPY ./docker_python/python_qt1.py /root/workdir/ui_demo/python_qt1.py

#将requirements.txt从宿主机中拷贝到docker镜像中
COPY ./docker_python/requirements.txt /root/workdir/ui_demo/requirements.txt

#运行pip安装Python项目所需要的环境依赖

RUN pip install -r /root/workdir/ui_demo/requirements.txt

# 中文编码设置
RUN locale-gen zh_CN.UTF-8
RUN update-locale LANG=zh_CN.UTF-8 LANGUAGE=zh_CN.UTF-8 LC_ALL=zh_CN.UTF-8
ENV LANG zh_CN.UTF-8

#将display设置为0,用于指定系统显示设备

ENV DISPLAY=:0

#CMD命令就是在运行这个镜像的时候需要执行的命令

CMD ["python3", "/root/workdir/ui_demo/python_qt1.py"]

 

3、构建自己的docker镜像,在Dockerfile文件所在的目录中运行终端执行以下命令

sudo docker build . 

等待镜像构建完成,注:此时可能会出现很多错误,在此列举一些常见的错误及其解决方法:

问题1:Ubuntu_apt-update_Certificate-verification-failed 

解决方法:Ubuntu_apt-update_Certificate-verification-failed-CSDN博客

问题2:Certificate verification failed: The certificate is NOT trusted.

解决方法:Certificate verification failed: The certificate is NOT trusted.-CSDN博客

问题3:docker容器内使用apt报错 E: List directory /var/lib/apt/lists/partial is missing. - Acquire (13: Permission)

解决方法:docker容器内使用apt报错 E: List directory /var/lib/apt/lists/partial is missing. - Acquire (13: Permission)-CSDN博客

4、执行所构建的docker镜像

docker run --rm -it   --name my-running-app   -e DISPLAY=$DISPLAY   -v /tmp/.X11-unix:/tmp/.X11-unix qt_docker

注:问题1:通常直接执行这条命令可能会出现qt.qpa.xcb: could not connect to display qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.这个错误

解决方法: 

1、 xhost +local:#此命令时授予X-server访问权限

2、ps aux | grep X   #查看宿主机的X-server是否成功运行,如果 X Server 正在运行,应该会看到类似于 Xorg 或者 X 的进程在列表中。

3、echo $DISPLAY     #检查 DISPLAY 环境变量是否设置正确,可以在终端中执行以下命令这条命令会输出当前 shell 中 DISPLAY 环境变量的内容。通常情况下,它会显示类似于 :0:1 等的内容,这表示当前正在使用的 X Server 的显示号码。

执行完上面命令的时候,查看我们的X-server是否配置成功

执行下面的命令:

xeyes
执行该命令会先出一个眼睛图标,表示设置成功

4、授予docker容器用户访问权限

xhost +SI:localuser:USERNAME
一般username默认为root

执行完上面命令之后,再次执行下面命令即可成功显示qt界面

 docker run --rm -it   --name my-running-app   -e DISPLAY=$DISPLAY   -v /tmp/.X11-unix:/tmp/.X11-unix <镜像名>

 

 

 

 

  • 20
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值