如何从Docker容器内部获取Docker主机的IP地址

本文讨论了如何从Docker容器内部获取主机IP地址和端口映射。建议的方法包括在创建容器时传递主机信息作为环境变量,通过Docker远程API,或者对于在AWS上运行的情况,利用实例元数据。
摘要由CSDN通过智能技术生成

本文翻译自:How to get the IP address of the docker host from inside a docker container

As the title says. 如标题所示。 I need to be able to retrieve the IP address the docker hosts and the portmaps from the host to the container, and doing that inside of the container. 我需要能够检索Docker主机的IP地址和从主机到容器的端口映射,并在容器内部进行操作。


#1楼

参考:https://stackoom.com/question/1YGwh/如何从Docker容器内部获取Docker主机的IP地址


#2楼

唯一的方法是在创建容器时将主机信息作为环境传递

run --env <key>=<value>

#3楼

/sbin/ip route|awk '/default/ { print $3 }'

正如@MichaelNeale所注意的那样,没有必要在Dockerfile使用此方法(除非仅在构建期间需要此IP),因为此IP将在构建期间进行硬编码。


#4楼

The --add-host could be a more cleaner solution (but without the port part, only the host can be handled with this solution). --add-host可能是一个更简洁的解决方案(但如果没有端口部分,则只能使用此解决方案来处理主机)。 So, in your docker run command, do something like: 因此,在您的docker run命令中,执行以下操作:

docker run --add-host dockerhost:`/sbin/ip route|awk '/default/ { print  $3}'` [my container]

(From https://stackoverflow.com/a/26864854/127400 ) (来自https://stackoverflow.com/a/26864854/127400


#5楼

If you enabled the docker remote API (via -H tcp://0.0.0.0:4243 for instance) and know the host machine's hostname or IP address this can be done with a lot of bash. 如果启用了tcp://0.0.0.0:4243 远程API (例如,通过-H tcp://0.0.0.0:4243 )并且知道主机的主机名或IP地址,则可以通过大量的bash来完成。

Within my container's user's bashrc : 在我容器的用户的bashrc

export hostIP=$(ip r | awk '/default/{print $3}')
export containerID=$(awk -F/ '/docker/{print $NF;exit;}' /proc/self/cgroup)
export proxyPort=$(
  curl -s http://$hostIP:4243/containers/$containerID/json |
  node -pe 'JSON.parse(require("fs").readFileSync("/dev/stdin").toString()).NetworkSettings.Ports["DESIRED_PORT/tcp"][0].HostPort'
)

The second line grabs the container ID from your local /proc/self/cgroup file. 第二行从本地/proc/self/cgroup文件中获取容器ID。

Third line curls out to the host machine (assuming you're using 4243 as docker's port) then uses node to parse the returned JSON for the DESIRED_PORT . 第三行缩小到主机(假设您使用4243作为docker的端口),然后使用node解析DESIRED_PORT返回的JSON。


#6楼

For those running Docker in AWS, the instance meta-data for the host is still available from inside the container. 对于在AWS中运行Docker的用户,仍可以从容器内部获得主机的实例元数据

curl http://169.254.169.254/latest/meta-data/local-ipv4

For example: 例如:

$ docker run alpine /bin/sh -c "apk update ; apk add curl ; curl -s http://169.254.169.254/latest/meta-data/local-ipv4 ; echo"
fetch http://dl-cdn.alpinelinux.org/alpine/v3.3/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.3/community/x86_64/APKINDEX.tar.gz
v3.3.1-119-gb247c0a [http://dl-cdn.alpinelinux.org/alpine/v3.3/main]
v3.3.1-59-g48b0368 [http://dl-cdn.alpinelinux.org/alpine/v3.3/community]
OK: 5855 distinct packages available
(1/4) Installing openssl (1.0.2g-r0)
(2/4) Installing ca-certificates (20160104-r2)
(3/4) Installing libssh2 (1.6.0-r1)
(4/4) Installing curl (7.47.0-r0)
Executing busybox-1.24.1-r7.trigger
Executing ca-certificates-20160104-r2.trigger
OK: 7 MiB in 15 packages
172.31.27.238

$ ifconfig eth0 | grep -oP 'inet addr:\K\S+'
172.31.27.238
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值