Running GUI apps with Docker


http://fabiorehm.com/blog/2014/09/11/running-gui-apps-with-docker/


I’ve been doing all of my real (paid) work on VMs / containers for a while now but when it comes to writing Java code for some projects for university I stillneed to move away from using vim and install some full blown IDE in order to beproductive. This has been bothering me for quite some time but this week Iwas finally able put the pieces together to run NetBeans in a Docker container so that I can avoidinstalling a lot of Java stuff on my machine that I don’t use on a daily basis.

There are a few different options to run GUI applications inside a Dockercontainer like using SSH with X11 forwarding,or VNC but the simplest one that Ifigured out was to share my X11 socket with the container and use it directly.

The idea is pretty simple and you can easily it give a try by running a Firefoxcontainer using the following Dockerfile as a starting point:

FROM ubuntu:14.04

RUN apt-get update && apt-get install -y firefox

# Replace 1000 with your user / group id
RUN export uid=1000 gid=1000 && 
    mkdir -p /home/developer && 
    echo "developer:x:${uid}:${gid}:Developer,,,:/home/developer:/bin/bash" >> /etc/passwd && 
    echo "developer:x:${uid}:" >> /etc/group && 
    echo "developer ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/developer && 
    chmod 0440 /etc/sudoers.d/developer && 
    chown ${uid}:${gid} -R /home/developer

USER developer
ENV HOME /home/developer
CMD /usr/bin/firefox

docker build -t firefox . it and run the container with:

docker run -ti --rm 
       -e DISPLAY=$DISPLAY 
       -v /tmp/.X11-unix:/tmp/.X11-unix 
       firefox

If all goes well you should see Firefox running from within a Docker container.

Getting a NetBeans container up and running

Preparing a NetBeans base image was not that straightforward since we need toinstall some additional dependencies (namely the libxext-dev, libxrender-devand libxtst-dev packages) in order to get it to connect to the X11 socketproperly. I also had trouble using OpenJDK and had to switch to Oracle’s Javafor it to work.

After lots of trial and error, I was finally able to make it work and the resultis a base image available at the Docker Hubwith sources on GitHub.

Here’s a quick demo of it in action:

Future work

Over the next few months I’ll be working on a Play!app and will hopefully write a blog post on the workflow I used. Stay tunned for more :)


PS: This approach of sharing the X11 socket also be applied to vagrant-lxccontainers and I’ll document that on the project’s Wiki when Ihave a chance.


原文地址:https://www.cnblogs.com/ztguang/p/12644961.html