加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 如何在docker容器中获取本地主机IP地址?

发布时间:2020-12-15 22:03:36 所属栏目:安全 来源:网络整理
导读:我正在使用docker并使用脚本运行.我想在docker中使用主机IP地址更改其中一个配置. #!/bin/bashIP=$(echo `ifconfig eth0 2/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'`)echo Setting xwiki config IPCONFIG=/xwiki/webapps/xwiki/WEB-INF/xwik
我正在使用docker并使用脚本运行.我想在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.

docker run -t -i $EXPOSE_PORTS $IMAGE "$@"

解决方法

如“ How to get the ip address of the docker host from inside a docker container”中所述,您可以直接从容器访问它:

/sbin/ip route|awk '/default/ { print $3 }'

但是,as commented,当您使用docker bridge(默认)作为容器时,这将输出桥接IP,如172.17.42.1而不是主机的IP,如192.168.1.x(典型的家庭NAT)

您可以使用run –env< key> =< value>将实际IP作为环境变量传递

-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 Sharma comments:

docker0 gives host docker ip. I used eth0,it gives my host ip.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读