根据 docker docs , .env 文件中的环境变量应采用 key-val 格式,如 VAR=VAL
这适用于像 foo=bar
这样的样本但没有提到不可避免的特殊字符,例如'=',这可能会混淆 key-val
分隔符 OR space
有效数据库连接字符串的两个部分,如:
secrets.env 文件:
connectionString=Data Source=some-server;Initial Catalog=db;User ID=uid;Password=secretpassword
在 docker-compose.debug.yaml 文件内容中引用为:
services:
some-service:
container_name: "service-name"
env_file:
- secrets.env
ports:
- "80:80"
进一步用于转化为
docker-compose.yaml
如下图所示完整流程:所以问题是 - 你如何包含具有
=
的连接字符串和 Spaces
作为值(value)的一部分?需要 - 我们在 VS 解决方案中几乎没有微服务,并期待避免重复相同的连接字符串,否则需要在 `docker-compose.yaml' 的服务规范中
试过 包括单/双引号中的值,但在
=
之后进行任何转换被视为包含引号的值,就像 kubernets yaml 文件约定一样
最佳答案
我运行了一个没有任何问题的测试:
$ cat .env
ENV=default
USER_NAME=test2
SPECIAL=field=with=equals;and;semi-colons
$ cat docker-compose.env.yml
version: '2'
services:
test:
image: busybox
command: env
environment:
- SPECIAL
$ docker-compose -f docker-compose.env.yml up
Creating network "test_default" with the default driver
Creating test_test_1_55eac1c3767c ... done
Attaching to test_test_1_d7787ac5bfc0
test_1_d7787ac5bfc0 | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
test_1_d7787ac5bfc0 | HOSTNAME=d249a16a8e09
test_1_d7787ac5bfc0 | SPECIAL=field=with=equals;and;semi-colons
test_1_d7787ac5bfc0 | HOME=/root
test_test_1_d7787ac5bfc0 exited with code 0
关于docker - 如何在 docker-compose .env 文件中包含特殊字符(例如 '=' )作为变量值的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55192288/