3

Is it possible to get host's info from within docker, for example

  1. HOST Machine's IP (eth0's IP not docker0's)
  2. Available RAM in HOST machine

etc.,

Thanks in advance

Shan
  • 2,141
  • 2
  • 17
  • 32

1 Answers1

4

It's not generally possible - it would break the isolation which Docker provides.

In this article, you can see how, any breakout from the Docker container is actually a serious security issue.

https://blog.docker.com/2014/06/docker-container-breakout-proof-of-concept-exploit/

However, there are various workarounds:

http://blog.michaelhamrah.com/2014/06/accessing-the-docker-host-server-within-a-container/

Suggests that the following approach:

"Although there’s no way to introspect the host’s ip address (AFAIK) you can pass this in via an environment variable:"

docker@boot2docker:~$  docker run -i -t -e DOCKER_HOST=192.168.59.103 ubuntu /bin/bash
root@07561b0607f4:/# env
HOSTNAME=07561b0607f4
DOCKER_HOST=192.168.59.103
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/

On EC2, specifically, you can access the instance's metadata:

See

Fetching AWS instance metadata from within Docker container?

In particular:

$ curl http://169.254.169.254/latest/meta-data/hostname
ec2-203-0-113-25.compute-1.amazonaws.com

Other metadata options are listed here:

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html

Community
  • 1
  • 1
Donald_W
  • 1,773
  • 21
  • 35
  • Thank you, Donald I'm currently using the same method on EC2. `docker run -it -p 9200:9200 -p 9300:9300 -e HOST_IP=$(hostname --ip-address) shan/elk:latest` My use case is, I'm running elasticsearch inside docker and i need to pass the IP of the host machine to `network.publish_host:` parameter in elasticsearch Without passing the HOST_IP, auto discovery is not working Thank you very much Donald. – Shan Apr 26 '15 at 19:21
  • I'm afraid you have'll to pass it in - it's just not possible to break out of the container otherwise. – Donald_W Apr 26 '15 at 19:34
  • Have to live with it, as security is more important – Shan Apr 26 '15 at 20:13
  • 1
    Donald's answer is true in general, but on EC2, you could query instance metadata to get the IP address. – Nathaniel Waisbrot Apr 26 '15 at 20:15
  • Thanks both of you @Donald_W and @NathanielWaisbrot, it working fine. I'm able to get the instance IP using `curl http://169.254.169.254/latest/meta-data/local-ipv4`. – Shan Apr 27 '15 at 11:09