Docker:在容器启动时更改文件中的单词

我正在为我们的fluentd创build一个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> 

所以这是在我们的形象。 但是我们想要为我们所有的环境使用图像。 用环境variables指定。

所以我们为我们的环境创build一个环境variables:

 ISSUE_SERVICE_URL = http://xxx.dev.xxx.xx/api/fluentdIssue 

这个envvariables在我们的开发环境中包含开发工具,比如在uat上等。比我们想要用我们的envvariables的值replace我们的ENDPOINTPLACEHOLDER。 在bash中我们可以使用:

 sed -i -- 's/ENDPOINTPLACEHOLDER/'"$ISSUE_SERVICE_URL"'/g' .../http_forward.conf 

但是,如果我们想在Docker容器中使用这个命令,我们该如何/何时执行这个命令呢? (我们不想挂载这个文件)

在你的DockerFile中你应该有一个以CMD开头的行。 你应该把它添加到那里。

或者你可以做得更干净:设置CMD行代替调用脚本。 例如CMD ./startup.sh 。 然后文件startup.sh将包含您的sed命令,然后命令启动您的fluentd(我假设目前是CMD)。

我们通过ansible编码来做到这一点。

将文件http_forward.conf作为模板,并根据环境部署更改,然后将该文件夹(包括conf文件)挂载到docker容器。

 ISSUE_SERVICE_URL = http://xxx.{{ environment }}.xxx.xx/api/fluentdIssue 

剧本会是这样的,我不testing它。

 - 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