简体   繁体   中英

How to get local host IP address in docker container?

I am using docker and run using script. I want to change in one of configuration with host machine IP address in docker.

#!/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

I run my docker with following command.

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

As mentioned in " How to get the ip address of the docker host from inside a docker container ", you can directly access it from the container:

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

But, as commented , when you are using the docker bridge (default) for the containers, this will output the bridges IP like 172.17.42.1 rather than the host's IP such as 192.168.1.x (typical of an home NAT)

You can pass the actual IP as an environment variable with run --env <key>=<value>

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

(From " Is there an easy way to programmatically extract IP address? ")

As the 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.]+')

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

but I'm not quite sure what you want to achive. Something like a application that should be accessable via host ip?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM