Ubuntu平台上(其他Linux平台没测试过),运行Docker的GUI是很简单的事情,只要共享DISPLAY变量挂在/tmp/.X11-unix,并且开放控制X server的访问权限即可。

1、案例运行

在宿主机上运行命令

$ xhost +

此时就可以使用docker的图形界面了,比如测试如下:

$ sudo docker run -it -e DISPLAY=unix$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix lorahz/qtclient:0.2

2、详细步骤

2.1 找到一个简单的QT 应用

qtclient

qtclient 和 dockerfile 详细见 https://github.com/panyingyun/qtclient.git.

2.2 编写 Dockerfile 文件
FROM amd64/ubuntu:20.04

COPY ./sources.list /etc/apt/

ENV DEBIAN_FRONTEND=noninteractive

RUN apt update && apt install -y build-essential wget zlib1g-dev vim  gfortran g++ gcc make cmake qt5-default \
    qtcreator libgl1-mesa-dev libx11-dev libxcb-keysyms1-dev libxcb-image0-dev \
    libxcb-shm0-dev libxcb-icccm4-dev libxcb-sync0-dev libxcb-xfixes0-dev \
    libxcb-shape0-dev libxcb-randr0-dev libxcb-render-util0-dev \
    libfontconfig1-dev libfreetype6-dev libx11-dev libxext-dev libxfixes-dev \
    libxi-dev libxrender-dev libxcb1-dev libx11-xcb-dev libxcb-glx0-dev x11vnc \
    xauth build-essential mesa-common-dev libglu1-mesa-dev libxkbcommon-dev \
    libxcb-xkb-dev libxslt1-dev libxkbcommon-x11-0 freeglut3-dev

RUN apt install -y language-pack-zh-han* language-pack-gnome-zh-han*
RUN apt install $(check-language-support) -y

ENV QT_DEBUG_PLUGINS=1
ENV LANG="zh_CN.UTF-8"
ENV LANGUAGE="zh_CN:zh:en_US:en"

WORKDIR /opt/

COPY ./qtclient  .

RUN /bin/sh auto_build_linux.sh

RUN mv ./build/qtclient /usr/local/bin && /bin/chmod -R 755 /usr/local/bin/qtclient

CMD ["qtclient"]

参考

Running gui in docker

https://blog.csdn.net/struct_slllp_main/article/details/112620111

Running qtcreator in Docker

https://nicroland.wordpress.com/2015/12/06/running-qtcreator-in-docker/

Ubuntu官方docker镜像:

https://hub.docker.com/r/amd64/ubuntu/tags?page=1&ordering=last_updated

中文乱码:

https://blog.csdn.net/waplys/article/details/88823969