简体   繁体   中英

Docker API : Show container logs in a webpage

I'm working on a project for a docker web client in node.js using the docker.io package from Appersonlabs https://github.com/appersonlabs/docker.io . In order to build a complete client, I want to stream the container logs to a webpage. Here's my route using the api :

app.get('/containers/:id',function(req,res){
    console.log('INSPECT CONTAINER WITH ID '+req.params.id);
    docker.containers.inspect(req.params.id,function(err,infos){
      docker.containers.attach(req.params.id, {stream: true, stdout: true, stderr:false, tty:false}, function(err,stream) {
        console.log(stream);
        res.render('containers/show.ejs',{container: infos, name: name, stream: stream});
      });
    });
  });

But when I console.log the stream I got a big JSON files with a bunch of output information.

According to docker's API here's how I should proceed ( http://docs.docker.io/en/latest/reference/api/docker_remote_api_v1.10/#attach-to-a-container )

IMPLEMENTATION

The simplest way to implement the Attach protocol is the following:

Read 8 bytes

  • chose stdout or stderr depending on the first byte
  • Extract the frame size from the last 4 bytes
  • Read the extracted size and output it on the correct output
  • Goto first step

Question :

How should I proceed to achieve that ? I really don't know how to start.

NOTE : As far as I know my http request is correct, here's a sample of the JSON response I get from the API:

method: 'POST',
    path: 'http://localhost:4243/v1.7/containers/8196569ecaaf2bbcf726189b60212676ad1351f6ff4df6ebe9deb4743b52e138/attach?stream=true&stdout=true&stderr=false&tty=false',
    _headers: [Object],

I think you need to add "logs: true" in attach options :

docker.containers.attach(containerId, {logs: true, stream: true, stdout: true, stderr: false, tty: false}, function(err, stream)

I just tested it, it works fine on docker 0.9.1.

如果您像我一样找到这篇文章,并希望获得实现的基本示例: https : //gist.github.com/m451/1dfd41460b45ea17eb71

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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