My dream come true: Launching GUI Docker sessions with DX11

Brick Pop
Stack Me Up
Published in
8 min readJan 6, 2020

--

Photo by Mike Meeks on Unsplash

Get ready, because today we will get you to launch complete Linux Desktop Environments on demand. You’ll create your ideal graphical environment in a few minutes and launch it in a few seconds.

Sounds good, doesn’t it?

If you are a Linux user, you may be familiar with Virtual Machines to try new stuff and experiment before you choose a distro. Virtualization is cool, but you know the downsides:

  • You need to boot a separate OS with its own Kernel and added overhead
  • The guest OS preallocates a all the RAM it may use, even if only 1% of it is used
  • Your guest OS won’t be as smooth
  • The guest OS needs virtual partitions, networking, guest tools, etc
  • You have to manually install it

A new hope: Docker

Something “clicked” in my head when I first heard of Docker:

  • Run any Linux command from any OS image, reusing the same Kernel
  • Run on disposable stateless containers that you could break and relaunch right after

That’s so cool, why don’t we all live on Docker containers?

Docker is intended for command line workflows and targeted to run servers, daemons, compilers, etc. Not desktop environments.

Enter x11docker

X11Docker is a feature rich script that addresses this problem. It does the heavy lifting to start an X11 server on the host system and makes Dockerized apps talk to it. So with it, you can run GUI apps from the Docker image that you like! 😃🎉

I’ve been using it for a month now. While I applaud the brilliant work that it has behind, I think that is that X11Docker is not a VM replacement.

Instead, I see it as a replacement for docker run <image> <command>.
A minimal VM-like scenario looks much more like:

Unless you make a script of it, I bet you’ll hardly use x11docker.

The missing piece: DX11

My yearn for multiple independent Desktop sessions kept growing until I ditched the script above and wrote a session manager that made my dream come true.

Without further ado, let me introduce DX11! 😃🎉

DX11 is a Linux program that allows you to run X11 Linux sessions with Docker. A “session” consists of a Docker image, a user home folder and optional settings.

Photo by Adam Whitlock on Unsplash

DX11 combines the native performance of Docker on Linux with many of the features you’d get when running VM’s:

  • As fluid and fast as your native system
  • Reuse the same kernel, system memory and disk storage
  • Scriptable, 100% reproducible desktop environments
  • Immutable desktop systems (similar in concept to Silverblue)
  • Disposable and throwaway sessions
  • Independent home folder persistence for each session
  • Optional home folder encryption
  • Optional Tor networking
  • Unprivileged execution, with the option to attach separately as root
  • Isolated sandboxed environments

Use cases

The list grows pretty much as you imagination allows. But a few examples include:

  • Distro hopping. Try a new distribution or desktop environment before you make it your daily driver. Evaluate the real performance instead of hoping that a native installation would be smoother.
  • Full portability. Copy your session’s data to a brand new computer, pull the Docker image and continue to work as if nothing ever happened.
  • Give your developers a reference workstation Dockerfile that they can still customize. Avoid the “works on my computer” problem.
  • Speed up the landing of new developers with an already functional session.
  • Post your session’s Dockerfile on your StackOverflow question or GitHub issue. Let others reproduce the exact same environment you have when you encounter that nasty error.
  • Deprecation proof development environments. Your legacy React Native or Cordova setup is carved in stone. If an SDK upgrade breaks a project, nothing else will be affected. Relaunch your stable session and npm install will keep working as usual.
  • Post your session’s Dockerfile on a public repo. Let others clone it, fork it and make it even cooler.
  • Evaluate bloated/unsafe software on a clone of your workstation and drop the changes when you are done experimenting.
  • Upgrade and downgrade at will, without the risk of losing any local data. Docker build will never interact with any session, even if they are running.
  • Replace your all-in-one bloated computer with minimal isolated systems that you can use on demand. One for your sysadmin work, one for your side projects, one for gaming, one for anonymous browsing, etc.
  • Forget about multiple SSH key management by having a different session for each project, with its corresponding key files and credentials.
  • Cybercafes, where a Linux computer spins up a fresh disposable desktop when you pay for it and terminates it when you are done.
  • Record a screencast of that upcoming Gnome/KDE release and transfer the video file to your main system right away. No VirtualBox Guest Additions needed.
  • If you write extensions for KDE, Gnome, etc., you can publish a Dockerfile to let people run sessions with them preinstalled.

