如果您试图传入数据,则
用图像构建
那你要找的是
ARG
和
--build-arg
; 可以在
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
. 这样做,你会想要:
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';
}
入口点。上海
#!/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
运行映像并提供远程_主机
â 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';
}