Can you run GUI applications in a Docker container?

前端 未结 22 2481
南旧
南旧 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
    

提交回复
热议问题