简体   繁体   English

使用 docker-py 的 client.containers.run 时,无法从我的 C++ 应用程序中查看容器日志

[英]Unable to see container logs from my C++ App when using client.containers.run of docker-py

In python with docker-py, I am running two docker containers:在带有 docker-py 的 python 中,我正在运行两个 docker 容器:

  1. eclipse-mosquitto日食蚊子
  2. an ubuntu:bionic based image where my C++ App is launched with ENTRYPOINT ["/directory/myApp"]一个 ubuntu:基于仿生的图像,其中我的 C++ 应用程序使用 ENTRYPOINT ["/directory/myApp"] 启动

I use the following docker-py API:我使用以下 docker-py API:

container = client.containers.run(imageName, detach=True, name=containerName, ports = ports, volumes = volumes)
  1. When it is the eclipse-mosquitto container, if I call container.logs() I got the following logs:当它是eclipse-mosquitto容器时,如果我调用 container.logs() 我会得到以下日志:
1606303570: mosquitto version 1.6.12 starting
1606303570: Config loaded from /mosquitto/config/mosquitto.conf.
1606303570: Opening ipv4 listen socket on port 1883.
1606303570: Opening ipv6 listen socket on port 1883.
1606303570: mosquitto version 1.6.12 running
  1. When it is my custom container, if I call container.logs() I got nothing and furthermore logs from Docker Desktop Dashboard is empty too:当它是我的自定义容器时,如果我调用 container.logs() 我什么也得不到,而且来自 Docker 桌面仪表板的日志也是空的:

This seems to be kind of similar to this issue https://github.com/docker/docker-py/issues/2697这似乎有点类似于这个问题https://github.com/docker/docker-py/issues/2697

After several tests this is my findings:经过几次测试,这是我的发现:

  1. If I run my custom container from command line, logs from Docker Desktop Dashboard are there如果我从命令行运行我的自定义容器,来自 Docker 桌面仪表板的日志就在那里
  2. If I run my custom container from '''subprocess.call(docker_cmd)''', logs from Docker Desktop Dashboard are there如果我从 '''subprocess.call(docker_cmd)''' 运行我的自定义容器,来自 Docker 桌面仪表板的日志就在那里
  3. If I run my custom container from '''client.containers.run''', logs from Docker Desktop Dashboard are empty如果我从 '''client.containers.run''' 运行自定义容器,来自 Docker 桌面仪表板的日志为空

The same tests with eclipse-mosquitto gives me always a Docker Desktop Dashboard full of logs.eclipse-mosquitto相同的测试总是给我一个充满日志的 Docker 桌面仪表板。

Does anybody have an idea how to get my logs from my custom container using client.containers.run ?有人知道如何使用client.containers.run从我的自定义容器中获取我的日志吗?

After several days of research, I am able to provide additional information isolating the issue.经过几天的研究,我能够提供额外的信息来隔离这个问题。

C++ testApp Code Sample: C++ testApp 代码示例:

#include <iostream>
#include <unistd.h>

int main(int argc, char **argv)
{
    printf("\nServer is starting... !!\n\n");
    while (1)
        usleep(100000);
    return 1;
}

DockerFile content: DockerFile 内容:

FROM amd64/ubuntu:latest
ADD / test
WORKDIR /test
RUN chmod +x testApp
ENTRYPOINT ["/test/testApp"]

Python Code Python代码

   import docker, time

    imageName = 'test'
    imagePath = 'C:/test/image'
    dockerClient = docker.from_env()

    try:
        dockerClient.images.remove(image = imageName, force = True)
    except (docker.errors.ImageNotFound):
        pass
    
    dockerClient.images.build(path = imagePath, tag = imageName, rm=True)
    container = dockerClient.containers.run(imageName, detach=True, name=imageName)

    while True:
        print(container.logs())
        time.sleep(1)

To summarize, when running my custom container from command line docker run -it test , I have got the logs but when running it with docker-py with dockerClient.containers.run , My logs stays empty.总而言之,当从命令行docker run -it test运行我的自定义容器时,我得到了日志,但是当使用 docker -pydockerClient.containers.run运行它时,我的日志保持为空。

Does anybody have an idea how to get my logs from my custom container using dockerClient.containers.run ?有人知道如何使用dockerClient.containers.run从我的自定义容器中获取我的日志吗?

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

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