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.

  • Cross-device link not permitted Error in Meteor Project with Docker
  • MobileFirst Container CLI Extensions are not installed
  • How to run or use multiple registry mirror?
  • error when install tools in docker container
  • Cannot increase size for docker container - Ubuntu 16.04
  • Migrating machine Certificates generated by docker-machine
  • 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],
    

  • Accessing host usb device in docker for windows
  • Tomcat remote deploy with Intellj IDEA
  • Understanding a docker entrypoint script
  • Issue connecting elasticsearch cluster (i.e. non dockerized) from elasticsearch client running in docker container
  • Number of threads used by Go runtime
  • Failed to connect to containerd
  • 2 Solutions collect form web for “Docker API : Show container logs in a webpage”

    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.

    If you find this post as I did and would like to get a basic, working example for the implementation: https://gist.github.com/m451/1dfd41460b45ea17eb71

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.