简体   繁体   English

如何在docker-compose.yml中有条件地运行不同的配置?

[英]How to conditionally run different configs in docker-compose.yml?

I have configured SSL via certbot on live server. 我已经通过实时服务器上的certbot配置了SSL。 I have a volume mapping for this in nginx section of docker-compose.yml : 我在docker-compose.yml nginx部分对此进行了卷映射:

volumes:
  ...
  - /etc/letsencrypt:/etc/letsencrypt

This works just fine on the live server but I have a different setup on my local machine, where I run the app and see it on http://localhost . 这在实时服务器上可以正常工作,但是我在本地计算机上进行了其他设置,在该计算机上运行应用程序并在http://localhost上看到了该应用程序。 I suppose I don't need SSL on my local machine, so probably I just can exclude this part of setup if it runs locally. 我想我不需要在本地计算机上使用SSL,因此如果它在本地运行,则可能可以排除这部分设置。

Also this case makes me think I will have to potentially configure some other things differently locally vs live. 同样,这种情况使我认为我将不得不潜在地在本地配置与实时配置其他一些其他东西。

So, the question is how to properly distinguish these differences between local and live setups and apply them (semi)automatically depending on the environment? 因此,问题是如何正确地区分本地设置和实时设置之间的差异,并根据环境自动(半)应用它们?

Environment variables are typically a good way to create simple runtime portability like this, and many tools/apps/packages support runtime configuration by nothing more than environment variables. 环境变量通常是一种创建简单的运行时可移植性的好方法,并且许多工具/应用程序/程序包仅通过环境变量来支持运行时配置。 Unfortunately, nginx is not one of those apps 不幸的是,nginx不是这些应用程序之一

For nginx, try something like this: 对于nginx,请尝试如下操作:

environment:
  - NGINX_CONF=localhost.conf

Here, localhost.conf would be your nginx configuration for your local machine. 在这里,localhost.conf将是本地计算机的nginx配置。 Run an entrypoint.sh of some kind, and symlink the config specified by NGINX_CONF to wherever nginx will pick it up (usually /etc/nginx/conf.d or /etc/nginx/sites-enabled). 运行某种类型的entrypoint.sh,并将NGINX_CONF指定的配置符号链接到nginx会选择它的位置(通常是/etc/nginx/conf.d或/ etc / nginx / sites-enabled)。

ln -s /etc/nginx/conf.d/running.conf /app/nginx-confs/${NGINX_CONF}

This assumes you have all of your confs copied to the container in /app/nginx-confs, but they can live wherever you like. 假设您已将所有conf复制到/ app / nginx-confs中的容器,但是它们可以放置在您喜欢的任何位置。 The localhost.conf would serve your site as http://localhost . localhost.conf将作为http:// localhost服务您的站点。

For your live server, pass NGINX_CONF=liveserver.conf. 对于您的实时服务器,请传递NGINX_CONF = liveserver.conf。 This conf would serve https://www.liveserver.com or whatever you host is. 该conf将服务https://www.liveserver.com或您托管的任何主机。

At this point, you can choose which nginx configuration you'll run when you start your container, using environment variables. 此时,您可以使用环境变量选择在启动容器时将运行的nginx配置。 Even if you don't want to do it this way, hopefully it gets you moving in the right direction and thinking about environment variables as a way to configure at runtime. 即使您不想这样做,也希望它可以使您朝正确的方向前进,并考虑将环境变量作为在运行时进行配置的一种方式。


There are other, more granular ways of managing nginx confs at runtime. 还有其他更精细的方式可以在运行时管理nginx conf。 Something like confd or a templating engine like the mustache templating engine are options. 诸如confd之类的东西或诸如胡子模板引擎之类的模板引擎都是可选的。 There are roundabout ways in nginx with env directive and set_by_lua , but this feels like the most hacky solution to me so I prefer the others. 在nginx中有env指令和set_by_lua回旋方式,但这对我来说似乎是最棘手的解决方案,所以我更喜欢其他方式。

You can write a Makefile to generate different docker-compose.yml files depending on your needs. 您可以根据需要编写一个Makefile生成不同docker-compose.yml文件。

Makefile: 生成文件:

# Makefile
-include config.mk

A_CONFIG_VAR ?= default_value

all: docker-compose.yml

docker-compose.yml: docker-compose.yml.in config.mk
        @echo 'Generating docker-compose.yml'
        @sed -e 's|@@A_CONFIG_VAR@@|$(A_CONFIG_VAR)|g' $< > $@

config.mk: put your configuration variables in this file. config.mk:将您的配置变量放入此文件中。

# config.mk
A_CONFIG_VAR = "a_value"

docker-compose.yml.in: write an input docker-compose.yml file like so docker-compose.yml.in:像这样编写一个输入docker-compose.yml文件

volumes:
  - /path/to/somewhere:@@A_CONFIG_VAR@@

Change the contents of config.mk to suit your needs. 更改config.mk的内容以适合您的需求。 And then run 然后跑

make

This should generate a docker-compose.yml file for you. 这应该为您生成一个docker-compose.yml文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM