简体   繁体   English

在run -it / bin / bash之外的docker容器中发出运行python脚本的问题

[英]Issue running python scripts in docker container outside of run -it /bin/bash

So I have a weird issue with a python script I'm trying to run... this works: 所以我有一个奇怪的问题,我试图运行的python脚本......这有效:

docker run -it <my image> /bin/bash

<inside container> xvfb-run python myscript.py <arg>

Everything starts up, firefox launches in headless mode, it's all good. 一切都开始了,firefox以无头模式启动,这一切都很好。

This doesn't work: 这不起作用:

docker run -d <my image> /bin/bash -c "xvfb-run python myscript.py <arg>"

docker top shows that the cmd is running, but nothing else happens. docker top显示cmd正在运行,但没有其他反应。

Why would the behavior be different from when I'm running it the second way? 为什么行为会与我第二种方式运行时不同?

So for anyone else who runs into this problem, here's how I ended up solving it: 因此,对于遇到此问题的其他人,这是我最终解决它的方式:

Don't use xvfb-run. 不要使用xvfb-run。 I'm not sure why but docker gets really unhappy. 我不确定为什么,但码头工人真的很不高兴。 You need to have Xvfb already running in the background. 您需要已经在后台运行Xvfb。 But you have to do it a certain way! 但你必须以某种方式做到这一点!

I wasn't able to get Docker working with Xvfb in a RUN command, but it was happy running out of a script. 我无法在RUN命令中让Docker使用Xvfb,但是很快就用完了脚本。 Create a script called start_headless_display.sh and have it look like this: 创建一个名为start_headless_display.sh的脚本,并使其如下所示:

Xvfb :1 -screen 0 1600x1200x16 &
python /myscript.py <args>

In your Dockerfile, make sure the script will be part of your image and add these instructions: 在Dockerfile中,确保脚本成为图像的一部分并添加以下说明:

ENV DISPLAY :1.0

RUN chmod a+x /start_headless_display.sh

CMD /start_headless_display.sh

Now when you run 现在,当您运行

docker run -d <your image>

Everything should work - docker top will show your script and Firefox chugging merrily away. 一切都应该工作 - docker top将显示你的脚本和Firefox快乐地走开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM