I'm trying to run an application from a Docker container which is supposed to open a GUI widow (a video stream in my case). The Docker container is run on a Raspberry Pi and I SSH into the Pi from my Mac and then I issue the Docker run command. I have one problem here:
When I run the whole thing as follows, it works flawlessly:
I run the command as:
docker run -it --net=host --device=/dev/vcsm --device=/dev/vchiq -e DISPLAY -v /tmp/.X11-unix joesan/motion_detector bash
From the bash, that opens up after issuing the Docker run command, I install xauth
root@cctv:/raspi_motion_detection/project# apt-get install xauth
I then add the Xauth cookie using Xauth add and then I then run my Python program which shows the GUI window with the video stream!
So far so good. But, every time I don't want to be doing these steps all over again. So I wrote a small script to do this as below:
HOST=cctv
DISPLAY_NUMBER=$(echo $DISPLAY | cut -d. -f1 | cut -d: -f2)
echo $DISPLAY_NUMBER
# Extract auth cookie
AUTH_COOKIE=$(xauth list | grep "^$(hostname)/unix:${DISPLAY_NUMBER} " | awk '{print $3}')
# Add the xauth cookie to xauth
xauth add ${HOST}/unix:${DISPLAY_NUMBER} MIT-MAGIC-COOKIE-1 ${AUTH_COOKIE}
# Launch the container  
docker run -it --net=host --device=/dev/vcsm --device=/dev/vchiq -e DISPLAY -v /tmp/.X11-unix  joesan/motion_detector`
But this time it fails with the error:
X11 connection rejected because of wrong authentication.
Unable to init server: Could not connect: Connection refused
I then tried to run the above script as a sudo user and I get the following:
xauth:  file /root/.Xauthority does not exist
xauth: (argv):1:  bad "add" command line
X11 connection rejected because of wrong authentication.
Unable to init server: Could not connect: Connection refused
Is there anything that I'm missing?

docker -it ..command, it launches the container, creates a bash shell inside the container, and places you there. I guess you execute theapt-get installandxauthcommands inside the container. In the shell script, you seem to be running the xauth commands on the host, not inside the container.