Hello.
I want to get an interactive shell from my remote docker container to type commands and get back the responses.
I read the documentation and examples but I couldn't figure out how to do it. Could someone please provide a sample code, for example a code snippet that creates and run an ubuntu container and then connect to this container to execute simple commands.
I think this code sample would be useful for people who are new to docker like me.
Thanks.
This Example Helped https://github.com/apocas/dockerode/blob/master/examples/run_stdin.js
But every letter is written two times in the screen.
for example ls command is converted to llss
This Link helped me to solve the duplicate letter problem.
https://stackoverflow.com/questions/24661774/createinterface-prints-double-in-terminal
I could not solve the duplication of letters.
When I use
readline.createInterface({ input: process.stdin, output: process.stdout, terminal: false })
mentioned in previous stackoverflow link I loose the terminal behavior like using tab and arrow keys.
@ehsanshekari
the following tyepscript code work for me
@apocas it will be usefull to share the following code in your docs or example it more helpfull than attach example
public async interactiveShell(containerName: string) {
const container = this.dockerApi.getContainer(
containerName
);
const exec = await container.exec({
Cmd: ['sh'],
AttachStderr: true,
AttachStdout: true,
AttachStdin: true,
Tty: true,
});
const http = await exec.start({stdin: true});
const stream = http.output;
(<any>process.stdin).setRawMode(true);
stream.pipe(process.stdout);
process.stdin.pipe(stream);
await new Promise(resolve => {
stream.on('end', resolve);
});
(<any>process.stdin).setRawMode(false);
process.stdin.resume();
}
This one seems tackled, going to close it.
Please feel free to submit a merge request with that example :)
Most helpful comment
@ehsanshekari
the following tyepscript code work for me
@apocas it will be usefull to share the following code in your docs or example it more helpfull than attach example