Can you run GUI applications in a Docker container?

前端 未结 22 2474
南旧
南旧 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:31

    OSX

    Jürgen Weigert has the best answer that worked for me on Ubuntu, however on OSX, docker runs inside of VirtualBox and so the solution doesn't work without some more work.

    I've got it working with these additional ingredients:

    1. Xquartz (OSX no longer ships with X11 server)
    2. socket forwarding with socat (brew install socat)
    3. bash script to launch the container

    I'd appreciate user comments to improve this answer for OSX, I'm not sure if socket forwarding for X is secure, but my intended use is for running the docker container locally only.

    Also, the script is a bit fragile in that it's not easy to get the IP address of the machine since it's on our local wireless so it's always some random IP.

    The BASH script I use to launch the container:

    #!/usr/bin/env bash
    
    CONTAINER=py3:2016-03-23-rc3
    COMMAND=/bin/bash
    NIC=en0
    
    # Grab the ip address of this box
    IPADDR=$(ifconfig $NIC | grep "inet " | awk '{print $2}')
    
    DISP_NUM=$(jot -r 1 100 200)  # random display number between 100 and 200
    
    PORT_NUM=$((6000 + DISP_NUM)) # so multiple instances of the container won't interfer with eachother
    
    socat TCP-LISTEN:${PORT_NUM},reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\" 2>&1 > /dev/null &
    
    XSOCK=/tmp/.X11-unix
    XAUTH=/tmp/.docker.xauth.$USER.$$
    touch $XAUTH
    xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -
    
    docker run \
        -it \
        --rm \
        --user=$USER \
        --workdir="/Users/$USER" \
        -v "/Users/$USER:/home/$USER:rw" \
        -v $XSOCK:$XSOCK:rw \
        -v $XAUTH:$XAUTH:rw \
        -e DISPLAY=$IPADDR:$DISP_NUM \
        -e XAUTHORITY=$XAUTH \
        $CONTAINER \
        $COMMAND
    
    rm -f $XAUTH
    kill %1       # kill the socat job launched above
    

    I'm able to get xeyes and matplotlib working with this approach.

    Windows 7+

    It's a bit easier on Windows 7+ with MobaXterm:

    1. Install MobaXterm for windows
    2. Start MobaXterm
    3. Configure X server: Settings -> X11 (tab) -> set X11 Remote Access to full
    4. Use this BASH script to launch the container

    run_docker.bash:

    #!/usr/bin/env bash
    
    CONTAINER=py3:2016-03-23-rc3
    COMMAND=/bin/bash
    DISPLAY="$(hostname):0"
    USER=$(whoami)
    
    docker run \
        -it \
        --rm \
        --user=$USER \
        --workdir="/home/$USER" \
        -v "/c/Users/$USER:/home/$USER:rw" \
        -e DISPLAY \
        $CONTAINER \
        $COMMAND
    

    0 讨论(0)
  • 2020-11-22 06:31

    There is another solution by lord.garbage to run GUI apps in a container without using VNC, SSH and X11 forwarding. It is mentioned here too.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 06:32

    OSX (10.13.6, high sierra)

    Similar to @Nick's answer, but his solution did not work for me.

    First install socat by doing brew install socat, and install XQuartz (https://www.xquartz.org/)

    Then followed these steps here (http://fabiorehm.com/blog/2014/09/11/running-gui-apps-with-docker/) in the comments section:

    1. in one mac terminal i started:
    
    socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"
    
    2. and in another mac terminal I ran:
    
    docker run -ti --rm \
    -e DISPLAY=$(ipconfig getifaddr en0):0 \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    firefox
    

    I was also able to launch CLion from my debian docker container too.

    0 讨论(0)
  • 2020-11-22 06:36

    You can also use subuser: https://github.com/timthelion/subuser

    This allows you to package many gui apps in docker. Firefox and emacs have been tested so far. With firefox, webGL doesn't work though. Chromium doesn't work at all.

    EDIT: Sound works!

    EDIT2: In the time since I first posted this, subuser has progressed greatly. I now have a website up subuser.org, and a new security model for connecting to X11 via XPRA bridging.

    0 讨论(0)
  • 2020-11-22 06:36

    For OpenGL rendering with the Nvidia driver, use the following image:

    https://github.com/thewtex/docker-opengl-nvidia

    For other OpenGL implementations, make sure the image has the same implementation as the host.

    0 讨论(0)
提交回复
热议问题