<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Bluish Coder: rkt</title>
 <link href="http://bluishcoder.co.nz/tag/rkt/atom.xml" rel="self"/>
 <link href="http://bluishcoder.co.nz/"/>
 <updated>2020-07-10T16:25:05+12:00</updated>
 <id>http://bluishcoder.co.nz/</id>
 <author>
   <name>Bluishcoder</name>
   <email>admin@bluishcoder.co.nz</email>
 </author>

 
 <entry>
   <title>Running X11 apps in an rkt container</title>
   <link href="http://bluishcoder.co.nz/2017/01/18/running-x11-apps-in-an-rkt-container.html"/>
   <updated>2017-01-18T12:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2017/01/18/running-x11-apps-in-an-rkt-container</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;https://coreos.com/rkt/&quot;&gt;rkt&lt;/a&gt; is a container runtime I&#39;ve been using on a few projects recently. I was creating a container for &lt;a href=&quot;https://mozart.github.io/&quot;&gt;Mozart&lt;/a&gt; which uses emacs as an IDE. This requires running an X11 application within the container and have it displayed on the host display.&lt;/p&gt;

&lt;p&gt;To get this working I needed to mount my hosts X11 unix domain socket inside the container and provide an &lt;code&gt;Xauthority&lt;/code&gt; file that gave the container the rights to connect to the host X server.&lt;/p&gt;

&lt;p&gt;The following shell commands use &lt;a href=&quot;https://github.com/containers/build/&quot;&gt;acbuild&lt;/a&gt; to create a container that runs &lt;code&gt;xclock&lt;/code&gt; as an example of the process:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;acbuild begin docker://ubuntu:16.04
acbuild set-name bluishcoder.co.nz/xclock
acbuild run -- apt install --no-install-recommends --yes x11-apps
acbuild run -- rm -rf /var/lib/apt/lists/*
acbuild environment add DISPLAY unix$DISPLAY
acbuild environment add XAUTHORITY /root/.Xauthority
acbuild mount add x11socket /tmp/.X11-unix
acbuild mount add x11auth /root/.Xauthority
acbuild set-exec xclock
acbuild write --overwrite xclock.aci
acbuild end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It uses an Ubuntu Linux image from the Docker hub as a base and installs &lt;code&gt;x11-apps&lt;/code&gt;. To reduce disk space it removes cached package files afterwards. A &lt;code&gt;DISPLAY&lt;/code&gt; environment variable is set to point to use the same &lt;code&gt;DISPLAY&lt;/code&gt; as the host. The &lt;code&gt;XAUTHORITY&lt;/code&gt; enviroment variable is set to point to a file in the home directory of the &lt;code&gt;root&lt;/code&gt; user in the container.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;mount&lt;/code&gt; subcommands expose the &lt;code&gt;x11socket&lt;/code&gt; and &lt;code&gt;x11auth&lt;/code&gt; endpoints to point to where the X11 unix domain socket and the &lt;code&gt;Xauthority&lt;/code&gt; file are expected to be. These will be provided by the &lt;code&gt;rkt&lt;/code&gt; invocation to mount host resources in those locations.&lt;/p&gt;

&lt;p&gt;The final part of the script sets the executable to be &lt;code&gt;xclock&lt;/code&gt; and writes the &lt;code&gt;aci&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;On the host side we need to create an &lt;code&gt;Xauthority&lt;/code&gt; file that provides the container access to our X11 server. This file needs to be set so that any hostname can connect to the X11 server as the hostname for the container can change between invocations. To do this the authentication family in the file needs to be set to &lt;code&gt;FamilyWild&lt;/code&gt;. I got the steps to do this from &lt;a href=&quot;https://stackoverflow.com/questions/16296753/can-you-run-gui-apps-in-a-docker-container&quot;&gt;this stack overflow post&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;xauth nlist :0 | sed -e &#39;s/^..../ffff/&#39; | xauth -f myauthority nmerge -
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will retrieve the &lt;code&gt;Xauthority&lt;/code&gt; information for display &lt;code&gt;:0&lt;/code&gt; and modify the first four bytes to be &lt;code&gt;ffff&lt;/code&gt;. This sets the authority family to &lt;code&gt;FamilyWild&lt;/code&gt;. A new file called &lt;code&gt;myauthority&lt;/code&gt; is created with this data. This file will be mapped to the &lt;code&gt;x11auth&lt;/code&gt; mount point in the container.&lt;/p&gt;

&lt;p&gt;The container can be executed with &lt;code&gt;rkt&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rkt run --insecure-options=image xclock.aci \
        --volume x11socket,kind=host,source=/tmp/.X11-unix \
        --volume x11auth,kind=host,source=./myauthority
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;--volume&lt;/code&gt; command line arguments map the mount points we defined in the &lt;code&gt;acbuild&lt;/code&gt; commands to locations on the host. The running &lt;code&gt;xclock&lt;/code&gt; application should now appear on the host X11 display.&lt;/p&gt;
</content>
 </entry>
 
 
</feed>
