How to view GUI apps from inside a docker containe

2019-05-07 01:19发布

问题:

When I try to run a GUI, like xclock for example I get the error:

Error: Can't open display: 

I'm trying to use Docker to run a ROS container, and I need to see the GUI applications that run inside of it.

I did this once just using a Vagrant VM and was able to use X11 to get it done.

So far I've tried putting way #1 and #2 into a docker file based on the info here: http://wiki.ros.org/docker/Tutorials/GUI

Then I tried copying most of the dockerfile here: https://hub.docker.com/r/mjenz/ros-indigo-gui/~/dockerfile/

Here's my current docker file:

# Set the base image to use to ros:kinetic
FROM ros:kinetic

# Set the file maintainer (your name - the file's author)
MAINTAINER me

# Set ENV for x11 display
ENV DISPLAY $DISPLAY
ENV QT_X11_NO_MITSHM 1

# Install an x11 app like xclock to test this
run apt-get update 
run apt-get install x11-apps --assume-yes

# Stuff I copied to make a ros user
ARG uid=1000
ARG gid=1000

RUN export uid=${uid} gid=${gid} && \
    groupadd -g ${gid} ros && \
    useradd -m -u ${uid} -g ros -s /bin/bash ros && \
    passwd -d ros && \
    usermod -aG sudo ros

USER ros
WORKDIR /home/ros

# Sourcing this before .bashrc runs breaks ROS completions
RUN echo "\nsource /opt/ros/kinetic/setup.bash" >> /home/ros/.bashrc

# Copy entrypoint script into the image, this currently echos hello world
COPY ./docker-entrypoint.sh /
ENTRYPOINT ["/docker-entrypoint.sh"]

回答1:

My personal preference is to inject the display variable and share the unix socket or X windows with something like:

docker run -it --rm -e DISPLAY \
  -v /tmp/.X11-unix:/tmp/.X11-unix \
  -v /etc/localtime:/etc/localtime:ro \
  my-gui-image

Sharing the localtime just allows the timezone to match up as well, I've been using this for email apps.

The other option is to spin up a VNC server, run your app on that server, and then connect to the container with a VNC client. I'm less a fan of that one since you end up with two processes running inside the container making signal handling and logs a challenge. It does have the advantage that the app is better isolated so if hacked, it doesn't have access to your X display.



标签: docker x11 ros