Set ENV variable in container is not working: value is not being overwritten

I have the following piece of definition in a Dockerfile:

# This aims to be the default value if -e is not present on the run command
# Copy the zz-php.ini on the container where it should be
COPY /container-files/etc/php.d/zz-php.ini /etc/php5/mods-available/zz-php.ini

# Create a symlink (same as other .ini files for PHP in Ubuntu)
RUN ln -s /etc/php5/mods-available/zz-php.ini /etc/php5/apache2/conf.d/zz-php.ini

# Copy the script for change the xdebug.remote_host value based on HOST_IP    
COPY /container-files/init-scripts/ /usr/local/bin/

# Execute the script
RUN chmod +x /usr/local/bin/

This is the relevant piece of definition at zz-php.ini:

; Xdebug
xdebug.remote_host=""  => this should be overwrited by HOST_IP

This is the content of the script


sed -i -E "s/xdebug.remote_host.*/xdebug.remote_host=$HOST_IP/" /etc/php5/apache2/conf.d/zz-php.ini

In order to build the image and run the container I follow this steps:

  • Build the image:

     docker build -t reynierpm/dev-php55 .
  • Run the container:

     docker run -e HOST_IP=$(hostname -I | cut -d' ' -f1) 
                --name dev-php5 
                -it /bin/bash reynierpm/dev-php55

After the image gets built and the container is running I open a browser and point to: http://container_address/index.php (which contains phpinfo()) and I can see the value of xdebug.remote_host as

why? What is not running when the container start? Why the value doesn’t get overwritten using the provided value by -e on the run command?

Two things with this command: you’re not actually exposing any ports to the host (docker run -p) option, so I wouldn’t expect to be able to connect to it; and you’re not actually running the script you quoted, just getting a shell.

If you need to do setup like this, the usual way to do it is to add an ENTRYPOINT script that runs your script. (But a service that depends on knowing the IP address at which it can be reached will have no end of problems like this: this is exactly the same problem as running this server on your home WiFi with an unrouteable address, and then trying to reach it from somewhere else. The docker run -p option is the same as configuring your home router to forward the port, but it will be reachable only on some different address.)

Hi David and thanks for your answer, I believe for get the port exposed I should add -p 8080:80. As for the ENTRYPOINT script I doesn’t know how to work with it. This are my first steps with Docker so would be helpful if you help me to get this done by adding an example.

Regarding accessing the container from other places doesn’t matter, the idea behind get the host ip is because I need that setup for Xdebug to work properly.


Also I know that running $(hostname -I | cut -d' ' -f1) on the console will give me the IP address but I have tried the following:

  • -e HOST_IP=$(hostname -I | cut -d' ' -f1)
  • -e HOST_IP="$(hostname -I | cut -d' ' -f1)"

and none works.

What is wrong?