gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:09:31 25 4
gpt4 key购买 nike

我正在使用 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 网桥(默认)时,这将输出网桥 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.]+')

关于bash - 如何在docker容器中获取本地主机IP地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33102813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com