4

Run Ubuntu Linux in Docker with Desktop Environment and VNC

 2 years ago
source link: https://computingforgeeks.com/run-ubuntu-linux-in-docker-with-desktop-environment-and-vnc/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Run Ubuntu Linux in Docker with Desktop Environment and VNC

Welcome to this guide on how to run Ubuntu Linux in Docker with Desktop Environment and VNC. Many times installing the Ubuntu desktop on a physical machine/virtual machine is a tiring task and consumes a lot of time to provision and install. There is a simplified method you can use to run Ubuntu Linux with a desktop environment. Many thanks to Docker!

Using a Docker image, you can run Ubuntu with LXDE and LXQt desktop environments and access it via an HTML5 VNC interface. This is even more fascinating since you can make several other configurations using the docker image such as:

  • HTTP Base Authentication
  • SSL encryption
  • Default Desktop User setup
  • Screen Resolution adjustment
  • Generate Dockerfile from jinja template
  • Sound (Preview version and Linux only)
  • Deploy to a subdirectory (relative url root)

Furthermore, there are several Ubuntu versions and flavors that include:

  • focal: Ubuntu 20.04 (latest)
  • focal-lxqt: Ubuntu 20.04 LXQt
  • bionic: Ubuntu 18.04
  • bionic-lxqt: Ubuntu 18.04 LXQt
  • xenial: Ubuntu 16.04 (deprecated)
  • trusty: Ubuntu 14.04 (deprecated)

Let’s dive in!

Step 1 – Install Docker on your System

To be able to run the Ubuntu Docker image, of course, you need the Docker engine installed on your system. Below is a dedicated guide that can help you install the Docker engine on any Linux distribution.

Start and enable Docker to run automatically on system boot.

sudo systemctl start docker && sudo systemctl enable docker

Add your system user to the Docker group to be able to execute Docker commands without sudo.

sudo usermod -aG docker $USER
newgrp docker

Verify the installed Docker version.

$ docker version
Client: Docker Engine - Community
 Version:           20.10.12
 API version:       1.41
 Go version:        go1.16.12
 Git commit:        e91ed57
 Built:             Mon Dec 13 11:45:48 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true
....

Step 2 – Pull the Ubuntu Docker Image

As said earlier, there are several images for Ubuntu that can be pulled. These images are available on the Docker page and can be pulled using Docker as shown.

##For Ubuntu 20.04 (latest)
docker pull dorowu/ubuntu-desktop-lxde-vnc:latest

##For Focal with LXDE
docker pull dorowu/ubuntu-desktop-lxde-vnc:focal

##For Focal with LXQt
docker pull dorowu/ubuntu-desktop-lxde-vnc:focal-lxqt

##For Bionic with LXDE
docker pull dorowu/ubuntu-desktop-lxde-vnc:bionic

##For Bionic with LXQt
docker pull dorowu/ubuntu-desktop-lxde-vnc:bionic-lxqt

##For Trusty 
docker pull dorowu/ubuntu-desktop-lxde-vnc:trusty

##For Xenial
docker pull dorowu/ubuntu-desktop-lxde-vnc:xenial

Once the desired Ubuntu image has been pulled, check if it is available in the local registry.

$ docker images
REPOSITORY                       TAG       IMAGE ID       CREATED         SIZE
dorowu/ubuntu-desktop-lxde-vnc   latest    1a89db715923   10 months ago   1.32GB

Step 3 – Run the Ubuntu Container with Desktop Environment

Here, we will go through several methods with configuration on how you can run the Ubuntu container to give you an intuitive experience.

Quick Start.

You can simply run the container and access it via port 6080 using the command below.

docker run -d \
  --name ubuntu_desktop \
  -v /dev/shm:/dev/shm \
  -p 6080:80 \
  dorowu/ubuntu-desktop-lxde-vnc

In the above command, we have set the container name to ubuntu_desktop and a persistent volume at /dev/shm Remember to replace ubuntu-desktop-lxde-vnc with the appropriate image pulled.

Check if the container is running with the port exposed.

$ docker ps
CONTAINER ID   IMAGE                            COMMAND         CREATED          STATUS                             PORTS                                   NAMES
f8fd69e3865e   dorowu/ubuntu-desktop-lxde-vnc   "/startup.sh"   17 seconds ago   Up 15 seconds (health: starting)   0.0.0.0:6080->80/tcp, :::6080->80/tcp   ubuntu_desktop

As seen from the output, we have port 6080 exposed. Allow it through the firewall and proceed to access the Ubuntu Desktop using the URL http://IP_Address:6080/.

Set HTTP Base Authentication

You can also set a password to be used when accessing the HTTP page on port 6080 by adding the HTTP_PASSWORD variable to the above command:

docker run -d \
  --name ubuntu_desktop \
  -e HTTP_PASSWORD=Passw0rd \
  -v /dev/shm:/dev/shm \
  -p 6080:80 \
  dorowu/ubuntu-desktop-lxde-vnc

