How to make a Docker container with VNC access

Gustavo Lewin
3 min readMar 6, 2024

--

Docker container with a VNC server — Empowering seamless GUI application management within Dockerized environments.

Introduction

Docker containers have become indispensable for their efficiency in packaging applications and their dependencies. However, when it comes to managing graphical user interfaces (GUIs) within Docker containers, developers often encounter challenges. This is where Virtual Network Computing (VNC) servers come into play, offering a solution to remotely access and interact with GUIs. Imagine having the convenience of a virtual machine (VM) within a Docker container. In this tutorial, we’ll delve into the creation of a Docker container integrated with a VNC server, providing developers with the flexibility to manage and troubleshoot GUI applications as if they were operating within a VM environment.

Requirements

This tutorial assumes a Linux environment for development and execution. Additionally, familiarity with basic Docker concepts and commands will be beneficial. Lastly, ensure that Docker is installed and configured on your system, as we’ll be leveraging its capabilities extensively throughout this tutorial. With these requirements in place, let’s embark on the journey of setting up a Docker container with a VNC server.

Step-by-Step

  1. Create a folder called vnc_ubuntu:
$ mkdir vnc_ubuntu
$ cd vnc_ubuntu

2. The following code is a Dockerfile that utilizes an Ubuntu image as its base, proceeding to install VNC and additional prerequisites necessary for the VNC server. Save it as vnc_ubuntu/Dockerfile:

# Use an official Ubuntu base image
FROM ubuntu:20.04

# Avoid warnings by switching to noninteractive for the build process
ENV DEBIAN_FRONTEND=noninteractive

ENV USER=root

# Install XFCE, VNC server, dbus-x11, and xfonts-base
RUN apt-get update && apt-get install -y --no-install-recommends \
xfce4 \
xfce4-goodies \
tightvncserver \
dbus-x11 \
xfonts-base \
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Setup VNC server
RUN mkdir /root/.vnc \
&& echo "password" | vncpasswd -f > /root/.vnc/passwd \
&& chmod 600 /root/.vnc/passwd

# Create an .Xauthority file
RUN touch /root/.Xauthority

# Set display resolution (change as needed)
ENV RESOLUTION=1920x1080

# Expose VNC port
EXPOSE 5901

# Set the working directory in the container
WORKDIR /app

# Copy a script to start the VNC server
COPY start-vnc.sh start-vnc.sh
RUN chmod +x start-vnc.sh

# List the contents of the /app directory
RUN ls -a /app

3. The following script initiates the VNC server. The Dockerfile is configured to copy it to a directory within the image. Save it as vnc_ubuntu/start-vnc.sh:

#!/bin/bash

echo 'Updating /etc/hosts file...'
HOSTNAME=$(hostname)
echo "127.0.1.1\t$HOSTNAME" >> /etc/hosts

echo "Starting VNC server at $RESOLUTION..."
vncserver -kill :1 || true
vncserver -geometry $RESOLUTION &

echo "VNC server started at $RESOLUTION! ^-^"

echo "Starting tail -f /dev/null..."
tail -f /dev/null

4. Now you can build an image called vnc_ubuntu:

$ docker build -t vnc_ubuntu .

5. Run the container with this command:

$ docker run -dt --rm --name vnc_ubuntu -p 5901:5901 vnc_ubuntu

6. To start the VNC server, it’s necessary to execute the script start-vnc.sh. You can automate this process by specifying an entrypoint in the Dockerfile. However, if you prefer manual execution like me:

$ docker exec -ti vnc_ubuntu /bin/bash

7. Once you have access to the container’s CLI:

# ./start-vnc.sh

8. Now you can use a VNC client in the host machine to access your container via localhost:5901 (tested with RealVNC Viewer). The password was defined in the Dockerfile as “password”.

Snapshot of the VNC Docker desktop showcasing a graphical user interface within a Docker container environment.

Conclusion

That’s it, congratulations! You’ve successfully configured a Docker container with a VNC server, empowering you to seamlessly manage and troubleshoot GUI applications within Dockerized environments. The possibilities are endless — from developing and testing GUI-based software to remotely accessing and debugging applications across different platforms. Embrace the flexibility and efficiency of containerized environments, and continue exploring innovative solutions to streamline your development workflow. Happy coding!😎😎

--

--