X11 forwarding doesn’t work when image runs in doc

2019-08-27 23:24发布

问题:

I have a docker container based on debian and it works when I run it like,

“docker run -ti --name devworkstation --rm --net=host -v /var/run/docker.sock:/var/run/docker.sock -e DISPLAY=$env:DISPLAY xxxx/xxxx:devworkstation”

By works, I mean that the bash command prompt comes up and when I run xeyes, I see the window on my windows 10 computer as I expect. (x11 forwarding works.)

($env:DISPLAY = “myWinIP:0.0”)

I then created a new external virtual switch named, “dockerExternalSwitch,” and a docker machine with, “docker-machine create -d hyperv --hyperv-virtual-switch dockerExternalSwitch dev1”

I set up my environment according to docker-machine env dev1. I then run the same docker run command as above on the docker machine. It brings up my command prompt as I expect, but when I run xeyes, I get an error:

Error: Can’t open display: xxx.xxx.xxx.xxx:0.0

I also have firefox installed on the debian image. It gives a similar error: Unable to init server: Broadway display type not supported: xxx.xx.xx.xxx:0.0 Error: cannot open display: xxx.xxx.xxx.xxx:0.0

Since it works when I run it directly (w/o a docker machine) and fails when I use the docker machine, I’m wondering if it is network related.

Dockerfile contents:

from debian:9.5
RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y firefox-esr
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y vim git curl
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y gdb valgrind
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y qt4-default
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y dos2unix tcpdump netcat
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y apt-utils
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y linux-image-rt-amd64
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y task-mate-desktop
RUN env
CMD /bin/bash

I’ve tried about all of the x11 forwarding suggestions I could find on the internet. And I've searched stack overflow for suggestions. I haven't found a solution and it appears to be related to the docker machine. Any suggestions on what I could try would be greatly appreciated.

(FYI, The reason I'm working with the docker-machine is that I have sample code that connects to a network camera that works as a docker-machine but not when I run without the docker machine. )

回答1:

I figured it out. When running the image w/o the docker machine, the DISPLAY variable had to be the ip address of my windows computer. When running the image with the docker machine, I had to change the DISPLAY environment variable to be the IP address of the new virtual switch I had created. In my script file I used the following code:

#$local is true when running w/o the docker machine
if ($local) {
     $IPv4=get-netipaddress -InterfaceAlias "vEthernet (Default Switch)" -AddressFamily "IPv4" | select -expand IPAddress
 }
 else {
     $IPv4=get-netipaddress -InterfaceAlias "vEthernet (dockerExternalSwitch)" -AddressFamily "IPv4" | select -expand          IPAddress
 }
debug-output "IPv4: $($IPv4)"
 $env:DISPLAY=$IPv4 + ":0.0"

if (! $local) {
  #set up to use docker-machine using docker-machine env dev1
}

docker run -ti --name devworkstation --rm --net=host -v /var/run/docker.sock:/var/run/docker.sock -e DISPLAY=$env:DISPLAY    xxx/xxx:devworkstation

Now the x11 forwarding works with and w/o the docker-machine.