Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the hostname of the docker host from inside a docker container on that host without env vars

Tags:

docker

What are the ways get the docker host's hostname from inside a container running on that host besides using environment variables? I know I can pass the hostname as an environment variable to the container at container creation time. I'm wondering how I can look it up at run time.

foo.example.com (docker host)   bar (docker container) 

Is there a way for container bar running in docker host foo.example.com to get "foo.example.com"?

Edit to add use case:

The container will create an SRV record for service discovery of the form

_service._proto.name. TTL class SRV priority weight port target. ----------------------------------------------------------------- _bar._http.example.com 60 IN SRV 5000 5000 20003 foo.example.com. 

where 20003 is a dynamically allocated port on the docker host for a service listening on some fixed port in bar (docker handles the mapping from host port to container port).

My container will run a health check to make sure it has successfully created that SRV record as there will be many other bar containers on other docker hosts that also create their own SRV records.

_service._proto.name. TTL class SRV priority weight port target. ----------------------------------------------------------------- _bar._http.example.com 60 IN SRV 5000 5000 20003 foo.example.com. <-- _bar._http.example.com 60 IN SRV 5000 5000 20003 foo2.example.com. _bar._http.example.com 60 IN SRV 5000 5000 20003 foo3.example.com. 

The health check will loop through the SRV records looking for the first one above and thus needs to know its hostname.

aside

I'm using Helios and just found out it adds an env var for me from which I can get the hostname. But I was just curious in case I was using docker without Helios.

like image 844
David Xia Avatar asked Apr 22 '15 21:04

David Xia


People also ask

How do I connect to host from inside container?

Accessing the Host With the Default Bridge Mode You just need to reference it by its Docker network IP, instead of localhost or 127.0. 0.1 . Your host's Docker IP will be shown on the inet line. Connect to this IP address from within your containers to successfully access the services running on your host.

How do I find the Docker image name?

The easiest way to list Docker images is to use the “docker images” with no arguments. When using this command, you will be presented with the complete list of Docker images on your system. Alternatively, you can use the “docker image” command with the “ls” argument.


1 Answers

You can easily pass it as an environment variable

docker run .. -e HOST_HOSTNAME=`hostname` .. 

using

-e HOST_HOSTNAME=`hostname` 

will call the hostname and use it's return as an environment variable called HOST_HOSTNAME, of course you can customize the key as you like.

note that this works on bash shell, if you using a different shell you might need to see the alternative for "backtick", for example a fish shell alternative would be

docker run .. -e HOST_HOSTNAME=(hostname) .. 
like image 196
Mohammad AbuShady Avatar answered Sep 27 '22 23:09

Mohammad AbuShady