Now accessing the page, you will be required to provide a password.

Enable VNC Viewer

The VNC viewer is accessed via a separate port 5900 that needs to be exposed in order to access it. The VNC Viewer port can be exposed as below.

docker run -d \
  --name ubuntu_desktop \
  -v /dev/shm:/dev/shm \
  -p 6080:80 \
  -p 5900:5900 \
  dorowu/ubuntu-desktop-lxde-vnc

Here, the VNC viewer should be available on port 5900.

You can as well set a password for the viewer by adding the VNC_PASSWORD variable to the above command:

docker run -d \
  --name ubuntu_desktop \
  -e VNC_PASSWORD=StrongPassword \
  -v /dev/shm:/dev/shm \
  -p 6080:80 \
  -p 5900:5900 \
  dorowu/ubuntu-desktop-lxde-vnc

When accessing the VNC viewer you will be required to provide the set password to access the VNC.

Encrypt with SSL.

You can generate SSL certificates for encryption. In this guide, we will generate self-signed certificates as below.

Ensure that OpenSSL is installed on your system before you proceed:

mkdir -p ssl
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ssl/nginx.key -out ssl/nginx.crt

Now when running the container, you need to specify the SSL_PORT and the path to the generated certificate as below.

docker run -d \
  --name ubuntu_desktop \
  -v /dev/shm:/dev/shm \
  -p 6081:443 \
  -e SSL_PORT=443 \
  -v ${PWD}/ssl:/etc/nginx/ssl \
  dorowu/ubuntu-desktop-lxde-vnc

Here, you can access the Ubuntu Desktop using the URL https://IP_address:6081.

Set the Default Desktop User

When running the container, the default user is root. However, you can add another user with the USER and PASSWORD variables as below:

docker run -d \
  --name ubuntu_desktop \
  -e USER=thor \
  -e PASSWORD=Passw0rd \
  -v /dev/shm:/dev/shm \
  -p 6080:80 \
  dorowu/ubuntu-desktop-lxde-vnc

Now here, we will have another user thor with password as Passw0rd added.

Deploy into a subdirectory

The container can be deployed into a subdirectory and serve as a prefix when accessing the page. For example, here we will specify the URL subdirectory as new.

Now specify the subdirectory using the RELATIVE_URL_ROOT environment variable.

docker run -d \
  --name ubuntu_desktop \
  -e RELATIVE_URL_ROOT=new \
  -p 6080:80 \
  dorowu/ubuntu-desktop-lxde-vnc

Remember the directory should not have any leading and trailing slash (/). Now you can access the Ubuntu Desktop using the URL http://IP_address:6080/new/

Set the Screen Resolution.

Normally the virtual desktop will automatically adjust to the browser window size when connecting to the server. But you can set a fixed resolution using the variable RESOLUTION as below.

docker run -d \
  --name ubuntu_desktop \
  -v /dev/shm:/dev/shm \
  -p 6080:80 \
  -e RESOLUTION=1360x768 \
  dorowu/ubuntu-desktop-lxde-vnc

You will have the virtual desktop with 1360×768(16:9) resolution set.

Enable Sound

Begin by inserting the kernel module below on your Linux system

sudo modprobe snd-aloop index=2

Now run the container with sound enabled as below.

docker run -d \
  --name ubuntu_desktop \
  -v /dev/shm:/dev/shm \
  -p 6080:80 \
  --device /dev/snd \
  -e ALSADEV=hw:2,0 \
  dorowu/ubuntu-desktop-lxde-vnc

In the above command, –device /dev/snd -e ALSADEV=hw:2,0 grants sound to the container and sets the basic ASLA device config to use card 2. Now you will have your container with sound enabled. Test this by playing a sound/video.

Step 4 – Manage the Container

YOu can easily stop and start the Ubuntu container using the commands:

##To stop
docker stop ubuntu_desktop

##To start
docker start ubuntu_desktop

##To delete
docker rm ubuntu_desktop

Futhermore, you can manage the container as a system service by creating the service file below.

sudo vim /etc/systemd/system/ubuntu-container.service

In the file, add the lines:

[Unit]
Description=Ubuntu container

[Service]
Restart=always
ExecStart=/usr/bin/docker start -a ubuntu_desktop
ExecStop=/usr/bin/docker stop -t 2 ubuntu_desktop

[Install]
WantedBy=local.target

Now reload the system daemon.

sudo systemctl daemon-reload

Start and enable the container to run automatically on boot.

sudo systemctl start ubuntu-container
sudo systemctl enable ubuntu-container

Check the status of the service.

Enjoy!

Closing Thoughts.

Now you have the easiest method to run your desired Ubuntu flavor with a Desktop Environment. Futhermore, we have seen the various configurations you can make to the container to make it as intuitive as possible.

Related posts.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK