How Do I Create a Docker Container with a GUI?
Docker is really great for spinning up a quick service that normally runs on a server. It's perfect for a web service, such as a Wordpress site. But, what if you want to run a Linux desktop on your Mac or PC? You could create an Ubuntu VM, but you can also use Docker.
Assuming you've gone through all the tutorials on http://docker.com, you'll quickly realize that Docker containers are very temporary things. Very easy to create and destroy, along with any data store in it. This is great for many applications, but not if you want to use it like a Linux desktop.
Docker has plenty of support for running a desktop, it just takes a little digging. The simplest and most reliable method I've found is it to run Ubuntu with an xdrp remote desktop using docker-composer. This provides the ability to save files across starting and stopping the container.
To do this, I created a Docker container using docker-compose. I started with this container:
Then created this .yml file for composer:
cat docker-compose.yml version: "2" services: xrdp: build: . image: "xrdp:test" ports: - "3389:3389" volumes: - /home/desktop
Then I created the container
(cd docker-ubuntu-xrdp-mate; build .)
This pulls in the Docker container for xrdp and adds a volume (/home/desktop) for storage.
Then I started the container;
docker-compose up -d
The container is now running. The nice thing about running it this way is that port mapping is 1:1. Enter ps to see the container running and ports it's using:
docker-compose ps Name Command State Ports -------------------------------------------------------------------------------------- dockerubuntuxrdpmate_xrdp_1 /usr/bin/supervisord -n Up 0.0.0.0:3389->3389/tcp
Now that the container is running, you can login via RDP. But first, we need the IP address of the container (0.0.0.0) won't work on a Mac. Enter this command to get the IP address of the container:
docker-machine ip 192.168.99.100
Now you can use Remote Desktop to access the container. I use Microsoft Remote Desktop on my Mac. The address is simply 192.168.99.100, the default port is 3389. The login for this container is: desktop/desktop.
That's it for getting access to the desktop. To maintain your files you need to use the docker-compose start and docker-composer stop commands. Do NOT use docker-compose down! The container and all of your modified files will be destroyed.
This is a really handy and a lot quicker than installing Ubuntu in a VM. It also easier to maintain, if it get polluted just use docker-composer down. It will get rebuilt when docker-composer up is run.
Isolated NetworkDocker containers are "bridged" by default, which is a weird name because it means the container is on a host-only network. VirtualBox actually calls a host-only network "Host Only Network", a VirtualBox bridged network is actually accessible from the LAN, it's bridged on the primary interface, therefore it gets it's IP address from your router.
There are ways around the container being isolated and it's different for Mac OS and Windows. But, I haven't figured this out yet.