2017-09-26 68 views
0

我使用一个简单的Nginx docker容器为一些静态文件提供服务。我使用在容器上设置一个环境变量的Kubernetes将这个容器部署到不同的环境中(例如,FOO_COUNT)。根据环境对响应设置了nginx设置标题var

获得Nginx通过FOO_COUNT的值作为每个响应头的最简单方法是什么,而不必为每个环境构建不同的容器?

+0

请参阅这篇文章也http://tarunlalwani.com/post/simple-parameterized-config-files-docker/。讨论@lifeisfoo给出的类似技巧 –

回答

2

开箱即用,nginx不支持大多数配置块中的环境变量。但是,您可以使用envsubst as explained in the official nginx Docker image

只需创建一个配置模板,该模板在名为的nginx容器中可用,例如, /etc/nginx/conf.d/mysite.template

它包含了你的nginx的配置,例如:

location/{ 
    add_header X-Foo-Header "${FOO_COUNT}"; 
} 

然后如果您使用的是docker-compose.yml文件,

/bin/bash -c "envsubst </etc/nginx/conf.d/mysite.template> /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'" 

覆盖Dockerfile命令,使用command

Kubernetes provides a similar way to override the default command in the image

+0

谢谢。看起来我将不得不深入Kubenetes文档,才能在该窗格上生成模板。 – harryg

+0

不客气。如果它解决了你的问题,请接受答案,这将有助于未来的访问者。 – lifeisfoo