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

您所在的位置:网站首页 docker容器的ip 如何从Docker容器内部获取Docker主机的IP地址

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

2024-02-03 05:59| 来源: 网络整理| 查看: 265

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


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3