What to do if you get a DISPLAY error in gym.render ()

trouble

When I try to run the Open AI Gym sample code in a docker container, I get the following error message:

Terminal


Traceback (most recent call last):
  File "hello_gym.py", line 8, in <module>
    env.render()
  File "/usr/local/lib/python3.6/dist-packages/gym/core.py", line 240, in render
    return self.env.render(mode, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/gym/envs/classic_control/cartpole.py", line 174, in render
    from gym.envs.classic_control import rendering
  File "/usr/local/lib/python3.6/dist-packages/gym/envs/classic_control/rendering.py", line 25, in <module>
    from pyglet.gl import *
  File "/usr/local/lib/python3.6/dist-packages/pyglet/gl/__init__.py", line 244, in <module>
    import pyglet.window
  File "/usr/local/lib/python3.6/dist-packages/pyglet/window/__init__.py", line 1880, in <module>
    gl._create_shadow_window()
  File "/usr/local/lib/python3.6/dist-packages/pyglet/gl/__init__.py", line 220, in _create_shadow_window
    _shadow_window = Window(width=1, height=1, visible=False)
  File "/usr/local/lib/python3.6/dist-packages/pyglet/window/xlib/__init__.py", line 165, in __init__
    super(XlibWindow, self).__init__(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/pyglet/window/__init__.py", line 570, in __init__
    display = pyglet.canvas.get_display()
  File "/usr/local/lib/python3.6/dist-packages/pyglet/canvas/__init__.py", line 94, in get_display
    return Display()
  File "/usr/local/lib/python3.6/dist-packages/pyglet/canvas/xlib.py", line 123, in __init__
    raise NoSuchDisplayException('Cannot connect to "%s"' % name)
pyglet.canvas.xlib.NoSuchDisplayException: Cannot connect to "None"

Execution environment

The execution environment is as follows.

version
OS Ubuntu18.04
python 3.6
gym 0.18.0
pyglet 1.5.0
xvfb 2:1.19.6
x11vnc 0.9.13

I'm trying to set vnc in a Ubintu 18.04 based docker container and see the render () output in a browser.

Thoughts

--VNC is not set --X display is not set --gym sample code error (around render) --The version of pyglet does not match (pyglet was up to date)

Here is the sample code for the gym:

sample.py


import gym
env = gym.make('CartPole-v0')
env.reset()
for _ in range(1000):
    env.render()
    env.step(env.action_space.sample()) # take a random action
env.close()

(Actually, this code causes an error for another reason, but that's another article ...)

Cause

The DISPLAY was not set.

Check the environment variables in the terminal.

Terminal


$ printenv

The list is displayed in a row

Terminal


Abbreviation
LANG=C.UTF-8
OLDPWD=/home/user
VISIBLE=now
USER=user
PWD=/home/user/workspace
HOME=/home/user
Abbreviation

There is no indication of DISPLAY =. I should have set it ...

Setting the DISPLAY environment variable (example of failure)

I tried the following three methods, but they didn't work. If you want to know the solution quickly, Jump

Setting method 1

Originally it was set in the Dockerfile (I thought it was done)

Dockerfile


ENV DISPLAY=:0

But I couldn't set the environment variables in the container.

Setting method 2 Set with the option of docker run

The next method I tried was this method.

Terminal


docker run -e DISPLAY=$DISPLAY Below abbreviation

By the way, even if I changed -e to --env, it didn't work lol

Setting method 3 Set with startup.sh

startup.sh is, roughly speaking, a shell script that can be executed when a container is started. (I just don't understand w)

You can write the CMD command at the end of the Dockerfile, but if you want to execute multiple commands, Execute them by writing them in startup.sh.

Dockerfile


CMD ["/startup.sh"]

The reason I used startup.sh is that I wantedssh and vnc to be started by supervisord, so /usr/bin/supervisord -c /etc/supervisor/supervisord.conf is at least I had to run it in bash. Therefore, I wrote the command on two lines.

startup.sh


#!/bin/bash
/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
export DISPLAY=:0

After all, this method did not resolve the error.

solution

Describe as follows in the Dockerfile.

RUN echo "export DISPLAY=:0"  >> /etc/profile

You have now set the environment variable inside the container to DISPLAY.

Summary

If you get the error pyglet.canvas.xlib.NoSuchDisplayException: Cannot connect to" None " when you run gym inside the docker container, The DISPLAY environment variable may not be set.

I was able to set it by executing RUN echo" export DISPLAY =: 0 ">>/etc/profile in the Dockerfile.

Recommended Posts

What to do if you get a DISPLAY error in gym.render ()
What to do if you get a wrong number of arguments error in binding.pry
What to do if you get a groovy warning in Thymeleaf Layout
What to do if you get a "302" error in your controller unit test code in Rails
What to do if you get a SQLite3 :: BusyException: database is locked error
What to do if you get a "Mysql2 :: Error: Operand should contain 1 column (s)" error in Rails
What to do if you get a JNI shared library error when trying to build in Eclipse
What to do if you get a port error when docker-compose up on Mac
What to do if you get a "Cannot Pull Container Error" when starting ECS ​​Fargate
What to do if you get an error in Basic authentication during Rails test code
What to do if you get an error during rails db: reset
What to do if you get an uninitialized constant Likes Controller error
What to do if you get an error when you hit Heroku logs
What to do if you get a MiniMagick vulnerability alert on GitHub
What to do if you accidentally create a model
What to do if you get an [An HTTP request took too long to complete.] Error in Docker.
What to do if you get the error message unrecognized selector send to instance "***"
What to do if you get an error on heroku rake db: migrate
What to do if you get an error with bundle install by entering gem'bcrypt' in your Gemfile
What to do if mysql2 gets a bundle install error
What to do if you can't get the text of an element in Selenium
What to do if you get a NoClassDefFoundError when trying to run eclipse on Java9
What to do if you get the error Couldn't find Item without an ID
What to do if you install Ubuntu
What to do if you get an "A server is already running." Error when you try to start the rails server
What to do if you get a javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake in the IBM JDK
What to do if you get Could not save master table to file after importing a project in Eclipse
What to do if you get angry with OpenSSL with pyenv install
What to do if you forget the root password in CentOS7
[Rails] What to do if you can't get parameters with form_with
[Rails] What to do if you can't get an error message with the errors method
[Java] What to do if you get an error in Eclipse saying "Not allowed at source level below 1.X"
What to do if you get an Argument Error: wrong number of arguments (given 2, expected 0) in your RSpec test
What to do if you get an error saying "Please enter a valid value" when getting with Rails datetime_field
What to do if you get an error saying "Could not find a JavaScript runtime." When starting rails server
What to do if you push incorrect information
How to make a jar with old Hadoop (hadoop-core-0.20.2-cdh3u6) in Gradle: (What to do if you get Could not expand ZIP ..)
What to do if you select a JRE in Eclipse and get "The selected JRE does not support the current compliance level 11"
What to do if you get Could not locate Gemfile or .bundle / directory
What to do when a null byte error occurs
What to do if an ActionController :: UnknownFormat error occurs
What to do if the image posted by refile disappears after setting a 404 error page in Rails
What to do if you get angry if you don't have nokogiri while installing wp2txt
What to do if you get To install the missing version, run `gem install bundler: 2.1.4`
What to do if password authentication fails in Docker/Postgres
What to do if Operation not permitted is displayed when you execute a command in the terminal
What to do if you get the error Too long with no output (exceeded 10m0s) on CircleCI
How to translate the error message into Japanese (What to do if you cannot log in for some reason)
# What to do if you accidentally do rails db: migrate: drop
What to do if you can't use the rails command
What to do if you get an error saying Cannot find a valid baseurl for repo when you use yum -y install docker in a udemy course using CentOS7
[Rails] What to do if you get an error saying "Could not find a JavaScript runtime." When executing the rails s command on Catalina
[Solution] What to do if you get a Docker error'ERROR: Cannot connect to the Docker daemon at unix: ///var/run/docker.sock. Is the docker daemon running?'
What to do if you get the warning "Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1." in Rails 6.0
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
What to do if a SAX Parser error occurs when using Liferay 7 / DXP on AWS
What you need to do to open a file from the menu in the document-Based App macOS app
[Rails] What to do if data is not registered in DB
What to do if Cloud9 is full in the Rails tutorial
What to do when you become a Problem During Content Assist