dockerode: exec Stream Doesn't Emit End Event

I want to execute a command in an already running container using “exec”. For some reason, the stream returned by “exec.start()” is not emitting the “end” event. So, I have no way of knowing when the command completes and the Node process doesn’t end.

Example code is below. Am I doing something wrong, or is this a bug?


const Docker = require("dockerode");
const docker = new Docker();
const PassThrough = require("stream").PassThrough;

(async () => {

  try {

    // Create/start container, if necessary.
    let container = docker.getContainer("test");
    let info;
    try {

      info = await container.inspect();

    }
    catch (error) {

      if (error.statusCode !== 404)
        throw error;
      container = await docker.createContainer({
        Image: "ubuntu",
        Tty: true,
        name: "test"
      });

    }
    info = await container.inspect();
    if (info.State.Running !== true)
      await container.start();

    // Execute 'cat' with data piped to stdin.
    const exec = await container.exec({
      AttachStdin: true,
      AttachStdout: true,
      AttachStderr: true,
      Tty: false,
      Cmd: ["cat"]
    });
    const stream = await exec.start({
      hijack: true,
      stdin: true
    });
    const input = new PassThrough();
    input.write("test one\ntest two\n");
    input.pipe(stream.output);
    docker.modem.demuxStream(stream.output, process.stdout, process.stderr);

    stream.output.on("end", async () => { // Event is not emitted.

      console.log(await exec.inspect());

    });

  }
  catch (error) {

    console.error(error);

  }

})();

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 9
  • Comments: 15

Commits related to this issue

Most upvoted comments

I never found a ‘real’ solution. I worked around by polling for completion by calling exec.inspect() via setInterval() and checking the ‘Running’ flag.

docker desktop 4.12 has a compatibility shim that should fix this. https://docs.docker.com/desktop/release-notes/#bug-fixes-and-minor-changes

I can confirm that this bug exists in Windows - or at least it did when I first commented about it here. Haven’t tested in macOS.

On Tue, Oct 20, 2020, 17:16 Phillip Hoff notifications@github.com wrote:

I’m seeing the same thing on Windows but not on Mac, where I do get the end event. Which platforms are you all seeing this issue?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/apocas/dockerode/issues/534#issuecomment-712967046, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADXDBGZCVG76IVXFKJ5CENDSLWZVTANCNFSM4IVWDJ2Q .

filed a more low-level repro case here: https://github.com/microsoft/go-winio/issues/257