Docker 运行 PyGtk

Author Avatar
Mutse Young 10月 05, 2020

安装

作者使用的是 Ubuntu 16.04.7,安装 Docker 命令如下:

::bash
$ sudo apt-get install docker.io

设置

安装完毕,默认使用 root 用户启动,使用以下命令修改为一般用户权限启动:

::bash
$ sudo groupadd docker
$ sudo usermod -aG docker $USER

为方便快速下载镜像,需要设置代理加速:

::bash
$ sudo vim /etc/docker/daemon.json
{
    "registry-mirrors": [
        "https://docker.mirrors.ustc.edu.cn",
        "https://registry.docker-cn.com"
    ]
}

保存后,重启 docker 服务,操作如下:

::bash
$ sudo service docker restart

编写 Dockerfile

::text
FROM i386/ubuntu
MAINTAINER Mutse Young "yyhoo2.young@gmail.com"
RUN rm /etc/apt/sources.list
COPY sources.list /etc/apt/sources.list
ADD hello.py /
RUN adduser --quiet --disabled-password young

RUN apt-get update
RUN apt-get install -y python python-gtk2
CMD ["python", "./hello.py"]

使用以下命令创建 docker 镜像:

::bash
$ docker build -t docker-pygtk .

Docker 镜像启动 GUI

为方便启动 Docker 镜像中 App,编写脚本 run.sh,内容如下:

::bash
#!/bin/bash
xhost +
docker run -d \
-v /etc/localtime:/etc/localtime:ro \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e DISPLAY=unix$DISPLAY \
-e GDK_SCALE \
-e GDK_DPI_SCALE \
python-hello

运行 run.sh 脚本,即可见到 App。

参考资料

Docker容器图形界面显示的配置方法