简体   繁体   English

docker-py:访问在ENTRYPOINT命令中运行的python脚本输出

[英]docker-py: Accessing output of python script run in ENTRYPOINT command

I'm having trouble getting the output of a Python script I'm running in a docker container using the docker-py module in Python. 我无法使用Python中的docker-py模块获取我在Docker容器中运行的Python脚本的输出。

First, some context: 首先,一些背景:

I've created a Dockerfile and built my image (with id 84730be6107f) in the normal way via the command line ( docker build -t myimage /path/to/dockerfile ). 我已经创建了一个Dockerfile并通过命令行( docker build -t myimage /path/to/dockerfile )以正常方式构建了我的映像(id docker build -t myimage /path/to/dockerfile )。 The Python script is executed as the ENTRYPOINT command in the Dockerfile: Python脚本作为ENTRYPOINT中的ENTRYPOINT命令执行:

ENTRYPOINT ["python", "/cont_dirpath/script.py"]

The directory containing the script is added (bound) to the container upon running it. 包含脚本的目录在运行时添加(绑定)到容器。

When I do this via the usual docker command line (in Ubuntu) using: 当我通过通常的docker命令行(在Ubuntu中)执行此操作时使用:

docker run -v /host_dirpath:/cont_dirpath 84730be6107f

I get the expected Python script output printed to stdout (displayed in the terminal - the script ends with a print result command). 我将预期的Python脚本输出打印到stdout(显示在终端中 - 脚本以print result命令结束)。 This is exactly the output I'm trying to get while doing this from within Python. 这正是我在Python中尝试获得的输出。

However, when trying to do this from within Python using the docker-py package, I am unable to view the results - the logs method returns an empty string. 但是,当尝试使用docker-py包在Python中执行此操作时,我无法查看结果 - logs方法返回一个空字符串。 Here's the Python code I'm using: 这是我正在使用的Python代码:

from docker import Client

docker = Client(base_url='unix://var/run/docker.sock',
                  version='1.10',
                  timeout=10)

contid = docker.create_container('84730be6107f', volumes={"/cont_dirpath":""})
docker.start(contid, binds={"/host_dirpath": {"bind": "/cont_dirpath"} })

print "Docker logs: \n" + str(docker.logs(contid))

I just get an empty string. 我只是得到一个空字符串。 I've tried piping the output of the script to echo and cat in the ENTRYPOINT command but to no avail. 我已经尝试在ENTRYPOINT命令中管道脚本的输出到echocat ,但无济于事。 How can I get the output of script.py run in the ENTRYPOINT command to be part of the log for that container so I can read it using the docker-py Python methods? 如何在ENTRYPOINT命令中运行script.py的输出作为该容器日志的一部分,以便我可以使用docker-py Python方法读取它? I've also tried the docker-py attach method (I think logs is just a wrapped version of this) with the same result. 我也尝试过docker-py attach方法(我认为logs只是一个包装版本),结果相同。

I've considered just writing the script output to a file instead of stdout, but it isn't clear how I would access and read that file using the docker-py methods. 我已经考虑过将脚本输出写入文件而不是stdout,但是我不清楚如何使用docker-py方法访问和读取该文件。

Any insights or suggestions would be greatly appreciated! 任何见解或建议将不胜感激!

What happens when you try to view the logs with kubectl ? 当您尝试使用kubectl查看日志时会发生什么?

kubectl logs $CONTAINERID

I'm guessing you'll see nothing. 我猜你什么也看不见。

What is your workload inside the container? 容器内的工作量是多少?

Python does something weird with buffers when run headless if your code looks like this: 如果您的代码如下所示,Python在无头运行时会对缓冲区执行奇怪的操作:

import time
while True:
   print('something')
   time.sleep(1)

To make it work, you need to manually flush the looped print statements: 要使其工作,您需要手动刷新循环打印语句:

import time
import sys
while True:
   print('something')
   sys.stdout.flush()
   time.sleep(1)

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

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