docker搭建QtCreator开发环境

本文介绍了如何使用Docker构建一个基于Ubuntu 16.04的Qt开发环境镜像,详细步骤包括编写Dockerfile,安装Qt,解决运行Qt应用时可能出现的问题,并分享了如何优化镜像和运行QtCreator的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当我们在开发一个新项目或者有新员工入职时,往往会为折腾开发环境而折腾好久。但docker则可以为我们提供这样一个标准化的镜像方案,大家只需拉取开发镜像即可,而无需去从头配置开发环境。本文以配置ubuntu16.04的Qt开发环境为例加以说明,如何制作一个开发镜像。

工具准备

  1. docker
  2. Qt的安装文件Qt Downloads,该示例中使用的是:qt-opensource-linux-x64-5.12.6.run
  3. 本例中验证的宿主机环境:
    No LSB modules are available.
    Distributor ID:	Ubuntu
    Description:	Ubuntu 16.04.6 LTS
    Release:	16.04
    Codename:	xenial
    

Dockerfile

# 基础镜像
FROM ubuntu:16.04

# 元数据
LABEL maintainer="fzzjoy" email="xxxxx@email.com"

# 从构建上下文目录中的文件复制到新的一层的镜像内的位置
COPY qt-opensource-linux-x64-5.12.6.run /tmp/qt/

# 安装依赖 (可以扩展)
RUN apt-get update \
    && apt-get install -y \
    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 \
    libgstreamer-plugins-base0.10-dev \
    libxkbcommon-x11-0 \
    && chmod +x /tmp/qt/qt-opensource-linux-x64-5.12.6.run

# 指定docker run时执行的程序
ENTRYPOINT /tmp/qt/qt-opensource-linux-x64-5.12.6.run

构建安装Qt

# Build base image (在Dockerfile的目录下执行)
docker build -t qt:base .

# N.B. This is an important step any time you're running GUIs in containers
xhost local:root

# Run installation wizard, save to new image, delete left over container
docker run -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY -v /dev/shm:/dev/shm --device /dev/dri --name qt_install --entrypoint /tmp/qt/qt-opensource-linux-x64-5.12.6.run qt:base
docker commit qt_install qt:latest
docker rm qt_install

运行QtCreator

docker run

  • -v:挂载宿主机路径到容器的相关路径。
  • -e:设置容器环境变量
# Then you can run QtCreator with this monster of a command
docker run -v /tmp/.X11-unix:/tmp/.X11-unix \
           -e DISPLAY=$DISPLAY \
           -v /dev/shm:/dev/shm \
           -v ~/src:/root \		# 挂载宿主机的~/src目录(该目录可以作为工作代码目录)到容器的root目录下。
           --device /dev/dri \  # 挂载宿主机的device到容器中
           --name qt_creator \  # 运行起来的容器名 
           --rm \				# 运行结束后删除容器 
           --entrypoint /opt/Qt5.12.6/Tools/QtCreator/bin/qtcreator \ # 指定容器运行的默认程序
           qt:latest			# 容器运行所采用的 镜像:TAG

可能遇到的问题

Q1:

fzz@ubuntu:~/src/docker-qt-creator$ docker run -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY -v /dev/shm:/dev/shm -v ~/src:/root --device /dev/dri --name qt_creator --rm --entrypoint /opt/Qt5.12.6/Tools/QtCreator/bin/qtcreator qt:latest
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, xcb.

A:
可以通过运行终端程序进入容器内

docker run -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY -v /dev/shm:/dev/shm -v ~/src:/root --device /dev/dri --name qt_creator -it --rm --entrypoint /bin/bash qt:latest

然后声明环境变量:

export QT_DEBUG_PLUGINS=1

运行程序查看缺少的是哪个依赖,安装对应的依赖项即可:

root@a11f632cd6b4:/# /opt/Qt5.12.6/Tools/QtCreator/bin/qtcreator

然后使用docker commit 提交镜像修改即可,或者在dockerfile中添加依赖,重新构建镜像。

优化

  • docker run启动用户使用当前宿主机登录的用户,而非root用户,避免code file权限问题。
  • 保持QtCreator配置的数据持久化
  • 封装了构建脚本和启动脚本

获取优化后的版本

参考

  1. Running QtCreator in Docker
  2. Docker中运行Qt应用程序
  3. Docker运行GUI原理
  4. Docker–从入门到实践
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值