KonradK
KonradK

Reputation: 33

How to properly convert Python docker API's CancellableStream into readable string?

What I got:

Python code that executes commands inside docker container using docker API and then reads output from them:

def foo(some_command_list, workdir):
    output_list = []
    output_byte_str = b''
    for command in some_command_list:
            cmd_output = container.exec_run(command, stream=True, workdir=workdir, tty=True)
            for chunk in cmd_output.output:
               output_byte_str += chunk
            output_list.append(output_byte_str.decode('utf-8'))
    return output_list

the stdout (bool) and stderr (bool) are set by default to true.

The following function returns a list of strings.

My goal:

  1. I believe this code is not done accordingly to best practices and standards of working with docker API. What should I change to make the code more readable and optimal?
  2. Also i see that some command outputs appear multiple times in my output_list even though they were placed only once in the some_command_list, what am I missing?

I would be grateful for any pointers.

Upvotes: 0

Views: 70

Answers (0)

Related Questions