Search code examples
dockerxvfbxdotool

RUN command seems not to work in Docker


My intention is to run a GUI jar file in Docker so I could automate commands with xdotool and may view it by x11vnc.

This is my Dockerfile:

# WEB 0.1

FROM ubuntu:14.04
RUN apt-get update \
 && apt-get install -y \
  default-jre \
  x11vnc \
  xdotool \
  xsel \
  xvfb \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN DISPLAY=:1.0 \
 && export DISPLAY \
 && mkdir /root/.vnc \
 && x11vnc -storepasswd 1234 /root/.vnc/passwd \
 && Xvfb :1 -screen 0 493x476x8 & \
 x11vnc -display :1.0 -usepw -forever &

ENTRYPOINT ["java"]
CMD ["-jar", "/var/bin/program.jar"]

I run it with:

docker run \
  --name program-jar \
  -p 5090:5900 \
  -v /var/bin/program-jar/:/var/bin/ \
  -d program-jar:0.1

But inside this container it is not defined $DISPLAY and is not running x11vnc and Xvfb

root@62febbc0b8f9:/# echo $DISPLAY

root@62febbc0b8f9:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1 11.7  0.9 4226956 98588 ?       Ssl  14:30   0:01 java -jar /var/bin/program.jar
root        26  0.2  0.0  18188  3268 ?        Ss   14:30   0:00 /bin/bash
root        41  0.0  0.0  15580  2044 ?        R+   14:30   0:00 ps aux
root@62febbc0b8f9:/# 

(If I run those commands in RUN inside bash it work... don't know why RUN seems not to work when run the docker build)


Solution

  • docker uses a layer file system when you RUN it create a separate layer for the installation it is NOT use to run a program but it is use to download source code or build from source code etc. for example RUN mvn package

    The way you should do this is create a shell script commonly they call it bootstrap.sh you copy that into your container COPY bootstrap.sh /app or something like that you can then put in this command

    #!/bin/bash
    
    DISPLAY=:1.0 \
     && export DISPLAY \
     && mkdir /root/.vnc \
     && x11vnc -storepasswd 1234 /root/.vnc/passwd \
     && Xvfb :1 -screen 0 493x476x8 & \
     x11vnc -display :1.0 -usepw -forever &
    
    java -jar /var/bin/program.jar
    

    into your shell script and the last command in your dockerfile change it to CMD ./bootstrap.sh something like that