简体   繁体   English

Python docker 容器使用 print()

[英]Python docker container use print()

Hello I would like to use您好,我想使用

print('Hello world')

to see what's going on with some variables, however im using Docker which means by default it's not printing anything.看看一些变量发生了什么,但是我使用 Docker,这意味着默认情况下它不打印任何东西。 Can someone tell me how I can pass these print commands to the docker container ?有人能告诉我如何将这些打印命令传递给 docker 容器吗?

This is because Python buffers its output by default.这是因为 Python 默认缓冲其输出。

An easy way to change this behavior is to use the PYTHONUNBUFFERED=1 environment variable : docker run -e PYTHONUNBUFFERED=1 <your_image>更改此行为的一种简单方法是使用PYTHONUNBUFFERED=1环境变量docker run -e PYTHONUNBUFFERED=1 <your_image>

An other way is to call the python command with the -u option .另一种方法是使用-u选项调用 python 命令。

Maybe you should run your container with the privileged option to share /dev (therefore, sharing /dev/stdout).也许您应该使用特权选项来运行容器以共享 /dev(因此,共享 /dev/stdout)。

This is working for me:这对我有用:

print.py:打印.py:

print("Hello world")

Dockerfile: Dockerfile:

FROM python
ADD print.py /
ENTRYPOINT ["python",  "/print.py"]

Commands:命令:

$ docker build -t python-test .
$ docker run -it --privileged python-test
Hello world

Hope it helps.希望能帮助到你。

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

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