在使用DcKePy的clie.Cult.Currror时,无法从C++应用程序中看到容器日志

2024-04-29 20:41:21 发布

您现在位置:Python中文网/ 问答频道 /正文

在使用docker py的python中,我运行两个docker容器:

  1. 莫斯基托月食酒店
  2. ULUTU:基于仿生的图像,我的C++应用程序用CexyPooT([/Directory /MyAPP)]启动:

我使用以下docker py API:

container = client.containers.run(imageName, detach=True, name=containerName, ports = ports, volumes = volumes)
  1. 当它是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. 当它是我的自定义容器时,如果调用container.logs(),我什么也得不到,而且Docker Desktop Dashboard中的日志也是空的:

这似乎有点类似于这个问题https://github.com/docker/docker-py/issues/2697

经过几次测试,这是我的发现:

  1. 如果我从命令行运行自定义容器,Docker Desktop Dashboard的日志是否存在
  2. 如果我从“subprocess.call(docker_cmd)”运行自定义容器,那么docker Desktop Dashboard的日志是否存在
  3. 如果从“client.containers.run”运行自定义容器,则Docker Desktop Dashboard中的日志为空

使用EclipseMosquitto进行的相同测试总是给我一个Docker桌面仪表板,上面满是日志

有人知道如何使用client.containers.run从自定义容器获取日志吗?


Tags: dockerrunpyclientversioncontainerports容器
1条回答
网友
1楼 · 发布于 2024-04-29 20:41:21

经过几天的研究,我能够提供更多的信息来解决这个问题

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内容:

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

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)

总而言之,当从命令行docker run -it test运行自定义容器时,我得到了日志,但当使用dockerClient.containers.run运行docker py时,我的日志保持为空

有人知道如何使用dockerClient.containers.run从自定义容器获取日志吗

相关问题 更多 >