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

342 views Asked by At

In python with docker-py, I am running two docker containers:

  1. eclipse-mosquitto
  2. an ubuntu:bionic based image where my C++ App is launched with ENTRYPOINT ["/directory/myApp"]

I use the following 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:
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:

This seems to be kind of similar to this issue 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
  2. If I run my custom container from '''subprocess.call(docker_cmd)''', logs from Docker Desktop Dashboard are there
  3. If I run my custom container from '''client.containers.run''', logs from Docker Desktop Dashboard are empty

The same tests with eclipse-mosquitto gives me always a Docker Desktop Dashboard full of logs.

Does anybody have an idea how to get my logs from my custom container using client.containers.run ?

1

There are 1 answers

0
Mike On

After several days of research, I am able to provide additional information isolating the issue.

C++ testApp Code Sample:

#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:

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

Python Code

   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.

Does anybody have an idea how to get my logs from my custom container using dockerClient.containers.run ?