Automatically set host IP as an environment property in docker

362 views Asked by At

I am in a corporate dhcp network and the ip changes frequently. While running one of the docker containers in my laptop, i have to pass the local ip of my machine for allowing a call back to a different application.

I am now running the container with -e MYHOST=x.x.x.x. But when IP changes, i have to lookup the ip manually and edit this property and restart my container.

I use Kitematic for launching. Is there a way it can be configured to automatically pickup the hostIP. Something like -e MYHOST=${hostIP}

2

There are 2 answers

0
Ricardo Branco On

In this case I think you have to use the --network=host option to docker-run.

0
Fabian Braun On

Actually you just need a command providing you the ip address on your operating system.

Then you can do

docker run -e MYHOST=$(<ip-command>) your-image

For example on Linux you could do

docker run -e MYHOST=$(hostname -i) your-image

Check for more options, including MacOS, here.