In some cases, you don't want to setup a complicated Linux Desktop environment on your local machine, with virtualization or dual-boot. You can use Docker to run a Linux Desktop environment in a container. This is a simple way to try out Linux Desktop environment without installing it on your local machine.

You can use Docker to run a Linux Desktop environment in a container. This is a simple way to try out Linux Desktop environment without installing it on your local machine.

Step 1 - Install Docker

No matter what host OS you are using, (Support Windows, Linux, MacOS), you need to have Docker installed on your local machine obviously.

You can follow the instructions on the official Docker website.

Step 2.A - Pull my built image from Docker Hub

I have built a Docker image with the Linux Desktop environment. You can pull it from Docker Hub.

docker pull hub.aiursoft.cn/aiursoft/internalimages/remote-desktop:latest

Then you can run the container with the following command:

docker run -d -p 33900:3389 --name remote-desktop hub.aiursoft.cn/aiursoft/internalimages/remote-desktop:latest

Now, you can use a Remote Desktop client to connect to localhost:33900 to access the Linux Desktop environment.

Step 2.B - Build the image yourself

If you want to build the image yourself, you can write a new Dockerfile with the following content:

FROM ubuntu:22
EXPOSE 3389/tcp
ARG USER=test
ARG PASS=1234
ARG X11Forwarding=false

ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=UTC

RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
    apt-get install -y tzdata && \
    echo "Etc/UTC" > /etc/timezone && \
    ln -fs /usr/share/zoneinfo/UTC /etc/localtime && \
    dpkg-reconfigure -f noninteractive tzdata

RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
        apt-get install -y ubuntu-desktop-minimal dbus-x11 xrdp sudo; \
    [ $X11Forwarding = 'true' ] && apt-get install -y openssh-server; \
    apt-get autoremove --purge; \
    apt-get clean; \
    rm /run/reboot-required*

RUN useradd -s /bin/bash -m $USER -p $(openssl passwd "$PASS"); \
    usermod -aG sudo $USER; \
    adduser xrdp ssl-cert; \
    # Setting the required environment variables
    echo 'LANG=en_US.UTF-8' >> /etc/default/locale; \
    echo 'export GNOME_SHELL_SESSION_MODE=ubuntu' > /home/$USER/.xsessionrc; \
    echo 'export XDG_CURRENT_DESKTOP=ubuntu:GNOME' >> /home/$USER/.xsessionrc; \
    echo 'export XDG_SESSION_TYPE=x11' >> /home/$USER/.xsessionrc; \
    # Enabling log to the stdout
    sed -i "s/#EnableConsole=false/EnableConsole=true/g" /etc/xrdp/xrdp.ini; \
    # Disabling system animations and reducing the
    # image quality to improve the performance
    sed -i 's/max_bpp=32/max_bpp=16/g' /etc/xrdp/xrdp.ini; \
    gsettings set org.gnome.desktop.interface enable-animations true; \
    # Listening on wildcard address for X forwarding
    [ $X11Forwarding = 'true' ] && \
        sed -i 's/#X11UseLocalhost yes/X11UseLocalhost no/g' /etc/ssh/sshd_config || \
        :;
RUN echo "$USER ALL=(ALL) NOPASSWD:ALL" | tee -a /etc/sudoers.d/$USER

CMD rm -f /var/run/xrdp/xrdp*.pid >/dev/null 2>&1; \
    service dbus restart >/dev/null 2>&1; \
    /usr/lib/systemd/systemd-logind >/dev/null 2>&1 & \
    [ -f /usr/sbin/sshd ] && /usr/sbin/sshd; \
    xrdp-sesman --config /etc/xrdp/sesman.ini; \
    xrdp --nodaemon --config /etc/xrdp/xrdp.ini

Then you can build the image with the following command:

file

docker build -t remote-desktop .

And run the container with the following command:

docker run -d -p 33900:3389 --name remote-desktop remote-desktop

Step 3 - Connect to the Linux Desktop environment

Now, you can use a Remote Desktop client to connect to localhost:33900 to access the Linux Desktop environment.

You can use the following credentials to login:

  • Username: test
  • Password: 1234

Enjoy your Linux Desktop environment in a Docker container!

file

You can install a browser now. With command:

wget https://dl-ssl.google.com/linux/linux_signing_key.pub -O /tmp/google.pub
sudo gpg --no-default-keyring --keyring /etc/apt/keyrings/google-chrome.gpg --import /tmp/google.pub
echo 'deb [arch=amd64 signed-by=/etc/apt/keyrings/google-chrome.gpg] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list
sudo apt update
sudo apt install -y google-chrome-stable

file

Conclusion

In this tutorial, you have learned how to run a Linux Desktop environment in a Docker container. This is a simple way to try out Linux Desktop environment without installing it on your local machine.