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

Viewed 7052

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?

2 Answers

host.docker.internal

"From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Mac [or Docker Desktop for Windows]"

docker-for-mac

docker-for-windows

Related