Dockerode: How can I efficiently get the ID of a container by its name?

Created on 10 Sep 2016  路  6Comments  路  Source: apocas/dockerode

At present, I list all containers first and then filter out the one with the right name.

All 6 comments

Got it. Sorry for the noise.

Would have been nice to see the solution.. ;) Just for reference this was my solution:

getContainerByName(name) {
    // filter by name
    var opts = {
      "limit": 1,
      "filters": `{"name": ["${name}"]}`
    }

    return new Promise((resolve, reject)=>{
      this.dockerode.listContainers(opts, function(err, containers) {
        if(err) {
          reject(err)
        } else{
          resolve(containers && containers[0])
        }
      });
    })
  }

The passing the name value as an array tripped me up, so don't forget those.

@knight42 So, would you like to share your solution? I can still not find any hints in documents

@davidkhala Simply docker.getContainer(containerName) should work.

@knight42 Thanks, and I found that getContainer return only a memory-object without checking its existence. I use Container.inspect() to do that and test OK. Or any other recommend?

@knight42 does it works the same way with getService?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

controversial picture controversial  路  3Comments

zigmund picture zigmund  路  3Comments

mhemrg picture mhemrg  路  4Comments

peterpetre picture peterpetre  路  6Comments

AlexanderCollins picture AlexanderCollins  路  5Comments