40532

How to setup an variable ENV inside Dockerfile to be overriden in a Docker container?

Question:

I have a container that should be created with the IP user.

This is what i have inside the Dockerfile:

ENV REMOTE_HOST=xxxxxxxxxx RUN { \ echo '[xdebug]'; \ echo 'zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so'; \ echo 'xdebug.remote_enable=1'; \ echo 'xdebug.remote_port=9000'; \ echo 'xdebug.remote_autostart=1'; \ echo 'xdebug.remote_handler=dbgp'; \ echo 'xdebug.idekey=dockerdebug'; \ echo 'xdebug.profiler_output_dir="/var/www/html"'; \ echo 'xdebug.remote_connect_back=0'; \ echo 'xdebug.remote_host=$REMOTE_HOST'; \ } >> /usr/local/etc/php/php.ini

And this is how I create an container derived from that Dockerfile: dockerrun an image from that Dockerfile:

docker run -e REMOTE_HOST=123456 -p 80:80 -v /Users/myusrname/Documents/mysite:/var/www/html myImage

This is what I have in php.ini inside the container:

root@1713e0a338f9:/var/www/html# cat /usr/local/etc/php/php.ini ... [xdebug] zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so xdebug.remote_enable=1 xdebug.remote_port=9000 xdebug.remote_autostart=1 xdebug.remote_handler=dbgp xdebug.idekey=dockerdebug xdebug.profiler_output_dir="/var/www/html" xdebug.remote_connect_back=0 xdebug.remote_host=$REMOTE_HOST

What is the correct way to pass that variable?

Answer1:

If you're attempting to pass in data that will be <em>built with the image</em> then you're looking for ARG and --build-arg; which can be found in the <a href="https://docs.docker.com/engine/reference/builder/#arg" rel="nofollow">Dockerfile documentation.</a>

<strong>Dockerfile:</strong>

FROM ubuntu ARG REMOTE_HOST RUN echo ${REMOTE_HOST} > /my_file

<strong>Then build the file:</strong>

➜ docker build -t test_image --build-arg REMOTE_HOST=1.2.3.4 . Sending build context to Docker daemon 10.24kB Step 1/3 : FROM ubuntu ---> 20c44cd7596f Step 2/3 : ARG REMOTE_HOST ---> Using cache ---> f9815e560847 Step 3/3 : RUN echo ${REMOTE_HOST} > /my_file ---> Running in da07d5d060b7 ---> cdfdbeac71b9

<strong>Run the image and print out the file:</strong>

➜ docker run test_image cat /my_file 1.2.3.4

I'll leave this with a note though; that you probably don't want to be hard-coding an IP address to your image; and instead you should set up your image to instead read the environment variable and update that file at runtime; in that case, once your Dockerfile is setup to handle that - you would use docker run -e REMOTE_HOST=1.2.3.4. Do this, you'll want something like:

<strong>Dockerfile:</strong>

FROM ubuntu COPY php.ini /usr/local/etc/php/php.ini COPY entrypoint.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/entrypoint.sh # Or ensure it's +x already ENTRYPOINT [ "entrypoint.sh" ]

<strong>php.ini</strong>

{ echo '[xdebug]'; echo 'zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so'; echo 'xdebug.remote_enable=1'; echo 'xdebug.remote_port=9000'; echo 'xdebug.remote_autostart=1'; echo 'xdebug.remote_handler=dbgp'; echo 'xdebug.idekey=dockerdebug'; echo 'xdebug.profiler_output_dir="/var/www/html"'; echo 'xdebug.remote_connect_back=0'; echo 'xdebug.remote_host=$REMOTE_HOST'; }

<strong>entrypoint.sh</strong>

#!/bin/bash set -e # Check if our environment variable has been passed. if [ -z "${REMOTE_HOST}" ] then echo "REMOTE_HOST has not been set." exit 1 else sed -i.bak "s/\$REMOTE_HOST/${REMOTE_HOST}/g" /usr/local/etc/php/php.ini fi exec "$@"

<strong>Build the image:</strong>

➜ docker build -t test_image . Sending build context to Docker daemon 4.608kB Step 1/5 : FROM ubuntu ---> 20c44cd7596f Step 2/5 : COPY php.ini /usr/local/etc/php/php.ini ---> 1785c0238ce8 Step 3/5 : COPY entrypoint.sh /usr/local/bin/ ---> c63c289c411e Step 4/5 : RUN chmod +x /usr/local/bin/entrypoint.sh # Or ensure it's +x already ---> Running in 09b07f8724a9 ---> 66ab090f405a Removing intermediate container 09b07f8724a9 Step 5/5 : ENTRYPOINT entrypoint.sh ---> Running in 1f5a7ebec98e ---> 2992128843cd Removing intermediate container 1f5a7ebec98e Successfully built 2992128843cd Successfully tagged test_image:lates

<strong>Run the image and provide REMOTE_HOST</strong>

➜ docker run -e REMOTE_HOST=1.2.3.4 test_image cat /usr/local/etc/php/php.ini { echo '[xdebug]'; echo 'zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so'; echo 'xdebug.remote_enable=1'; echo 'xdebug.remote_port=9000'; echo 'xdebug.remote_autostart=1'; echo 'xdebug.remote_handler=dbgp'; echo 'xdebug.idekey=dockerdebug'; echo 'xdebug.profiler_output_dir="/var/www/html"'; echo 'xdebug.remote_connect_back=0'; echo 'xdebug.remote_host=1.2.3.4'; }

Answer2:

If you run

docker run --build-arg REMOTE_HOST=123456 ....

Then your docker file should have the following line to retrieve the environment variable.

ARG REMOTE_HOST ENV REMOTE_HOST=$REMOTE_HOST

And you should be able to print it using a php echo like the following:

echo 'xdebug.remote_host='.getenv('REMOTE_HOST');

However if you just want to pass a host to the container you should be using the <em>--add-host</em> argument when running "docker run". Something like the following:

docker run --add-host=remote_host:10.180.0.1 ....

Recommend