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)
              });
        },
      );

关于javascript - 如何使用NodeJS dokerode库获取过程的输出?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57223195/

10-16 16:26