Skip to content

Instantly share code, notes, and snippets.

@wware
Created December 7, 2020 14:37
Show Gist options
  • Save wware/3ca7858942a1ae66bc7ed37c0cd8ef3d to your computer and use it in GitHub Desktop.
Save wware/3ca7858942a1ae66bc7ed37c0cd8ef3d to your computer and use it in GitHub Desktop.

Making X Windows work for a Docker container

https://stackoverflow.com/questions/49169055

This is because the container couldn't access the x11 socket of the host. so when doing the docker run, need to include these two flag.

-v /tmp/.X11-unix:/tmp/.X11-unix
-e DISPLAY=unix$DISPLAY

and after this, we need to do another operation. because the default settings of X11 only allows local users to print. so we need to change this to all users.

$ sudo apt-get install x11-xserver-utils
$ xhost +

then the problem solved.

I really want to be able to use Docker containers with GUIs anywhere, so I need to figure out if this will also work on OS X.

FROM ubuntu
MAINTAINER [email protected]
RUN apt update -y
RUN apt install -y apt-utils
RUN apt install -y python2.7 python-tk
COPY ./tryit.py /bin/tryit.py
CMD tryit.py
#!/bin/bash
docker build -t dui .
docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix:rw dui tryit.py
#!/usr/bin/env python2
import Tkinter as tk
class ExampleApp(tk.Frame):
''' An example application for TkInter. Instantiate
and call the run method to run. '''
def __init__(self, master):
# Initialize window using the parent's constructor
tk.Frame.__init__(self,
master,
width=300,
height=200)
# Set the title
self.master.title('TkInter Example')
# This allows the size specification to take effect
self.pack_propagate(0)
# We'll use the flexible pack layout manager
self.pack()
# The greeting selector
# Use a StringVar to access the selector's value
self.greeting_var = tk.StringVar()
self.greeting = tk.OptionMenu(self,
self.greeting_var,
'hello',
'goodbye',
'heyo')
self.greeting_var.set('hello')
# The recipient text entry control and its StringVar
self.recipient_var = tk.StringVar()
self.recipient = tk.Entry(self,
textvariable=self.recipient_var)
self.recipient_var.set('world')
# The go button
self.go_button = tk.Button(self,
text='Go',
command=self.print_out)
# Put the controls on the form
self.go_button.pack(fill=tk.X, side=tk.BOTTOM)
self.greeting.pack(fill=tk.X, side=tk.TOP)
self.recipient.pack(fill=tk.X, side=tk.TOP)
def print_out(self):
''' Print a greeting constructed
from the selections made by
the user. '''
print('%s, %s!' % (self.greeting_var.get().title(),
self.recipient_var.get()))
def run(self):
''' Run the app '''
self.mainloop()
app = ExampleApp(tk.Tk())
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment