如何在Docker容器中获取本地主机IP地址?
问题描述:
我正在使用docker并使用脚本运行。我想在Docker中用主机IP地址更改其中一个配置。如何在Docker容器中获取本地主机IP地址?
#!/bin/bash
IP=$(echo `ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'`)
echo Setting xwiki config IP
CONFIG=/xwiki/webapps/xwiki/WEB-INF/xwiki.cfg
sed -i -e "s/^xwiki.authentication.authhost=localhost*/xwiki.authentication.authhost= $IP/" $CONFIG
/xwiki/start_xwiki.sh -f
我跑我用下面的命令泊坞窗。
docker run -t -i $EXPOSE_PORTS $IMAGE "[email protected]"
答
正如在“How to get the ip address of the docker host from inside a docker container”所提到的,您可以直接从容器访问:
/sbin/ip route|awk '/default/ { print $3 }'
但是,as commented,当您使用泊坞窗桥(默认)的容器,这将输出的桥梁IP像172.17.42.1,而不是主机的IP,如192.168.1.X(典型的家用NAT)的
您可以通过实际的IP为环境变量与run --env <key>=<value>
-e "DOCKER_HOST=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')
(摘自 “Is there an easy way to programmatically extract IP address?”)
docker0
给主机泊坞窗IP。我用eth0
,它给我的主机IP。
-e "DOCKER_HOST=$(ip -4 addr show eth0| grep -Po 'inet \K[\d.]+')
答
docker run -it --net="host" IMAGE
?
但我不太清楚你想要什么。 类似于应通过主机IP访问的应用程序?
你想实现什么? – Smutje
@Smutje我想将本地机器IP地址传递给码头集装箱。在我的脚本中$ IP给出的容器地址不是我的本地主机。 –