You name it. The more you use it, the more you realize the enormous potential it has.

Well, show me the money!

Create a session

Let’s create a simple Dockerfile that customizes a Deepin Linux base image with a few extra packages:

The hard work is already done in x11docker/deepin. All we need to do is adding the commands that we would run after installing the system ourselves. In this case, a few system tools and a browser.

Now, let’s create our first session with it:

  • This creates or updates a session named my-workstation
  • A Docker image is built from Dockerfile and my-workstation is linked to it

Run the new session

That’s even simpler:

This will switch to a new Virtual Terminal and start Deepin in a fraction of what it would take to boot the entire OS.

Deepin Desktop Environment started by DX11

Do some work, create some files and log out. Run dx11 run my-workstation again. Your prior work is there, your desktop boots in seconds and your session has the exact software you need.

How about a brand new Gnome session? No problem, let’s extend a Dockerfile from ubuntu:eoan and install a few packages that we need to use. Two commands (dx11 new and dx11 run) and two minutes later, our DX11 session looks like this:

Gnome Desktop running on Ubuntu 19.10. Started by DX11.

Et voilà! Would you like to do the same with Ubuntu Mate? No problem.

We haven’t touched a single installer yet, but here is our third session in less than 10 minutes:

Mate Desktop started by DX11

That’s insane! Starting a session with an already built image will take less than 10 seconds.

Attach as root

While my-workstation is running, you can attach to it from your normal session:

This will give you a root console, which allows you to try and change whatever you like on the system.

  • If you log out of the system, any changes besides /home/user will be lost
  • You can consolidate them into your Docker image using docker commit if you like

More base images

You can see a few examples provided on the DX11 repo and you can also checkout the Docker Hub images contributed by the X11Docker team.

Contribute

DX11 is far from a finished tool. There are still bits and pieces that need attention, but so far I’m excited to be writing an article like this 😃

  • If you like it, please give it a try today and provide valuable feedback so we all can benefit from it.
  • Also, feel free to explore these Docker Hub images, experiment with your ideal setup and submit Pull Requests with your examples

Future work

The next thing I’d like to see is a “Docker Hub” for desktop sessions. Docker Hub itself would do the trick for the image registry, but most DX11 users would expect screenshots, screencasts, comments, etc.

That would be a nice challenge to undertake.

Aside conclusions: Nim

In my last article I briefly explained that I was going to check out the Nim programming language and see what I could do with it.

After the experience of building DX11, I’d like to share a few thoughts about it:

  • Nim is a nice and clean language
  • I’ve felt that the language is well thought out and it leads to writing nice code quickly
  • The compiler is fast and produces small, dependency-free binaries. Sometimes, error reporting could be better, but as a newcomer to Nim I’ve been able to quickly find the root issues and get them fixed
  • However, the main drawback is the lack of a graphical debugger for the most common IDE’s. You can use CLI tools, but things are likely to be slower when working on complex projects
  • Although not as popular as NodeJS’s, Go or Rust, Nim has many libraries available for use on your project, covering the typical dependencies you may find in systems programming

In my opinion it’s worth giving it a try. I hope more people get involved so we can tackle bigger and more complex projects with it.

That’s it for today! I hope you enjoy the Christmas toy 🎁 and if you like it, please clap out loud 👏, show some love ❤️, subscribe and give DX11 a try!

Until the next time, take care!

--

--

Brick Pop
Stack Me Up

Full stack tech, thoughts and life. Personal journey is underway. The future is now.