Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

followProgress doesn't work #560

Closed
twodayslate opened this issue Mar 13, 2020 · 2 comments
Closed

followProgress doesn't work #560

twodayslate opened this issue Mar 13, 2020 · 2 comments
Labels

Comments

@twodayslate
Copy link

twodayslate commented Mar 13, 2020

undefined:1

^

SyntaxError: Unexpected token  in JSON at position 0
    at JSON.parse (<anonymous>)
    at processLine (/Users/twodayslate/Development/swiftpackageregistry/node_modules/dockerode/node_modules/docker-modem/lib/modem.js:401:24)
    at pump (/Users/twodayslate/Development/swiftpackageregistry/node_modules/dockerode/node_modules/docker-modem/lib/modem.js:393:9)
    at IncomingMessage.onStreamEvent (/Users/twodayslate/Development/swiftpackageregistry/node_modules/dockerode/node_modules/docker-modem/lib/modem.js:384:5)
    at IncomingMessage.emit (events.js:203:13)
    at addChunk (_stream_readable.js:295:12)
    at readableAddChunk (_stream_readable.js:276:11)
    at IncomingMessage.Readable.push (_stream_readable.js:210:10)
    at HTTPParser.parserOnBody (_http_common.js:129:22)
    at Socket.socketOnData (_http_client.js:449:22)

This seems like a docker-modem issue but I'm posting so others can get something working

var Docker = require('dockerode');
var docker = new Docker({socketPath: '/var/run/docker.sock'});
var fs = require('fs');
const process = require('process');


function runCommand(container, cmd, callback) {
    container.exec({Cmd: cmd, AttachStdin: true, AttachStdout: true}, function(err, exec) {
      if(err) { console.log(err); return callback(err);}
      exec.start({}, function(err, stream) {
        var nextDataType = null;
        var nextDataLength = null;
        var buffer = Buffer.from('');
        function processData(data) {
          if (data) {
            buffer = Buffer.concat([buffer, data]);
          }
          if (!nextDataType) {
            if (buffer.length >= 8) {
              var header = bufferSlice(8);
              nextDataType = header.readUInt8(0);
              nextDataLength = header.readUInt32BE(4);
              // It's possible we got a "data" that contains multiple messages
              // Process the next one
              processData();
            }
          } else {
            if (buffer.length >= nextDataLength) {
              var content = bufferSlice(nextDataLength);
              if (nextDataType === 1) {
                process.stdout.write(content);
              } else {
                process.stderr.write(content);
              }
              nextDataType = null;
              // It's possible we got a "data" that contains multiple messages
              // Process the next one
              processData();
            }
          }
        }

        function bufferSlice(end) {
          var out = buffer.slice(0, end);
          buffer = Buffer.from(buffer.slice(end, buffer.length));
          return out;
        }
        function didClose() {
          callback();
        }
        stream.on('data', processData);
        stream.on('close', didClose);
      });
    });
}

docker.createContainer({Image: 'swift:5.1.4', Cmd: ['/bin/bash'], AutoRemove: true, Tty: true,}, function (err, container) {
  container.start(function (err) {
    console.log(err);
    runCommand(container, ['swift', '--version'], function() {
      runCommand(container, ['git', '--version'], function() {
        container.stop(function (err, data) {
          container.remove(function (err, data) {
            console.log("done :)")
          });
        });
      });
    });
  });
});
@stale
Copy link

stale bot commented Apr 25, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Open a new issue if needed.

@stale
Copy link

stale bot commented Oct 28, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Open a new issue if needed.

@stale stale bot added the stale label Oct 28, 2022
@stale stale bot closed this as completed Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants