RDR
RDR

Reputation: 483

Finding docker container ip/port programatically

I need to find out (programmatically) the container ip & ports of a particular app that I have deployed in Docker. The app could be running in multiple nodes and scaled up.

Is there a way using Docker API to find out the container ips and ports?

Upvotes: 1

Views: 909

Answers (2)

Matt
Matt

Reputation: 74620

The Docker API provides HTTP endpoints for all operations so it can be easily managed in most languages.

A simple example is using curl and jq on the command line.

You can list all containers ports

$ curl -s --unix-socket /var/run/docker.sock \
    http:/v1.26/containers/json \
    | jq '.[] | .Id, .Ports'

"b8249afa78bcc8027a38048384c7656a305a3c8d5a517d52df5a299223d8064d"
[
  {
    "IP": "0.0.0.0",
    "PrivatePort": 3142,
    "PublicPort": 3142,
    "Type": "tcp"
  }
]
"49125f8274242a5ae244ffbca121f354c620355186875617d43876bcde619732"
[
  {
    "IP": "0.0.0.0",
    "PrivatePort": 4873,
    "PublicPort": 4873,
    "Type": "tcp"
  }
]

Retrieve the list of the port map definitions for a specific container

$ curl -s --unix-socket /var/run/docker.sock \
    http:/v1.26/containers/49125f8274242a5ae244ffbca121f354c620355186875617d43876bcde619732/json \
    | jq '.NetworkSettings | .Ports | keys'

[
  "4873/tcp"
]

Upvotes: 2

Ricardo Branco
Ricardo Branco

Reputation: 6079

Use the Docker SDK. Just choose the language... ;)

https://docs.docker.com/engine/api/sdks/

Upvotes: 0

Related Questions