AgustinCB / docker-api

Docker Remote API driver for node.js. It uses the same modem than dockerode, but the interface is promisified and with a fancier syntax.
GNU General Public License v3.0
303 stars 51 forks source link

exec in interactive mode #57

Open JulienB37 opened 5 years ago

JulienB37 commented 5 years ago

Hi,

How can i make an exec command with the interactive mode ?? I'm looking for a way to do docker exec -it test /bin/bash with docker api.

Thanks for helping.

sabrehagen commented 5 years ago

Hi @JulienB37, did you get this working? If so, can you post your code please? Did you stream input to the container's stdin?

JulienB37 commented 5 years ago

hi, i did not find a good solution :(

And finally i use docker with chid_process to execute a docker command in interactive mode

const {execFileSync} = require('child_process')
execFileSync('docker', ['exec', '-it', 'myContainer', '/bin/bash'], {stdio: 'inherit'});

If someone has a better solution it would be nice

sabrehagen commented 5 years ago

I ended up with a hacky solution by accessing the .connection property of the stream returned from exec.start() as seen in the last line below. I assume this is not the intended approach.

  import { tarStream } from 'tar-stream';

  // The outgoing tar compression stream
  const tarCompressor = tarStream();

  const syncContainer = await docker.container.create({
    Binds: volumesToSync,
    Cmd: ['sleep', 'infinity'],
    Image: 'stemn/development-environment:latest',
  });

  await syncContainer.start();

  // Create a tar extractor pointng at the root of the filesystem
  const tarExtractorExec = await syncContainer.exec.create({
    AttachStdin: true,
    Cmd: ['tar', 'xf', '-'],
    OpenStdin: true,
    WorkingDir: '/',
  });

  // The receiving tar extractor stdio stream
  const tarExtractor = await tarExtractorExec.start();

  // Pipe the tar compression stream to the standard input of the tar extractor
  tarCompressor.pipe(tarExtractor.connection);