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.

  • How to install docker in Jenkins without root privileges?
  • Why my docker container apache service will be shut down everytime I attach my container and then exit?
  • consult a suggestion about modify docker daemon labels
  • Ubuntu based docker-machine image
  • Running a docker-compose “Getting Started” example causes “Invalid volume specification” on Windows
  • How to access Docker container from another machine on lan
  • 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.

  • Find container size using python sdk
  • Change Container Assigned IP address
  • Creating Docker container for mapped drive
  • How to run webdriver tests on docker firefox-standalone-debug container instead of regular firefox browser
  • Assess is denied while docker save
  • How Can I Run A Command That Does Not Exit Upon Failure Until It Succeeds?
  • One Solution collect form web for “Add xserver into Docker container (the host is headless)”

    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).


    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.


    • 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 🙂

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.