问题
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?
回答1:
If you're attempting to pass in data that will be built with the image then you're looking for ARG
and --build-arg
; which can be found in the Dockerfile documentation.
Dockerfile:
FROM ubuntu
ARG REMOTE_HOST
RUN echo ${REMOTE_HOST} > /my_file
Then build the file:
➜ 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
Run the image and print out the file:
➜ 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:
Dockerfile:
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" ]
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=$REMOTE_HOST';
}
entrypoint.sh
#!/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 "$@"
Build the image:
➜ 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
Run the image and provide REMOTE_HOST
➜ 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';
}
回答2:
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 --add-host argument when running "docker run". Something like the following:
docker run --add-host=remote_host:10.180.0.1 ....
来源:https://stackoverflow.com/questions/47596381/how-to-setup-an-variable-env-inside-dockerfile-to-be-overriden-in-a-docker-conta