Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the host ip dynamically in a docker-compose file?

For a docker development environment we are using a docker-compose-file. Everything works fine and we are really happy, but there is one tiny issue i would to get working:

We install php-fpm with xdebug. Now we configure it as follows:

PHP-Dockerfile:

FROM php:7.1-fpm

RUN docker-php-ext-install pdo_mysql \
&& docker-php-ext-install json \
&& pecl install xdebug \
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini

docker-compose.yml:

services:
  //[...]
  php:
    build: ./docker/php/
    container_name: dev-php
    expose:
      - 9000
    volumes:
      - .:/var/www/html
    environment:
      XDEBUG_CONFIG: remote_host=192.168.0.50
      PHP_IDE_CONFIG: "serverName=docker"

This setup works fine: we are able to debug with xdebug in the IDE on the local system. The only problem is, that we save one IP "192.168.0.50" static in the docker-compose-file. So each employee hast to change it to his own IP before running...

Now the question: is there a smarter way to get the hosts IP-address dynamically when building?

like image 860
j0nnybrav0 Avatar asked Oct 20 '25 19:10

j0nnybrav0


1 Answers

There's no "magic command" that will get host IP address, so the only way to get around this problem is solution with Environment Variables:

services:
  //[...]
   php:
     build: ./docker/php/
     container_name: dev-php
   expose:
     - 9000
   volumes:
     - .:/var/www/html
   environment:
     XDEBUG_CONFIG: remote_host=${WORKER_IP}
     PHP_IDE_CONFIG: "serverName=docker"

And then pass this variable by .env file:

https://docs.docker.com/compose/env-file/

More you can read in documentation:

https://docs.docker.com/compose/environment-variables/#configuring-compose-using-environment-variables.

Good luck :)

like image 196
Krzysztof Raciniewski Avatar answered Oct 23 '25 11:10

Krzysztof Raciniewski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!