0

From the host environment, is there a way to get the container and its properties, at least to get its IP(s) addresses, by querying using its alias?

For clarity, I'm referring to the actual alias you can give to containers on specific networks, see here

Currently my solution so far is iterating over all containers through the rest API and see if the alias matches what I'm looking for. It's obvious this is not ideal as this does not scale well in the case there are many containers on the host.

Ideally I'd want to just send a DNS request to docker's embedded dns server, but this does not seem possible. See this question/answer for more info

Does anyone know of a better solution?

/update

since an alias is tied to a network, it's expected that the to-be queried alias is accompanied with its network name, e.g., 'container-alias.network-name'. So we'd have to enumerate over the containers in a single network, but even this does not scale well if there are many containers in a single network.

Mind you, that getting all containers filtered by network name is possible in the docker api, allowing for a single REST call to the api. However, even though the 'Alias' key is in the REST result, it never holds any values. (this might be a bug, I'm not sure.) You have to GET for a specific container, i.e., GET /v1.24/containers/container-uuid/json before you can see its aliases.

hbogert
  • 4,198
  • 5
  • 24
  • 38

3 Answers3

1

There's almost certainly a neater way to do this, maybe even entirely using docker inspect, but if you have jq available you can try something like:

docker inspect -f '{{json .NetworkSettings.Networks}}' <container name/ID> | \
jq '.[] | select(.Aliases[] | contains("<alias on network>")) | .IPAddress'
Sidepipe
  • 89
  • 1
  • 2
0

Remember that most of these details aren't useful outside of Docker space; except on one very specific configuration (on the same native-Linux host as the container) you can't directly access the container-specific IP addresses.

If you can run docker commands you can start containers, so you can get indirect access to the Docker DNS system. For example, let's start a container with an alias on a non-default network:

docker network create testnet
docker run \
  --net testnet \
  --name sleep \
  --network-alias zzz \
  -d \
  busybox \
  sleep 600

Now we can run another container on that same network and start making DNS queries. We can get the Docker-internal IP address of the zzz alias:

$ docker run --net testnet --rm giantswarm/tiny-tools dig +short zzz
172.18.0.2

Then we can reverse-resolve that address:

$ docker run --net testnet --rm giantswarm/tiny-tools dig +short -x 172.18.0.2
sleep.testnet.

That canonical name is the container name and network, and if you need to probe deeper, you can now docker inspect sleep.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • I thought of this, however I should've provided more context. The main use for this will be local development related. There is already a project which allows for getting ips by container name and hooks in linux' nss layer. (nss-docker). I'd like to extend this program to also translate aliases. As such, starting a container on-the-fly does not seem to be a ideal solution as it incurs quite a big latency (i think.) – hbogert May 08 '20 at 12:50
0

I think you should know what you are doing by reading the docker ref

And then, caress docker simple than ever by just run the following

cat << EOF >> /etc/bash.bashrc
function docker(){
    [[ "ip" == "$1" ]] && /usr/bin/docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $2 || /usr/bin/docker $@
}
EOF
source /etc/bash.bashrc
docker ip $INSTANCE_ID
Salehi
  • 362
  • 2
  • 13
  • it seems I can't use the `alias.network` reference as an argument to `docker inspect` – hbogert Sep 13 '21 at 13:16
  • If you are using docker swarm, the json format should be different, I don't have a swarm cluster to test it, but if you can provide it in some paste service, I'll look for proper solution. – Salehi Oct 02 '21 at 14:34