1

I'm trying to figure out why dockerode stream directs all data written to the stream directly to the output of the stream.

This is how I create the container, I call the .write() method on the this.stream object.

        this.container = await this.dockerConnection.createContainer({
            Image: this.image,
            Tty: true,
            Cmd: cmd,
            HostConfig: {
                NetworkMode: "none",
                Binds: [this.mountPath + ":/home/appuser/workdir"],
            },
            OpenStdin: true,
            StdinOnce: false,
            AutoRemove: true,
        });

        this.stream = await this.container.attach({
            stream: true,
            stdin: true,
            stdout: true,
            stderr: true,
        });

        await this.container.start();

However after connecting this stream to a websocket which is connected to xterm.js, I noticed if I launch a shell like /bin/sh through the cmd argument and write to the resulting this.stream, the commands I run are echoed back to me on the this.stream.on("data", ...) listener.

For eg if I write ls\n, the data passed into the .on("data",...) looks like

ls
bin/ etc/ lib/ ... (etc.)

How do I prevent it from echoing the "ls" back to me?

Here is the code for the stream write and reads on the server:

    let sb = new Sandbox("alpine-sandbox");

    let stream = await sb.launchSHShell();

    stream.on("data", (data) => {
        socket.emit("response", data.toString());
    })

    socket.on("command", (cmd) => {
        stream.write(cmd);
    })


David Yue
  • 713
  • 7
  • 24
  • The echo is a feature of the underlying TTY to give ppl feedback of the typed input. It is controlled by the TERMIOS settings and normally not controlled by the terminal side of a TTY, but the application (here: the shell where you typed [`l`, `s`]). Long story short - it is expected to echo things back under normal circumstances - unless the application awaits sensitive data like password input and thus switched echo off on purpose. – jerch Apr 21 '21 at 17:07

1 Answers1

0

Okay, my current hack to solve this problem is to only read every other on("data", ...) response. I noticed that the data listener is called twice, once for the command I wrote and the next for the actual output from the shell. So reading every other response prevents this echo. Still looking for a better solution though.

David Yue
  • 713
  • 7
  • 24