简体   繁体   English

dockerode stream “数据”监听器回显写入数据

[英]dockerode stream “data” listener echoes write data

I'm trying to figure out why dockerode stream directs all data written to the stream directly to the output of the stream. 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.stream object 上调用.write()方法。

        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. 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在this.stream.on("data", ...)监听器上。

For eg if I write ls\n , the data passed into the .on("data",...) looks like例如,如果我写ls\n ,传递给.on("data",...)的数据看起来像

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

How do I prevent it from echoing the " ls " back to me?如何防止它向我回显“ ls ”?

Here is the code for the stream write and reads on the server:这是 stream 在服务器上读写的代码:

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


Okay, my current hack to solve this problem is to only read every other on("data", ...) response.好的,我目前解决这个问题的方法是只读取每隔一个on("data", ...)响应。 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.我注意到数据侦听器被调用了两次,一次用于我编写的命令,下一次用于来自 shell 的实际 output。 So reading every other response prevents this echo.因此,阅读所有其他响应可以防止这种回声。 Still looking for a better solution though.不过仍在寻找更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM