如何使用 NodeJS dokerode 库获取进程的输出?

How to get the output of the process using NodeJS dokerode library?

var Docker = require('dockerode');
var docker = new Docker({socketPath: '/var/run/docker.sock'});
var container = docker.getContainer('740aae30d312');
let params = {
    Cmd: ['sh','-c','ls -a $URL'],
    Env: ['URL=/home'],
    AttachStdout: true,
    AttachStderr: true,
  }
container.exec(params,(err, exec) => {
      err && console.error(err);
      exec.start({ hijack: true, stdin: false }, 
          function(err, stream) {
            docker.modem.demuxStream(stream, process.stdout, process.stderr);
          });
    },
  );

目前,此代码在控制台上显示容器主目录中的文件列表。我需要同样的东西,但在一个变量中

process.stdout 是一个流,您可以直接通过管道传输到它,而不是读取每个块并使用 docker.modem.demuxStream(stream, process.stdout, process.stderr);

stream.pipe(process.stdout);   
 var Docker = require('dockerode');
    var docker = new Docker({socketPath: '/var/run/docker.sock'});
    var container = docker.getContainer('471e865f0a96');
    container_output="";
    let params = {
        Cmd: ['sh','-c','ls -a $URL'],
        Env: ['URL=/home'],
        AttachStdout: true,
        AttachStderr: true,
      }
    container.exec(params,(err, exec) => {
          err && console.error(err);
          exec.start({ hijack: true, stdin: false }, 
              function(err, stream) {
                stream.setEncoding('utf8');
              container_output=(stream.pipe(process.stdout));
              console.log("Directory in container is" ,container_output)
              });
        },
      );