从容器开始传递变量到文件 [英] Passing variable from container start to file

查看:127
本文介绍了从容器开始传递变量到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Docker文件中有以下几行,我想在配置文件中将值设置为默认值,然后应用程序在最后启动时提供,并且可以使用 -e 选项启动容器。



我正在尝试使用Docker的 ENV 突击队

  ENV CONFIG_VALUE default_value 
运行sed -i'/ CONFIG_VALUE /'$ CONFIG_VALUE'/ g'CONFIG_FILE
CMD command_to_start_app

我在文件CONFIG_FILE中显式显示了字符串CONFIG_VALUE,Dockerfile的默认值被正确地替代。但是,当我使用添加的 -e CONFIG_VALUE = 100 运行容器时,不执行替换,将保留Dockerfile中设置的默认值。



当我做

  docker exec -i -t container_name bash 

echo $ CONFIG_VALUE 环境变量确实包含所需的值100。

解决方案

在Dockerfile中不可能:这些指令是静态的制作图像。



如果在启动容器时需要运行时指令,则应该使用 CMD 指令。

换句话说, sed 将发生在 CMD 调用。在执行 docker运行时,该脚本将可以访问在 docker运行之前设置的环境变量。 p>

I have the following lines in a Dockerfile where I want to set a value in a config file to a default value before the application starts up at the end and provide optionally setting it using the -e option when starting the container.

I am trying to do this using Docker's ENV commando

 ENV CONFIG_VALUE default_value
 RUN sed -i 's/CONFIG_VALUE/'"$CONFIG_VALUE"'/g' CONFIG_FILE
 CMD command_to_start_app

I have the string CONFIG_VALUE explicitly in the file CONFIG_FILE and the default value from the Dockerfile gets correctly substituted. However, when I run the container with the added -e CONFIG_VALUE=100 the substitution is not carried out, the default value set in the Dockerfile is kept.

When I do

docker exec -i -t container_name bash

and echo $CONFIG_VALUE inside the container the environment variable does contain the desired value 100.

解决方案

That shouldn't be possible in a Dockerfile: those instructions are static, for making an image.

If you need runtime instruction when launching a container, you should code them in a script called by the CMD directive.
In other words, the sed would take place in a script that the CMD called. When doing the docker run, that script would have access to the environment variable set just before said docker run.

这篇关于从容器开始传递变量到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