0

I hope someone will be able to help me. I'm using Dockerode to make actions on my Docker containers via JavaScript.

I have some hard times to get the name of a single container. The request is "get the name of the container who has the id XXX" but I can't make it works.

To do that I used listContainers and I tried to play with the options:

docker.listContainers({id: idContainer}, Meteor.bindEnvironment(function(err, containers) {
  if (containers != null) {
      containers.forEach(Meteor.bindEnvironment(function(containerInfo) {
          if(containerInfo.Names[0].startsWith("/")){
             containerInfo.Names[0] = containerInfo.Names[0].substr(1);
             console.log( containerInfo.Names[0]);
          }
      }));
  }
}));

where idContainer is the id of a known container

But the output contains the name of ALL containers.

I really hope someone can help me

1 Answers1

0

Looking at Docker API docs, my take is that your call parameters are malformed maybe.

Instead of:

docker.listContainers({ id: idContainer }, function (...) {...});

You need to specify:

docker.listContainers({ filters: { id: idContainer } }, function (...) {...});

In the docs, the id parameters is inside the filter parameter group.

elpddev
  • 4,314
  • 4
  • 26
  • 47
  • it's strange it's still returns me all the containers, you have an idea why ? –  Apr 21 '17 at 09:57
  • I have not dealt with docker api. To investigate you could look at the req structure going out and compare it to a test you can do doing the same command via curl or postman directly to the docker api daemon. – elpddev Apr 21 '17 at 10:01
  • Thank you for the advice, I mark your answer as the right one because it's right (yea it makes no sense) thank you –  Apr 21 '17 at 10:38
  • 1
    It peaked my curiosity, so I've tried curl with the help of: http://stackoverflow.com/questions/39976683/docker-api-can-t-apply-json-filters and https://nathanleclaire.com/blog/2015/11/12/using-curl-and-the-unix-socket-to-talk-to-the-docker-api/. What I found is that the command does not work in direct api when doing `filters: { id: someid }` but `fitlers: { id: [someid] }`. It needs the id as array for some reason. For ex: `curl -g --unix-socket /var/run/docker.sock http:/containers/json'?filters=%7B%22id%22%3A%5B%2294115da2a95d%22%5D%7D'` – elpddev Apr 21 '17 at 11:24
  • thank you for the help, and yeah I had founded this Github issue https://github.com/docker/compose/issues/2515 where the guy had to add [brackets]. But for me it's still not work, did it worked for you ? –  Apr 24 '17 at 04:55
  • Last comment: Hi, finally it's ok I can use it thank you ! –  Apr 24 '17 at 07:12