问题描述
我有一个应该由IP用户创建的容器.
I have a container that should be created with the IP user.
这是我在Dockerfile中拥有的内容:
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
这是我创建从该Dockerfile派生的容器的方式:docker从该Dockerfile运行图像:
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
这是我在php.ini中的容器内的内容:
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?
推荐答案
如果您尝试传递将用图像构建的数据,那么您正在寻找ARG
和--build-arg
;可以在 Dockerfile文档中找到.
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
然后构建文件:
➜ 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
运行图像并打印出文件:
➜ docker run test_image cat /my_file
1.2.3.4
尽管如此,我会留个便条;您可能不想将IP地址硬编码到图像中;相反,您应该设置映像以改为读取环境变量并在运行时更新该文件;在这种情况下,一旦设置了Dockerfile即可处理-您将使用docker run -e REMOTE_HOST=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 "$@"
构建图像:
➜ 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
运行映像并提供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';
}
这篇关于如何在Dockerfile中设置变量ENV以在Docker容器中覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!