Docker containers are amazing, you can build multiple self-contained environments with a minimum amount of effort. They can be used for developing, deploying to production or just test new software and tools. The fact that they are so lightweight and minimal is the most powerful characteristic, but it can be limiting in some cases, like when we need to run something that has graphics that need to be rendered on screen.
Something that many people don’t know is that is actually possible to attach a graphical server to a container and visualize it.
Use VNC to connect to container’s GUI 🔗
We’ll use Virtual Network Computing (VNC) for this task, so as a requirement we first need to have it installed on our system. Once we have it, we can proceed with the creation of a custom docker image. I’ll use Ubuntu as a base image:
FROM ubuntu:22.04
RUN apt-get update \
&& apt-get install -y x11vnc xvfb\
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
ENV DISPLAY=:20
WORKDIR /app
COPY entryscript.sh .
RUN chmod +x entryscript.sh
EXPOSE 5920
ENTRYPOINT [ "/app/entryscript.sh" ]
Now we need to define the entry script as follows, adjusting the resolution and password accordingly:
#!/bin/bash
Xvfb :20 -screen 0 2560x1440x16 &
x11vnc -passwd test -display :20 -N -forever &
tail -f /dev/null
And we are ready! Let’s build the container with:
docker build -t vnc_viewer .
and run it:
docker run -it --rm -p 5920:5920 vnc_viewer
Now all we have left to do is open VNC and connect to localhost:5920
, and
the job is done. Just for testing purposes, we can install x11-apps
and
run xclock
as a test!