博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何从Docker容器内部获取Docker主机的IP地址
阅读量:3580 次
发布时间:2019-05-20

本文共 2702 字,大约阅读时间需要 9 分钟。

本文翻译自:

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楼

参考:


#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 command, do something like: 因此,在您的命令中,执行以下操作:

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

(From ) (来自 )


#5楼

If you enabled the (via 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 (例如,通过 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 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.gzfetch http://dl-cdn.alpinelinux.org/alpine/v3.3/community/x86_64/APKINDEX.tar.gzv3.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.triggerExecuting ca-certificates-20160104-r2.triggerOK: 7 MiB in 15 packages172.31.27.238$ ifconfig eth0 | grep -oP 'inet addr:\K\S+'172.31.27.238

转载地址:http://imlgj.baihongyu.com/

你可能感兴趣的文章
【IC1】【转 非常好】运算放大器使用的六个经验
查看>>
【IC-ADC 3】ADC的选型
查看>>
HiKey960/970用户手册;HiKey960 Development Board User Manual
查看>>
【书籍推荐】FPGA,xilinx
查看>>
N9-SQL注入(union注入)
查看>>
N10-sql注入(information_schema注入)
查看>>
N1-Kali虚拟机中SQLmap
查看>>
N11-sql注入(http头注入)
查看>>
N2-sqlmap初使用
查看>>
N12-sql盲注原理以及boolean盲注案例实现
查看>>
N13-sqli盲注 基于时间型
查看>>
N1 技术心得 2019-6-26
查看>>
N1-环境配置
查看>>
N2-审计方法与步骤
查看>>
N3-常见的INI配置
查看>>
代码审计 N4 常见危险函数和特殊函数(一)
查看>>
MySQL笔记
查看>>
计算机运算方法之(原码 补码 反码 移码)
查看>>
计算机组成原理之(二进制与十进制互相转换,数的定点表示与浮点数表示)例题:设浮点数字长16位,其中阶码5位(含有1位阶符),尾数11位(含有1位数符)
查看>>
选择排序(java代码实现)
查看>>