Search code examples
dockercontainersheadlessxserver

Add xserver into Docker container (the host is headless)


I'm building a Docker container which have maven and some dependencies. Then it execute a script inside the container. It seems, one of that dependencies needs an Xserver to work. Nothing is shown on screen but it seems necessary and can't be avoided.

I got it working putting an ENV DISPLAY=x.x.x.x:0 on Dockerfile and it connects to the external Xserver and it works. But the point is to make a Docker self-sufficient container.

So I need to add a Xserver to my container adding in Dockerfile the necessary. And I want that Xserver only accessible by the Docker container itself and not externally.

The FROM of my Dockerfile is FROM ubuntu:15.04 and that is unchangeable because my Dockerfile have a lot of things depending of that specific version.

I've read some post about how to connect from docker container to Xserver of the Docker host machine, like this. But as I put in question's title, the Docker host is headless and doesn't have Xserver.

  • Which would be the minimum apt-get packages to install into the container to have a Xserver?
  • I guess in my Dockerfile will be needed the display environment var like ENV DISPLAY=:0. Is this correct?
  • Is anything else needed to be added in docker run command?

Thank you.


Solution

  • You can install and run a x11vnc inside your docker container. I'll show you how to make it running on a headless host and connect it remotely to run X applications(e.g. xterm).

    Dockerfile:

    FROM joprovost/docker-x11vnc
    
    RUN mkdir ~/.vnc && touch ~/.vnc/passwd
    RUN x11vnc -storepasswd "vncdocker" ~/.vnc/passwd
    EXPOSE 5900
    CMD ["/usr/bin/x11vnc", "-forever", "-usepw", "-create"]
    

    And build a docker image named vnc:

    docker build -t vnc .
    

    Run a container and remember map port 5900 to host for remote connect(I'm using --net=host here):

    docker run -d --name=vnc --net=host vnc
    

    Now you have a running container with x11vnc inside, download a vnc client like realvnc and try to connect to <server_ip>:5900 from local, the password is vncdocker which is set in Dockerfile, you'll come to the remote X screen with an xterm open. If you execute env and will find the environment variable DISPLAY=:20

    Let's go to the docker container and try to open another xterm:

    docker exec -it vnc bash
    

    Then execute the following command inside container:

    DISPLAY=:20 xterm
    

    A new xterm window will popup in your vnc client window. I guess that's the way you are going to run your application.

    Note:

    • The base vnc image is based on ubuntu 14, but I guess the package is similar in ubuntu 16
    • Don't expose 5900 if you don't want remote connection

    Hope this can help :-)