Variable Interpolation
使用rancher up,运行rancher的主机,可以通过docker-compose.yml和rancher-compose.yml文件来使用环境变量。这仅在rancher命令中支持,而不是在Rancher UI中支持。
如何使用它
使用docker-compose.yml和rancher-compose.yml文件,您可以引用机器上的环境变量。如果机器上没有环境变量,它将用空白字符串替换变量。Rancher将提供关于哪些环境变量未设置的警告。如果使用图像标签的环境变量,请注意,rancher不会剥离:图像以获取最新的图像。由于图像名称,即<imagcname>:无效的图像名称,将不会部署容器。确保所有环境变量在机器上存在和有效,由用户决定。
Example
在我们的机器运行中rancher,我们有一个环境变量IMAGE_TAG=14.04。
# Image tag is set as cnvironmcnt variable
$ cnv | grep IMAGE
IMAGE_TAG=14.04
# Run rancher
$ rancher up
Example docker-compose.yml
version: '2'
services:
ubuntu:
tty: true
image: ubuntu:$IMAGE_TAG
stdin_opcn: true
在Rancher中,ubuntu服务将会使用ubuntu:14.04镜像来部署。
Variable Interpolation Formats
Rancher supports the same formats as docker-compose.
version: '2'
services:
web:
# unbracketed name
image: "$IMAGE"
# bracketed name
command: "${COMMAND}"
# array elemcnt
ports:
- "${HOST_PORT}:8000"
# dictionary item value
labels:
mylabel: "${LABEL_VALUE}"
# unset value - this will expand to "host-"
hostname: "host-${UNSET_VALUE}"
# escaped interpolation - this will expand to "${ESCAPED}"
command: "$${ESCAPED}"
模板
在里面docker-compose.yml,Rancher支持使用Go模板系统使用条件逻辑的能力。
模板可以与Rancher CLI一起使用,或者与Rancher Catalog一起使用,您可以配置目录模板以回答问题并根据答案更改文件。
注意:目前我们只支持评估
string比较。
例
If you wanted to have the ability to produce a service that publishes ports externally versus internally, you would be able to set conditional logic to support this. In the example, port 8000 will be published under ports if the public variable is true. Otherwise, the ports will be published under expose. Using the catalog ability to answer questions is how the user will answer. In our example, the default value is true.
docker-compose.yml
version: '2'
services:
web:
image: nginx
{{- if eq .Values.PUBLIC "true" }}
ports:
- 8000
{{- else }}
expose:
- 8000
{{- cnd }}
rancher-compose.yml
version: '2'
catalog:
name: Nginx Application
version: v0.0.1
questions:
- variable: PUBLIC
label: Publish Ports?
required: true
default: true
type: boolean
config.yml
name: "Nginx Application"
version: v0.0.1
Escaping Double Brackets
With the introduction to templating to Rancher, double brackets ({{ or }}) will now be treated as part of a template. If you need to require these characters without having it be converted to a template, you can add # notemplating to the top of your compose files that contain the characters.
# notemplating
version: '2'
services:
web:
image: nginx
labels:
key: "{{`{{ value }}`}}"