在Docker中使用Ubuntu的桌面环境

今天

    Dockerを導入してGUI操作可能なLinux(Ubuntu)コンテナを作成する

我会参考这个,在我自己的环境中进行复现并写报告。由于我主要只在Ubuntu终端上使用,所以没有特别考虑应用方面的问题。

环境

宿主机:macOS Catalina
Docker:Docker版本19.03.5,构建633a0ea

Docker文件

FROM ubuntu:16.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
    ubuntu-desktop \
    samba-common-bin \
    x11-apps \
    sudo

ARG uid=1000
ARG gid=1000
RUN groupadd -g ${uid} developer && \
    useradd -u ${gid} -g developer -G sudo -r developer && \
    mkdir -p /home/developer/.config/nautilus &&\
    chown ${uid}:${gid} -R /home/developer && \
    mkdir -p /var/lib/samba/usershares

RUN echo 'Defaults visiblepw'             >> /etc/sudoers
RUN echo 'developer ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# set locale to initialize terminal property
RUN locale-gen en_US.UTF-8  
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8

USER developer
WORKDIR /home/developer

使用方法

我会编写一个类似下面的Shell脚本。

docker build -t udesk .
xhost + 127.0.0.1
HOSTDISPLAY=host.docker.internal:0
docker run --rm -it\
       -e DISPLAY=$HOSTDISPLAY \
       -v /tmp/.X11-unix/:/tmp/.X11-unix \
       --name udesk \
       udesk /bin/bash -c "nautilus"
xhost - 127.0.0.1

你只需要运行bash init.sh,就可以从构建镜像到启动容器的整个过程。你可以打开另一个终端,并使用docker stop udesk命令来停止容器。由于加上了–rm选项,所以容器在结束后会被删除。因此,如果你想继续使用容器,请删除–rm并重新启动容器。

    • 上の init.sh で書いている xhost +/ xhost – のテクニックはDocker のコンテナで matplotlib の描画結果を表示させたいな場合にも応用が効きますね.

 

    Linux がホストの場合は xhost + local:docker/ xhost – local:docker とか HOSTDISPLAY=$DISPLAY としておくとうまくいくはずです.

参考文献

    • Dockerを導入してGUI操作可能なLinux(Ubuntu)コンテナを作成する

 

    • https://marsee101.blog.fc2.com/blog-entry-4438.html

 

    • https://askubuntu.com/questions/798928/nautilus-share-message-called-net-usershare-info-but-it-failed

 

    • Docker: コンテナのlocaleを設定したい

 

    https://medium.com/@mreichelt/how-to-show-x11-windows-within-docker-on-mac-50759f4b6

对于那些想尝试使用XForwarding的人,可供参考的文献。

    • https://unix.stackexchange.com/questions/12755/how-to-forward-x-over-ssh-to-run-graphics-applications-remotely

 

    • https://blog.amedama.jp/entry/docker-container-x11-forwarding

 

    • http://fabiorehm.com/blog/2014/09/11/running-gui-apps-with-docker/

 

    • https://unix.stackexchange.com/questions/108679/x-client-forwarded-over-ssh-cannot-open-display-localhost11-0

 

    sshのX11Forwadingがうまく動作しない