300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 如何从Docker容器内部获取Docker主机的IP地址

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

时间:2019-11-02 02:40:01

相关推荐

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

本文翻译自: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楼

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

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

(From /a/26864854/127400 )(来自/a/26864854/127400 )

#5楼

If you enabled the docker remote API (via-Htcp://0.0.0.0:4243for 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 (例如,通过-Htcp://0.0.0.0:4243)并且知道主机的主机名或IP地址,则可以通过大量的bash来完成。

Within my container's user'sbashrc:在我容器的用户的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/cgroupfile.第二行从本地/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 theDESIRED_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-/alpine/v3.3/main/x86_64/APKINDEX.tar.gzfetch http://dl-/alpine/v3.3/community/x86_64/APKINDEX.tar.gzv3.3.1-119-gb247c0a [http://dl-/alpine/v3.3/main]v3.3.1-59-g48b0368 [http://dl-/alpine/v3.3/community]OK: 5855 distinct packages available(1/4) Installing openssl (1.0.2g-r0)(2/4) Installing ca-certificates (0104-r2)(3/4) Installing libssh2 (1.6.0-r1)(4/4) Installing curl (7.47.0-r0)Executing busybox-1.24.1-r7.triggerExecuting ca-certificates-0104-r2.triggerOK: 7 MiB in 15 packages172.31.27.238$ ifconfig eth0 | grep -oP 'inet addr:\K\S+'172.31.27.238

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。