1

Is there any way of getting the container name inside the container in a java programm using processBuilder ?

Example: the command docker run -it --name AppContainer RetrieveNameApp should return AppContainer

Thank you !

Javier Buzzi
  • 6,296
  • 36
  • 50
N7Legend
  • 71
  • 9
  • 2
    Is this an xy problem? https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Don Branson Dec 16 '20 at 22:56
  • 1
    Going along with what Don asked, why would you want a container to even know that it is dockerized? Containers should be oblivious to it. If you explain why you want it, maybe we can suggest a better solution or try to find an API for what you are asking for. – zero298 Dec 16 '20 at 22:59
  • 1
    well this might help you https://stackoverflow.com/questions/20995351/how-can-i-get-docker-linux-container-information-from-within-the-container-itsel – dreamcrash Dec 16 '20 at 22:59
  • 1
    Very interesting ! I did not know that this kind of problem was theorized. In fact here I really need the name of the container from inside this same container. Thanks to @dreamcrash and some research, I created a new environment variable to be able to access the name of the container from inside and send data to a database – N7Legend Dec 16 '20 at 23:33

1 Answers1

1

I just ran into this use case, but the examples given were docker centric, not to mention they wanted you to expose docker.sock which... it's bad form/security risk.

Prerequisite:

apt update
apt install dnsutils

Or...

yum install bind-utils

Lastly:

hostname -I | xargs -L 1 -d ' ' dig +noall +answer -x | awk '{print $5}' | cut -f2 -d _ | sort | uniq

If you run this inside the running docker container, you'll get the name.


Working examples:

(docker-compose) Note: you can copy this EXACTLY into your shell. This is for demonstration purposes ONLY, and i DO NOT suggest you write your docker-compose.yaml like this.

docker-compose -f <(cat << EOF
services:
    this-is-my-name-every-body:
      image: python:3.6
EOF
) run --rm this-is-my-name-every-body bash -c "apt update &>/dev/null; apt install -y dnsutils &>/dev/null; hostname -I | xargs -L 1 -d ' ' dig +noall +answer -x | awk '{print \$5}' | cut -f2 -d _ | sort | uniq"

(docker) Note: in order to be able to do a reverse lookup the docker run command MUST have a --network, and that requires you to docker network create it (must be only ran once).

docker network create temp-net
docker run --rm -it --network temp-net --name hello-every-body python:3.6 bash -c "apt update &>/dev/null; apt install -y dnsutils &>/dev/null; hostname -I | xargs -L 1 -d ' ' dig +noall +answer -x | awk '{print \$5}' | cut -f2 -d _ | sort | uniq"
Javier Buzzi
  • 6,296
  • 36
  • 50
  • Wow! This was a wildly unknown feature of the docker DNS server! Here's a slightly simpler one-liner for a container with a single network interface: "host $(hostname -i)" will return something like "4.0.18.172.in-addr.arpa domain name pointer cont_name_1.service_default." – xrobau May 26 '22 at 23:23
  • 1
    @xrobau there is a lot of overlap between `host` and `dig`, the reason i chose `dig` was because it uses tabs to separate the parts of the output, making it easier to parse out. `host` unfortunately uses spaces as delimiters, and makes it more difficult. While they both arrive at the same outcome, I did make the shell command more complicated to cover my butt, in case someone was using `--scale 2` (+) and the same name was used multiple times. – Javier Buzzi May 27 '22 at 09:06