# Share X server across multiple containers

**URL:** <https://forums.balena.io/t/share-x-server-across-multiple-containers/28142>\
**Category:** balenaOS\
**Created:** [August 28, 2019, 3:37am UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142 "2019-08-28T03:37:17Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jassky](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jassky/32/2432_2.png) [@Jassky](https://forums.balena.io/u/Jassky)\
**Post date:** [August 28, 2019, 3:37am UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/1 "2019-08-28T03:37:17Z")

</div>

I have a MultiContainer application where both containers need to display windows.

Container 1 `[electron-app]` starts with `startx /usr/app/reflx-os :0`  
Container 2 `[python-server]` starts with `python3 /usr/app/server.py` and needs to display an OpenCV window.

`[electron-app]` displays properly. However, when `[python-server]` tries to open the OpenCV window it throws this error:  
`Gtk: cannot open display: 0`

The issue is that the X server is running on the `[electron-app]`. So how can I get my `[python-server]` to use the X server from the other container?

It seems like sharing the hosts X server via unix socket is simple:

- [https://www.jujens.eu/posts/en/2017/Feb/15/docker-unix-socket](https://www.jujens.eu/posts/en/2017/Feb/15/docker-unix-socket)
- [http://somatorio.org/en/post/running-gui-apps-with-docker](http://somatorio.org/en/post/running-gui-apps-with-docker)
- [http://wiki.ros.org/docker/Tutorials/GUI](http://wiki.ros.org/docker/Tutorials/GUI)
- [https://github.com/jessfraz/dockerfiles/issues/169](https://github.com/jessfraz/dockerfiles/issues/169)

However, in this case, the host machine isn’t hosting the X server. The `[electron-app]` container is.

---

<div class="post-metadata">

**Author:** ![Jassky](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jassky/32/2432_2.png) [@Jassky](https://forums.balena.io/u/Jassky)\
**Post date:** [August 28, 2019, 4:51am UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/2 "2019-08-28T04:51:48Z")

</div>

Follow-up:

I attempted some binding workarounds without success.

Attempt 1: I tried creating a shared named volume `x-unix:/tmp/.X11-unix`

Attempt 2: in my `[electron-app]` Dockerfile I ran `mount --bind /tmp/.X11-unix /data/.X11-unix` and in my `[python-server]` Dockerfile I ran `mount --bind /data/.X11-unix /tmp/.X11-unix`

In both cases, the `X0` socket DOES show up in the `[python-server]` in `/tmp/.X11-unix/X0` but does not seem to function as expected. I tried setting `DISPLAY` to everything from :0 to :0.0 to unix:0 to localhost:0 to 127.0.0.1:0, etc.

Perhaps a permissions issue?

---

<div class="post-metadata">

**Author:** ![thundron](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/thundron/32/1379_2.png) [@thundron](https://forums.balena.io/u/thundron)\
**Post date:** [August 28, 2019, 7:40am UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/4 "2019-08-28T07:40:01Z")

</div>

Hi, it could be a permission issue indeed if the container can see the resource but can’t make use of it.  
What do you mean with “does not seem to function as expected”? Do you see any error(s) in the logs? What is the expected behaviour and how does it behave instead?  
Is it possible to have a look at the `Dockerfile.template`s and `docker-compose.yml` of your application?

---

<div class="post-metadata">

**Author:** ![Jassky](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jassky/32/2432_2.png) [@Jassky](https://forums.balena.io/u/Jassky)\
**Post date:** [August 28, 2019, 8:22am UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/6 "2019-08-28T08:22:58Z")

</div>

@thundron [https://github.com/JeffJassky/balena-share-x-across-containers](https://github.com/JeffJassky/balena-share-x-across-containers)

What I meant by “does not seem to function as expected” I mean it I get the same `Gtk cannot open display:` error no matter what.

The only X related log file I can find on `[electron-app]` is `/var/log/Xorg.2.log` which unfortunately has nothing even relevant to displays or display errors in it. I cannot find any X related log files on the `[python-server]`

Another possibly relevant reference: [https://www.jujens.eu/posts/en/2017/Feb/15/docker-unix-socket/](https://www.jujens.eu/posts/en/2017/Feb/15/docker-unix-socket/)

---

<div class="post-metadata">

**Author:** ![thundron](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/thundron/32/1379_2.png) [@thundron](https://forums.balena.io/u/thundron)\
**Post date:** [August 28, 2019, 9:15am UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/7 "2019-08-28T09:15:31Z")

</div>

Thanks for the resources as well, we’ll check it out and reach back to you.  
In the meantime you could have a look at our [x11-window-manager project](https://github.com/balena-io-playground/x11-window-manager) to check if there’s something different you can fiddle with

---

<div class="post-metadata">

**Author:** ![thundron](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/thundron/32/1379_2.png) [@thundron](https://forums.balena.io/u/thundron)\
**Post date:** [August 28, 2019, 9:27am UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/9 "2019-08-28T09:27:46Z")

</div>

It seems the containers configs are mostly correct, but you’ll need to also add `UDEV=1` (I _think_ to both containers, but might be wrong) to be able to use the unix socket.  
Tell us how it goes after adding it!

---

<div class="post-metadata">

**Author:** ![Jassky](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jassky/32/2432_2.png) [@Jassky](https://forums.balena.io/u/Jassky)\
**Post date:** [August 28, 2019, 9:44am UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/15 "2019-08-28T09:44:06Z")

</div>

> [@thundron](#):
>
> UDEV=1

Tried this. Still the same issue: `Gtk-WARNING **: cannot open display: localhost:0`.

Any other thoughts?

---

<div class="post-metadata">

**Author:** ![Jassky](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jassky/32/2432_2.png) [@Jassky](https://forums.balena.io/u/Jassky)\
**Post date:** [August 28, 2019, 10:25am UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/19 "2019-08-28T10:25:56Z")

</div>

Seperate note: Binding the `x-unix:/tmp/.X11-unix:rw` volume persists the socket files (X0, X1, etc) across pushes of new builds. Each time the container starts X adds/increments a new display. Eventually `/tmp/.X11-unix` is filled with X0, X1, X2, X3… etc.

It’s not really a problem - just an annoyance. I added `rm -r /tmp/.X11-unix` to my start.sh file to manually remove defunct sockets.

---

<div class="post-metadata">

**Author:** ![lucianbuzzo](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/lucianbuzzo/32/615_2.png) [@lucianbuzzo](https://forums.balena.io/u/lucianbuzzo)\
**Post date:** [August 28, 2019, 10:28am UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/20 "2019-08-28T10:28:08Z")

</div>

Hi, I’ve looked through this thread and its not entirely clear to me what the intended result is, do you want your application to display the output of the `python-server` and `electron-app` side by side? Or are they displayed on different monitors?

---

<div class="post-metadata">

**Author:** ![Jassky](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jassky/32/2432_2.png) [@Jassky](https://forums.balena.io/u/Jassky)\
**Post date:** [August 28, 2019, 10:46am UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/21 "2019-08-28T10:46:53Z")

</div>

The intent is separate (but overlaying) windows on a single display. The electron-app is a web interface for controlling camera settings. The python-server is an OpenCV window that displays the live camera preview. The intent is for the camera preview to be laying over the center of the web interface.

If I run the python server in the background of the `[electron-app]` the effect works as intended - but I wanted to containerize the `[python-server]` for the benefits of supervising, improved logging and code separation.

Also updated [https://github.com/JeffJassky/balena-share-x-across-containers](https://github.com/JeffJassky/balena-share-x-across-containers) with latest configs.

---

<div class="post-metadata">

**Author:** ![Jassky](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jassky/32/2432_2.png) [@Jassky](https://forums.balena.io/u/Jassky)\
**Post date:** [August 28, 2019, 11:42am UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/22 "2019-08-28T11:42:49Z")

</div>

Update: disabling access control on the main X process using the -ac flag allows the window to show up properly.

From the X manpage:

> -ac disables host-based access control mechanisms. Enables access by any host, and permits any host to modify the access control list. Use with extreme caution. This option exists primarily for running test suites remotely.

Unfortunately it doesn’t look like THE solution. Though at least we’ve narrowed it down to an access/permissions issue.

 ![IMG_1164](https://global.discourse-cdn.com/balena/original/2X/6/6a460480bd2abad5cfa607d5656e518205c71887.jpeg)

---

<div class="post-metadata">

**Author:** ![wrboyce](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/wrboyce/32/374_2.png) [@wrboyce](https://forums.balena.io/u/wrboyce)\
**Post date:** [August 28, 2019, 12:36pm UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/23 "2019-08-28T12:36:12Z")

</div>

Hi jassky, if it is working with ‘-ac’ but not without then it seems like a permissions issue. You can use the `xhost` command to inspect/update the access controls. I think you would want to do something like `xhost +python-server` from the electron-app container, but you might have to specific the python-server IP.

---

<div class="post-metadata">

**Author:** ![robertgzr](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/robertgzr/32/1387_2.png) [@robertgzr](https://forums.balena.io/u/robertgzr)\
**Post date:** [August 28, 2019, 12:44pm UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/24 "2019-08-28T12:44:35Z")

</div>

As another idea here… How about exposing the opencv output as a stream endpoint to the electron container and having your frontend display that however it likes? That would alleviate the need to expose the X server and cut this dependency between your containers.

---

<div class="post-metadata">

**Author:** ![Jassky](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jassky/32/2432_2.png) [@Jassky](https://forums.balena.io/u/Jassky)\
**Post date:** [August 28, 2019, 1:04pm UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/26 "2019-08-28T13:04:39Z")

</div>

> @wrboyce I think you would want to do something like `xhost +python-server` from the electron-app container, but you might have to specific the python-server IP.

Wouldn’t the IP address be the same for all containers?

> @robertgzr How about exposing the opencv output as a stream endpoint to the electron container and having your frontend display that however it likes?

I like the level of familiarity I would have with presenting a video stream in a web view - however, frame rate and latency performance and image quality are pretty critical. I assume streaming frames from OpenCV right into X would be faster and look better than streaming over a TCP socket or something. Though I haven’t tested thoroughly. If you happen to know of any good solutions for that I’m all ears.

---

<div class="post-metadata">

**Author:** ![wrboyce](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/wrboyce/32/374_2.png) [@wrboyce](https://forums.balena.io/u/wrboyce)\
**Post date:** [August 28, 2019, 1:09pm UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/27 "2019-08-28T13:09:02Z")

</div>

> Wouldn’t the IP address be the same for all containers?

Docker has an internal network it uses, on which each container has its own (unique) IP address. In reality, you would probably be okay to whitelist 172.16.0.0/20.

---

<div class="post-metadata">

**Author:** ![robertgzr](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/robertgzr/32/1387_2.png) [@robertgzr](https://forums.balena.io/u/robertgzr)\
**Post date:** [August 28, 2019, 1:16pm UTC](https://forums.balena.io/t/share-x-server-across-multiple-containers/28142/28 "2019-08-28T13:16:31Z")

</div>

> I like the level of familiarity I would have with presenting a video stream in a web view - however, frame rate and latency performance and image quality are pretty critical. I assume streaming frames from OpenCV right into X would be faster and look better than streaming over a TCP socket or something. Though I haven’t tested thoroughly.
> 
> If you happen to know of any good solutions for that I’m all ears.

I’m not really an expert on this… but looking around a bit looks like this is not uncharted territory and opencv \> gstreamer would be the way to go:

> **[Raspberry Pi Camera openCV rendering with low latency streaming with...](http://hopkinsdev.blogspot.com/2016/06/raspberry-pi-camera-opencv-rendering.html)**
>
> A blog about electronics and software developments.
