Can you run GUI applications in a Docker container?

前端 未结 22 2509
南旧
南旧 2020-11-22 06:12

How can you run GUI applications in a Docker container?

Are there any images that set up vncserver or something so that you can - for example - add an e

22条回答
  •  失恋的感觉
    2020-11-22 06:32

    Based on Jürgen Weigert's answer, I have some improvement:

    docker build -t xeyes - << __EOF__
    FROM debian
    RUN apt-get update
    RUN apt-get install -qqy x11-apps
    ENV DISPLAY :0
    CMD xeyes
    __EOF__
    XSOCK=/tmp/.X11-unix
    XAUTH_DIR=/tmp/.docker.xauth
    XAUTH=$XAUTH_DIR/.xauth
    mkdir -p $XAUTH_DIR && touch $XAUTH
    xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -
    docker run -ti -v $XSOCK:$XSOCK -v $XAUTH_DIR:$XAUTH_DIR -e XAUTHORITY=$XAUTH xeyes
    

    The only difference is that it creates a directory $XAUTH_DIR which is used to place $XAUTH file and mount $XAUTH_DIR directory instead of $XAUTH file into docker container.

    The benefit of this method is that you can write a command in /etc/rc.local which is to create a empty folder named $XAUTH_DIR in /tmp and change its mode to 777.

    tr '\n' '\000' < /etc/rc.local | sudo tee /etc/rc.local >/dev/null
    sudo sed -i 's|\x00XAUTH_DIR=.*\x00\x00|\x00|' /etc/rc.local >/dev/null
    tr '\000' '\n' < /etc/rc.local | sudo tee /etc/rc.local >/dev/null
    sudo sed -i 's|^exit 0.*$|XAUTH_DIR=/tmp/.docker.xauth; rm -rf $XAUTH_DIR; install -m 777 -d $XAUTH_DIR\n\nexit 0|' /etc/rc.local
    

    When system restart, before user login, docker will mount the $XAUTH_DIR directory automatically if container's restart policy is "always". After user login, you can write a command in ~/.profile which is to create $XAUTH file, then the container will automatically use this $XAUTH file.

    tr '\n' '\000' < ~/.profile | sudo tee ~/.profile >/dev/null
    sed -i 's|\x00XAUTH_DIR=.*-\x00|\x00|' ~/.profile
    tr '\000' '\n' < ~/.profile | sudo tee ~/.profile >/dev/null
    echo "XAUTH_DIR=/tmp/.docker.xauth; XAUTH=\$XAUTH_DIR/.xauth; touch \$XAUTH; xauth nlist \$DISPLAY | sed -e 's/^..../ffff/' | xauth -f \$XAUTH nmerge -" >> ~/.profile
    

    Afterall, the container will automatically get the Xauthority file every time the system restart and user login.

提交回复
热议问题