Docker container + X11 on macOS = Awesome

I’ve been exploring dockerizaton lately more and more and I think I like the idea of having my apps in containers.  I am not going to lie, it does take some time to get used to, but it is pretty cool the things that you can do.

One thing that I think I wanted to accomplish was able to run X11 apps through docker.  I found Docker Headless VNC container and that was slick.  Where I was able to run a whole Xfce environment within a Docker container!!! kudos to those folks.  Now, my challenge was that in my case, I didn’t want a fat image and/or configure each app to download of configuring. So, this wasn’t my solution at this time.  So, I decided to explore further and found a way to run X11 from a Docker container in macOS.  Below are the steps on how to accomplish this.

  1. Install XQuartz (X11),  Docker and homebrew.  You can follow their respective installation process.
  2. Install socat which is a “multipurpose relay.”  This will allow us to call the display.
    brew install socat
  3. Create you local Dockerfile.  In this particular case, I am using Ubuntu latest (which at the time of this post is 16.04.1 LTS).  On your terminal, you can do “vi Dockerfile” and paste the info from below in that file.
    FROM ubuntu:latest
    
    RUN apt-get update && apt-get install -y firefox
    
    RUN useradd -ms /bin/bash developer && \
     echo "\ndeveloper ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
    
    USER developer
    ENV HOME /home/developer
    CMD /usr/bin/firefox
  4. Then, let’s build the Docker image by doing the following…
    docker build -t firefox .
  5. Now, on a different terminal, run the following command…
    socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"
  6. Then come back to your old terminal (or a different one) and run this command
    docker run -ti --rm \
    -e DISPLAY=$(ipconfig getifaddr en0):0 \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    firefox
  7. At this point, you should have opened a Firefox instance from your Docker container through X11 on macOS 🙂
    If you didn’t… take a look at the steps again.  Happy dev and opening other X11 instances.

Sources:

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

https://stackoverflow.com/questions/16296753/can-you-run-gui-apps-in-a-docker-container#

https://github.com/mbessler/docker-gnucash

https://hub.docker.com/r/palshife/ssh/

https://hub.docker.com/r/palshife/gnucash/

https://github.com/ConSol/docker-headless-vnc-container

https://askubuntu.com/questions/192050/how-to-run-sudo-command-with-no-password

https://stackoverflow.com/questions/27701930/add-user-to-docker-container

https://askubuntu.com/questions/842572/file-in-etc-sudoers-d-not-recognized

6 thoughts on “Docker container + X11 on macOS = Awesome

  1. Steve

    Good Grief! Typos are easy to do! Sorry, should be:
    socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\”$DISPLAY\”

    i.e. there should be a ‘T:’, as in ‘UNIX-CLIENT:’

    Reply
  2. Steve

    Great Post! One typo:
    socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIEN\”$DISPLAY\”
    should be:
    socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT\”$DISPLAY\”

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.