2015-10-13 642 views
4

我正在使用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]" 
+0

你想实现什么? – Smutje

+0

@Smutje我想将本地机器IP地址传递给码头集装箱。在我的脚本中$ IP给出的容器地址不是我的本地主机。 –

回答

5

正如在“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?”)

由于OP Pawan Sharmacomments

docker0给主机泊坞窗IP。我用eth0,它给我的主机IP。

-e "DOCKER_HOST=$(ip -4 addr show eth0| grep -Po 'inet \K[\d.]+') 
+0

谢谢。 docker0给主机码头IP。我用eth0它给我的主机ip。这是正确还是通用的解决方案? –

+0

@PawanSharma好的。我已经编辑了相应的答案,并包含您的评论以获取更多可见性。 – VonC

+0

这在Windows上不适用于我。我总是得到一个172.x.x.x IP,但不是我正在寻找的10.0.75.X IP。 – Huber

1

docker run -it --net="host" IMAGE

但我不太清楚你想要什么。 类似于应通过主机IP访问的应用程序?