title: How to dockerize an app together with VNC (arduino IDE)
body: <p>I'm trying to dockerize an arduino IDE on raspberry pi using this answer: <a href="https://stackoverflow.com/a/16311264/6791424">https://stackoverflow.com/a/16311264/6791424</a></p>
  
  <p><em>ps: I know how to pass the X server to the docker container but in this case I really need VNC</em></p>
  
  <p>Here's my Dockerfile:</p>
  
  <pre><code>FROM resin/rpi-raspbian:latest
  
  RUN apt-get update &amp;&amp; apt-get install -y arduino x11vnc xvfb
  
  RUN mkdir ~/.vnc &amp;&amp; x11vnc -storepasswd 1234 ~/.vnc/passwd
  
  CMD x11vnc -forever -usepw -create &amp; /usr/bin/arduino
  </code></pre>
  
  <p>Here's the output of <code>docker run -p 5900 arduino_docker</code>:</p>
  
  <pre><code>05/04/2018 08:22:10 -usepw: found /root/.vnc/passwd
  05/04/2018 08:22:10 x11vnc version: 0.9.13 lastmod: 2011-08-10  pid: 11
  05/04/2018 08:22:10 
  05/04/2018 08:22:10 wait_for_client: WAIT:cmd=FINDCREATEDISPLAY-Xvfb
  05/04/2018 08:22:10 
  05/04/2018 08:22:10 initialize_screen: fb_depth/fb_bpp/fb_Bpl 24/32/2560
  05/04/2018 08:22:10 
  05/04/2018 08:22:10 Autoprobing TCP port 
  05/04/2018 08:22:10 Autoprobing selected TCP port 5900
  05/04/2018 08:22:10 Autoprobing TCP6 port 
  05/04/2018 08:22:10 Autoprobing selected TCP6 port 5900
  05/04/2018 08:22:10 listen6: bind: Address already in use
  05/04/2018 08:22:10 Not listening on IPv6 interface.
  05/04/2018 08:22:10 
  
  The VNC desktop is:      1843fd6c8bdd:0
  PORT=5900
  Exception in thread "main" java.lang.ExceptionInInitializerError
      at processing.app.Preferences.save(Preferences.java:735)
      at processing.app.Preferences.init(Preferences.java:249)
      at processing.app.Base.main(Base.java:117)
  Caused by: java.awt.HeadlessException: 
  No X11 DISPLAY variable was set, but this program performed an operation which requires it.
      at sun.awt.HeadlessToolkit.getMenuShortcutKeyMask(HeadlessToolkit.java:234)
      at processing.core.PApplet.&lt;clinit&gt;(Unknown Source)
      ... 3 more
  </code></pre>
  
  <p>As you can see, the vnc server is working. However, the arduino IDE tries to access the DISPLAY env variable but can't find it. I even tried adding <code>ENV DISPLAY 0</code> in the Dockerfile but then it complains of not being able to access that display.</p>
  
  <p><strong>UPDATE</strong>: by doing some research on xvfb: <a href="https://en.wikipedia.org/wiki/Xvfb#Remote_control_over_SSH" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Xvfb#Remote_control_over_SSH</a></p>
  
  <p>I changed the dockerfile to:</p>
  
  <pre><code>FROM resin/rpi-raspbian:latest
  
  RUN apt-get update &amp;&amp; apt-get install -y arduino x11vnc xvfb
  
  RUN mkdir ~/.vnc &amp;&amp; x11vnc -storepasswd 1234 ~/.vnc/passwd
  
  ENV DISPLAY :1
  
  CMD /usr/bin/Xvfb :1 -screen 0 1366x768x16 &amp; x11vnc -display :1 -forever -usepw -create &amp; /usr/bin/arduino
  </code></pre>
  
  <p>Now it connects but the screen is black.</p>
  
  <p>Here's the output:</p>
  
  <pre><code>05/04/2018 11:30:21 -usepw: found /root/.vnc/passwd
  05/04/2018 11:30:21 x11vnc version: 0.9.13 lastmod: 2011-08-10  pid: 12
  05/04/2018 11:30:21 
  05/04/2018 11:30:21 wait_for_client: WAIT:cmd=FINDCREATEDISPLAY-Xvfb
  05/04/2018 11:30:21 
  05/04/2018 11:30:21 initialize_screen: fb_depth/fb_bpp/fb_Bpl 24/32/2560
  05/04/2018 11:30:21 
  05/04/2018 11:30:21 Autoprobing TCP port 
  05/04/2018 11:30:21 Autoprobing selected TCP port 5900
  05/04/2018 11:30:21 Autoprobing TCP6 port 
  05/04/2018 11:30:21 Autoprobing selected TCP6 port 5900
  05/04/2018 11:30:21 listen6: bind: Address already in use
  05/04/2018 11:30:21 Not listening on IPv6 interface.
  05/04/2018 11:30:21 
  
  The VNC desktop is:      f6627eb9da23:0
  PORT=5900
  05/04/2018 11:30:34 Got connection from client 172.17.0.1
  05/04/2018 11:30:34   other clients:
  05/04/2018 11:30:34 Normal socket connection
  05/04/2018 11:30:34 incr accepted_client=1 for 172.17.0.1:58432  sock=6
  05/04/2018 11:30:34 wait_for_client: got client
  05/04/2018 11:30:34 Client Protocol Version 3.8
  05/04/2018 11:30:34 Protocol version sent 3.8, using 3.8
  05/04/2018 11:30:34 client progressed=1 in 3/4 0.033597 s
  05/04/2018 11:30:34 client useCopyRect: 172.17.0.1 0
  05/04/2018 11:30:34 client_set_net: 172.17.0.1  0.1224
  05/04/2018 11:30:34 wait_for_client: running: env X11VNC_SKIP_DISPLAY=''  /bin/sh /tmp/x11vnc-find_display.QuzQwT
  /tmp/x11vnc-find_display.QuzQwT: 1: /tmp/x11vnc-find_display.QuzQwT: netstat: not found
  /tmp/x11vnc-find_display.QuzQwT: 1: /tmp/x11vnc-find_display.QuzQwT: xauth: not found
  05/04/2018 11:30:35 wait_for_client: find display cmd failed.
  05/04/2018 11:30:35 wait_for_client: FINDCREATEDISPLAY cmd: /bin/sh /tmp/x11vnc-find_display.QuzQwT Xvfb
  trying N=20 ...
  redir_daemon=
  /tmp/x11vnc-find_display.QuzQwT: 461: /tmp/x11vnc-find_display.QuzQwT: /tmp/x11vnc-find_display.QuzQwT: 461: /tmp/x11vnc-find_display.QuzQwT: nmerge: not found
  -f: not found
  /tmp/x11vnc-find_display.QuzQwT: 462: /tmp/x11vnc-find_display.QuzQwT: nmerge: not found
  /tmp/x11vnc-find_display.QuzQwT: 462: /tmp/x11vnc-find_display.QuzQwT: -f: not found
  /usr/bin/Xvfb :20 -screen 0 1280x1024x24 -cc 4 -nolisten tcp -auth  
  use: X [:&lt;display&gt;] [option]
  -a #                   default pointer acceleration (factor)
  -ac                    disable access control restrictions
  -audit int             set audit trail level
  -auth file             select authorization file
  -br                    create root window with black background
  +bs                    enable any backing store support
  -bs                    disable any backing store support
  -c                     turns off key-click
  c #                    key-click volume (0-100)
  -cc int                default color visual class
  -nocursor              disable the cursor
  -core                  generate core dump on fatal error
  -displayfd fd          file descriptor to write display number to when ready to connect
  -dpi int               screen resolution in dots per inch
  -dpms                  disables VESA DPMS monitor control
  -deferglyphs [none|all|16] defer loading of [no|all|16-bit] glyphs
  -f #                   bell base (0-100)
  -fc string             cursor font
  -fn string             default font name
  -fp string             default font path
  -help                  prints message with these options
  +iglx                  Allow creating indirect GLX contexts
  -iglx                  Prohibit creating indirect GLX contexts (default)
  -I                     ignore all remaining arguments
  -ld int                limit data space to N Kb
  -lf int                limit number of open files to N
  -ls int                limit stack space to N Kb
  -nolock                disable the locking mechanism
  -maxclients n          set maximum number of clients (power of two)
  -nolisten string       don't listen on protocol
  -listen string         listen on protocol
  -noreset               don't reset after last client exists
  -background [none]     create root window with no background
  -reset                 reset after last client exists
  -p #                   screen-saver pattern duration (minutes)
  -pn                    accept failure to listen on all ports
  -nopn                  reject failure to listen on all ports
  -r                     turns off auto-repeat
  r                      turns on auto-repeat 
  -render [default|mono|gray|color] set render color alloc policy
  -retro                 start with classic stipple and cursor
  -s #                   screen-saver timeout (minutes)
  -seat string           seat to run on
  -t #                   default pointer threshold (pixels/t)
  -terminate             terminate at server reset
  -to #                  connection time out
  -tst                   disable testing extensions
  ttyxx                  server started from init on /dev/ttyxx
  v                      video blanking for screen-saver
  -v                     screen-saver without video blanking
  -wm                    WhenMapped default backing-store
  -wr                    create root window with white background
  -maxbigreqsize         set maximal bigrequest size 
  +xinerama              Enable XINERAMA extension
  -xinerama              Disable XINERAMA extension
  -dumbSched             Disable smart scheduling, enable old behavior
  -schedInterval int     Set scheduler interval in msec
  -sigstop               Enable SIGSTOP based startup
  +extension name        Enable extension
  -extension name        Disable extension
  -query host-name       contact named host for XDMCP
  -broadcast             broadcast for XDMCP
  -multicast [addr [hops]] IPv6 multicast for XDMCP
  -indirect host-name    contact named host for indirect XDMCP
  -port port-num         UDP port number to send messages to
  -from local-address    specify the local address to connect from
  -once                  Terminate server after one session
  -class display-class   specify display class to send in manage
  -cookie xdm-auth-bits  specify the magic cookie for XDMCP
  -displayID display-id  manufacturer display ID for request
  [+-]accessx [ timeout [ timeout_mask [ feedback [ options_mask] ] ] ]
                         enable/disable accessx key sequences
  -ardelay               set XKB autorepeat delay
  -arinterval            set XKB autorepeat interval
  -screen scrn WxHxD     set screen's width, height, depth
  -pixdepths list-of-int support given pixmap depths
  +/-render          turn on/off RENDER extension support(default on)
  -linebias n            adjust thin line pixelization
  -blackpixel n          pixel value for black
  -whitepixel n          pixel value for white
  -fbdir directory       put framebuffers in mmap'ed files in directory
  -shmem                 put framebuffers in shared memory
  /usr/bin/nohup: failed to run command ‘.xinitrc’: No such file or directory
  /tmp/x11vnc-find_display.QuzQwT: 981: /tmp/x11vnc-find_display.QuzQwT: -f: not found
  05/04/2018 11:30:43 Using X display :20
  05/04/2018 11:30:43 rootwin: 0x43 reswin: 0x200001 dpy: 0x109a8b8
  05/04/2018 11:30:43 
  05/04/2018 11:30:43 ------------------ USEFUL INFORMATION ------------------
  05/04/2018 11:30:43 X DAMAGE available on display, using it for polling hints.
  05/04/2018 11:30:43   To disable this behavior use: '-noxdamage'
  05/04/2018 11:30:43 
  05/04/2018 11:30:43   Most compositing window managers like 'compiz' or 'beryl'
  05/04/2018 11:30:43   cause X DAMAGE to fail, and so you may not see any screen
  05/04/2018 11:30:43   updates via VNC.  Either disable 'compiz' (recommended) or
  05/04/2018 11:30:43   supply the x11vnc '-noxdamage' command line option.
  05/04/2018 11:30:43 
  05/04/2018 11:30:43 Wireframing: -wireframe mode is in effect for window moves.
  05/04/2018 11:30:43   If this yields undesired behavior (poor response, painting
  05/04/2018 11:30:43   errors, etc) it may be disabled:
  05/04/2018 11:30:43    - use '-nowf' to disable wireframing completely.
  05/04/2018 11:30:43    - use '-nowcr' to disable the Copy Rectangle after the
  05/04/2018 11:30:43      moved window is released in the new position.
  05/04/2018 11:30:43   Also see the -help entry for tuning parameters.
  05/04/2018 11:30:43   You can press 3 Alt_L's (Left "Alt" key) in a row to 
  05/04/2018 11:30:43   repaint the screen, also see the -fixscreen option for
  05/04/2018 11:30:43   periodic repaints.
  05/04/2018 11:30:43 
  05/04/2018 11:30:43 XFIXES available on display, resetting cursor mode
  05/04/2018 11:30:43   to: '-cursor most'.
  05/04/2018 11:30:43   to disable this behavior use: '-cursor arrow'
  05/04/2018 11:30:43   or '-noxfixes'.
  05/04/2018 11:30:43 using XFIXES for cursor drawing.
  05/04/2018 11:30:43 GrabServer control via XTEST.
  05/04/2018 11:30:43 
  05/04/2018 11:30:43 Scroll Detection: -scrollcopyrect mode is in effect to
  05/04/2018 11:30:43   use RECORD extension to try to detect scrolling windows
  05/04/2018 11:30:43   (induced by either user keystroke or mouse input).
  05/04/2018 11:30:43   If this yields undesired behavior (poor response, painting
  05/04/2018 11:30:43   errors, etc) it may be disabled via: '-noscr'
  05/04/2018 11:30:43   Also see the -help entry for tuning parameters.
  05/04/2018 11:30:43   You can press 3 Alt_L's (Left "Alt" key) in a row to 
  05/04/2018 11:30:43   repaint the screen, also see the -fixscreen option for
  05/04/2018 11:30:43   periodic repaints.
  05/04/2018 11:30:43 
  05/04/2018 11:30:44 XKEYBOARD: number of keysyms per keycode 7 is greater
  05/04/2018 11:30:44   than 4 and 51 keysyms are mapped above 4.
  05/04/2018 11:30:44   Automatically switching to -xkb mode.
  05/04/2018 11:30:44   If this makes the key mapping worse you can
  05/04/2018 11:30:44   disable it with the "-noxkb" option.
  05/04/2018 11:30:44   Also, remember "-remap DEAD" for accenting characters.
  05/04/2018 11:30:44 
  05/04/2018 11:30:44 X FBPM extension not supported.
  05/04/2018 11:30:44 X display is not capable of DPMS.
  05/04/2018 11:30:44 --------------------------------------------------------
  05/04/2018 11:30:44 
  05/04/2018 11:30:44 Default visual ID: 0x21
  05/04/2018 11:30:44 Read initial data from X display into framebuffer.
  05/04/2018 11:30:44 initialize_screen: fb_depth/fb_bpp/fb_Bpl 24/32/5120
  05/04/2018 11:30:44 rfbNewFramebuffer(0x108b6c0, 0x0, 1280, 1024, 8, 1, 4)
  05/04/2018 11:30:44 Pixel format for client 172.17.0.1:
  05/04/2018 11:30:44   32 bpp, depth 24, little endian
  05/04/2018 11:30:44   true colour: max r 255 g 255 b 255, shift r 16 g 8 b 0
  05/04/2018 11:30:44 
  05/04/2018 11:30:44 X display :20 is 32bpp depth=24 true color
  05/04/2018 11:30:44 
  05/04/2018 11:30:44 calling setTranslateFunction()...
  05/04/2018 11:30:44 Pixel format for client 172.17.0.1:
  05/04/2018 11:30:44   32 bpp, depth 24, little endian
  05/04/2018 11:30:44   true colour: max r 255 g 255 b 255, shift r 16 g 8 b 0
  05/04/2018 11:30:44 no translation needed
  05/04/2018 11:30:44   done.
  05/04/2018 11:30:44 TS_REDIR is empty, restarting...
  05/04/2018 11:30:44 
  05/04/2018 11:30:44 Xinerama is present and active (e.g. multi-head).
  05/04/2018 11:30:44 Xinerama: number of sub-screens: 1
  05/04/2018 11:30:44 Xinerama: no blackouts needed (only one sub-screen)
  05/04/2018 11:30:44 
  05/04/2018 11:30:44 fb read rate: 58 MB/sec
  05/04/2018 11:30:44 The X server says there are 10 mouse buttons.
  05/04/2018 11:30:44 screen setup finished.
  PORT=5900
  05/04/2018 11:30:44 
  
  The VNC desktop is:      f6627eb9da23:0
  
  ******************************************************************************
  Have you tried the x11vnc '-ncache' VNC client-side pixel caching feature yet?
  
  The scheme stores pixel data offscreen on the VNC viewer side for faster
  retrieval.  It should work with any VNC viewer.  Try it by running:
  
      x11vnc -ncache 10 ...
  
  One can also add -ncache_cr for smooth 'copyrect' window motion.
  More info: http://www.karlrunge.com/x11vnc/faq.html#faq-client-caching
  
  05/04/2018 11:30:44 rfbProcessClientSecurityType: executing handler for type 2
  05/04/2018 11:30:44 Battling with something for -norepeat!! (1 resets left)
  05/04/2018 11:30:44 Disabled X server key autorepeat.
  05/04/2018 11:30:44   to force back on run: 'xset r on' (2 times)
  05/04/2018 11:30:44 created   xdamage object: 0x20002c
  05/04/2018 11:30:50 cutbuffer_send: no send: uninitialized clients
  05/04/2018 11:30:57 client useCopyRect: 172.17.0.1 0
  05/04/2018 11:30:57 Pixel format for client 172.17.0.1:
  05/04/2018 11:30:57   8 bpp, depth 8
  05/04/2018 11:30:57   true colour: max r 7 g 7 b 3, shift r 0 g 3 b 6
  05/04/2018 11:30:57 rfbProcessClientNormalMessage: ignoring unsupported encoding type ultraZip
  05/04/2018 11:30:57 Using compression level 9 for client 172.17.0.1
  05/04/2018 11:30:57 Using image quality level 0 for client 172.17.0.1
  05/04/2018 11:30:57 Using JPEG subsampling 1, Q15 for client 172.17.0.1
  05/04/2018 11:30:57 Enabling X-style cursor updates for client 172.17.0.1
  05/04/2018 11:30:57 Enabling full-color cursor updates for client 172.17.0.1
  05/04/2018 11:30:57 Enabling cursor position updates for client 172.17.0.1
  05/04/2018 11:30:57 Enabling KeyboardLedState protocol extension for client 172.17.0.1
  05/04/2018 11:30:57 Enabling NewFBSize protocol extension for client 172.17.0.1
  05/04/2018 11:30:57 Enabling LastRect protocol extension for client 172.17.0.1
  05/04/2018 11:30:57 Enabling SupportedMessages protocol extension for client 172.17.0.1
  05/04/2018 11:30:57 Enabling SupportedEncodings protocol extension for client 172.17.0.1
  05/04/2018 11:30:57 Enabling ServerIdentity protocol extension for client 172.17.0.1
  05/04/2018 11:30:57 Enabling Xvp protocol extension for client 172.17.0.1
  05/04/2018 11:30:57 Using tight encoding for client 172.17.0.1
  05/04/2018 11:31:30 created selwin: 0x20002d
  05/04/2018 11:31:30 called initialize_xfixes()
  </code></pre>