Docker 如何为多个环境创建单个NGINX.conf文件
我使用NGINX作为反向代理 我有3个环境(开发、质量保证、生产) 考虑一下,开发的IP地址是1.2.3.4,qa是4.3.2.1,生产是3.4.1.2 我已经如下配置了nginx.conf文件,在开发环境中,它的工作非常好 在构建这些docker映像时,我已经明确提到了映像应该构建在哪个配置上,如下所示Docker 如何为多个环境创建单个NGINX.conf文件,docker,nginx,nginx-reverse-proxy,nginx-config,Docker,Nginx,Nginx Reverse Proxy,Nginx Config,我使用NGINX作为反向代理 我有3个环境(开发、质量保证、生产) 考虑一下,开发的IP地址是1.2.3.4,qa是4.3.2.1,生产是3.4.1.2 我已经如下配置了nginx.conf文件,在开发环境中,它的工作非常好 在构建这些docker映像时,我已经明确提到了映像应该构建在哪个配置上,如下所示 cd conf/clustered-develop/;sudo docker build -t jcibts-swmdtr-dev.jci.com/nginx:1 --build-arg PO
cd conf/clustered-develop/;sudo docker build -t jcibts-swmdtr-dev.jci.com/nginx:1 --build-arg PORT=8765 --build-arg ENVIRONMENT=clustered-develop .
要求是docker映像应仅构建1,然后将其推送到docker可信存储库
它将被提升到其他环境的docker可信存储库,而无需再次构建映像
我的问题是,我能做些什么来为所有环境使用这些单一的配置
比如用localhost替换ip或者用127.0.0.1替换ip(我两个都试过了,但都没用)
根据这一点:
/etc/nginx/conf.d/nginx.template
)构建映像一次,其中包含您希望在dev、qa和prod之间更改的所有值的变量名。例如:
upstream licenseportal {
server ${NGINX_HOST}:${NGINX_PORT};
}
envsubt
,通过使用特定于环境的值替换模板中的变量来创建新的nginx.conf
:
# For Develop
docker run -d \
-e NGINX_HOST='1.2.3.4' \
-e NGINX_PORT='9006' \
-p 9006:9006 \
jcibts-swmdtr-dev.jci.com/nginx:1 \
/bin/bash -c "envsubst < /etc/nginx/conf.d/nginx.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
# For Production
docker run -d \
-e NGINX_HOST='4.3.2.1' \
-e NGINX_PORT='9006' \
-p 9006:9006 \
jcibts-swmdtr-dev.jci.com/nginx:1 \
/bin/bash -c "envsubst < /etc/nginx/conf.d/nginx.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
用于开发的#
docker run-d\
-e NGINX_HOST='1.2.3.4'\
-e NGINX_端口='9006'\
-p 9006:9006\
jcibts-swmdtr-dev.jci.com/nginx:1\
/bin/bash-c“envsubst/etc/nginx/conf.d/default.conf&&nginx-g‘守护进程关闭’”
#用于生产
docker run-d\
-e NGINX_HOST='4.3.2.1'\
-e NGINX_端口='9006'\
-p 9006:9006\
jcibts-swmdtr-dev.jci.com/nginx:1\
/bin/bash-c“envsubst/etc/nginx/conf.d/default.conf&&nginx-g‘守护进程关闭’”
注意:要使其工作-
envsubt
需要作为映像的一部分安装。i、 e运行apt get-y更新和apt get-y安装gettext
感谢您的回复。在我的例子中,映像将只构建一次。虽然我指定了环境变量,但它将基于这些配置构建。我正在寻找的东西,将允许我在所有环境中使用相同的图像。没有问题。这将只需要您构建一次映像,并使用相同的映像。我已经更新了答案,使之更清楚。
# For Develop
docker run -d \
-e NGINX_HOST='1.2.3.4' \
-e NGINX_PORT='9006' \
-p 9006:9006 \
jcibts-swmdtr-dev.jci.com/nginx:1 \
/bin/bash -c "envsubst < /etc/nginx/conf.d/nginx.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
# For Production
docker run -d \
-e NGINX_HOST='4.3.2.1' \
-e NGINX_PORT='9006' \
-p 9006:9006 \
jcibts-swmdtr-dev.jci.com/nginx:1 \
/bin/bash -c "envsubst < /etc/nginx/conf.d/nginx.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"