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

stream pipe problem? #237

Closed
kangjianfeng opened this issue Apr 26, 2016 · 7 comments
Closed

stream pipe problem? #237

kangjianfeng opened this issue Apr 26, 2016 · 7 comments
Labels

Comments

@kangjianfeng
Copy link

i use restify http server ,code below:

server.get('/api/container/:id/logs', function create(req, res, next) {
var opts={
follow: true,
stdout: true,
stderr: true
};
var c=docker.getContainer(req.params.id);
c.logs(opts,function(err,stream){
if(!err){
stream.pipe(res); //but nothing response?
}
});
});

@amberovsky
Copy link

I got same issue. No output from a running container with 'follow: true'.
Without following or on stopped container it works fine.
docker v1.11.0

@amberovsky
Copy link

Possible solution

container.logs({ follow: true, stdout: true, stderr: true }, function (error, dockerStream) {
    if (error === null) {
       const StringDecoder = require('string_decoder').StringDecoder;
       const decoder = new StringDecoder('utf8');

        dockerStream.on('data', function (chunk) {
            console.log(decoder.write(chunk));
        });
    }
})

@CollinEstes
Copy link
Contributor

I'm working with the .logs API right now as well, and I am able to get data out of the stream but it is never sends an "end" event, anybody have any insight as to why that would be case. my code looks like this:

(I'm using server sent events to write output to the client that is the parts that you can see in this code)

router.get('/:id/logs',
  setHeadersForEventStream,
  function (req, res, next) {

    function ping () {
      sendDataPacket(res, 'ping', 'ping');
    }

    let pingLogs = setInterval(ping, 1000);

    getLogsForContainer(req.params.id, function (err, stream) {
      if (err) { return next(err); }

      stream.on('data', (data) => {
        console.log('in data:  ', data.toString());
        sendDataPacket(res, 'ok', data.toString());
      });

      stream.on('error', (data) => {
        console.log('in error:  ', data.toString());
        sendDataPacket(res, 'error', data.toString());
      });

      stream.on('end', (code) => {
        clearInterval(pingLogs);
        sendDataPacket(res, 'exit', code.toString());
      });

    })
  });

I get data out of the .logs stream but like I said it never hits stream.end

@CollinEstes
Copy link
Contributor

So my issue here was I had {follow: true} on my log options. That causes end to never be called. Once I removed that I was able to have my .logs working without any issue.

@crestenstclair
Copy link

Working with the logging API as well, getting the following error:

/Users/cresten/workspace/albert-io-cli/node_modules/docker-modem/lib/modem.js:265
  stream.on('readable', function() {
         ^

TypeError: stream.on is not a function
    at Modem.demuxStream (/Users/cresten/workspace/albert-io-cli/node_modules/docker-modem/lib/modem.js:265:10)
    at Object.container.logs [as callback] (/Users/cresten/workspace/albert-io-cli/dockerUtils.js:102:21)
    at /Users/cresten/workspace/albert-io-cli/node_modules/dockerode/lib/container.js:617:10
    at Modem.buildPayload (/Users/cresten/workspace/albert-io-cli/node_modules/docker-modem/lib/modem.js:241:7)
    at ClientRequest.<anonymous> (/Users/cresten/workspace/albert-io-cli/node_modules/docker-modem/lib/modem.js:188:12)
    at emitOne (events.js:101:20)
    at ClientRequest.emit (events.js:188:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:469:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:105:23)
    at Socket.socketOnData (_http_client.js:359:20)

@dayaftereh
Copy link

Have the same Problem. It looks that the response stream object is type of string.

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

const container = docker.getContainer('123456');
container.logs({   
    stdout: true,
    stderr: true
  }, function(err, stream){
    if(err) {
      return logger.error(err.message);
    }
    console.log(typeof stream); // string
})

@apocas
Copy link
Owner

apocas commented Jul 23, 2020

Old issue, if needed it will be reopened.

@apocas apocas closed this as completed Jul 23, 2020
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

6 participants