Docker: 在容器启动时更改文件中的单词
Docker: Change word in file at container startup
我正在为我们的 fluentd 创建一个 docker 图像。
该图像包含一个名为 http_forward.conf 的文件
它包含:
<store>
type http
endpoint_url ENDPOINTPLACEHOLDER
http_method post # default: post
serializer json # default: form
rate_limit_msec 100 # default: 0 = no rate limiting
raise_on_error true # default: true
authentication none # default: none
username xxx # default: ''
password xxx # default: '', secret: true
</store>
这就是我们的形象。但我们希望将图像用于我们所有的环境。用环境变量指定。
所以我们为我们的环境创建一个环境变量:
ISSUE_SERVICE_URL = http://xxx.dev.xxx.xx/api/fluentdIssue
这个环境变量在我们的开发环境中包含 dev,在 uat 中包含 uat 等。
比我们想用我们的环境变量的值替换我们的 ENDPOINTPLACEHOLDER 。在 bash 中我们可以使用:
sed -i -- 's/ENDPOINTPLACEHOLDER/'"$ISSUE_SERVICE_URL"'/g' .../http_forward.conf
但是 how/when 如果我们想在我们的 docker 容器中使用它,我们必须执行这个命令吗? (我们不想挂载这个文件)
我们通过 ansible
编码实现了这一点。
将文件http_forward.conf
作为模板,根据环境部署更改,然后将文件夹(包括conf文件)挂载到docker容器。
ISSUE_SERVICE_URL = http://xxx.{{ environment }}.xxx.xx/api/fluentdIssue
剧本大概是这样的,我没测试
- template: src=http_forward.conf.j2 dest=/config/http_forward.conf mode=0644
- docker:
name: "fluentd"
image: "xxx/fluentd"
restart_policy: always
volumes:
- /config:/etc/fluent
在您的 DockerFile 中,您应该有一行以 CMD 开头的某处。你应该把它加在那里。
或者您可以做得更干净:将 CMD 行设置为调用脚本。例如 CMD ./startup.sh
。然后文件 startup.sh 将包含您的 sed 命令,然后是启动 fluentd 的命令(我假设当前是 CMD)。
我正在为我们的 fluentd 创建一个 docker 图像。 该图像包含一个名为 http_forward.conf 的文件 它包含:
<store>
type http
endpoint_url ENDPOINTPLACEHOLDER
http_method post # default: post
serializer json # default: form
rate_limit_msec 100 # default: 0 = no rate limiting
raise_on_error true # default: true
authentication none # default: none
username xxx # default: ''
password xxx # default: '', secret: true
</store>
这就是我们的形象。但我们希望将图像用于我们所有的环境。用环境变量指定。
所以我们为我们的环境创建一个环境变量:
ISSUE_SERVICE_URL = http://xxx.dev.xxx.xx/api/fluentdIssue
这个环境变量在我们的开发环境中包含 dev,在 uat 中包含 uat 等。 比我们想用我们的环境变量的值替换我们的 ENDPOINTPLACEHOLDER 。在 bash 中我们可以使用:
sed -i -- 's/ENDPOINTPLACEHOLDER/'"$ISSUE_SERVICE_URL"'/g' .../http_forward.conf
但是 how/when 如果我们想在我们的 docker 容器中使用它,我们必须执行这个命令吗? (我们不想挂载这个文件)
我们通过 ansible
编码实现了这一点。
将文件http_forward.conf
作为模板,根据环境部署更改,然后将文件夹(包括conf文件)挂载到docker容器。
ISSUE_SERVICE_URL = http://xxx.{{ environment }}.xxx.xx/api/fluentdIssue
剧本大概是这样的,我没测试
- template: src=http_forward.conf.j2 dest=/config/http_forward.conf mode=0644
- docker:
name: "fluentd"
image: "xxx/fluentd"
restart_policy: always
volumes:
- /config:/etc/fluent
在您的 DockerFile 中,您应该有一行以 CMD 开头的某处。你应该把它加在那里。
或者您可以做得更干净:将 CMD 行设置为调用脚本。例如 CMD ./startup.sh
。然后文件 startup.sh 将包含您的 sed 命令,然后是启动 fluentd 的命令(我假设当前是 CMD)。