docker - 如何在 docker-compose .env 文件中包含特殊字符(例如'=')作为变量值的一部分?
问题描述
根据docker docs,.env 文件中的环境变量预计将采用 key-val 格式,因为VAR=VAL
它适用于示例,foo=bar
但没有提及不可避免的特殊字符,例如“=”,这可能会混淆key-val
分隔符或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
如下所示的完整流程:
所以问题是 - 你如何包含连接字符串作为 value 的一部分=
?Spaces
需要- 我们在 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
推荐阅读
- python-3.x - 给定两个数字的输入,如何在换行符上打印数字列表的切片范围
- git - Git - 在另一个分支发生重大变化后重新创建一个分支是否很好?
- java - 用于本地化我的应用程序的 Google 翻译 API
- python - 为什么从最后一个元素分配的列表中的dict?
- jquery - 附加后删除类
- vba - 在VBA中按条件检查删除行
- laravel - Laravel:switch 在一个项目中正常工作,但在另一个项目中崩溃
- chart.js - 如何在 Chart.js 中以不同颜色更改 y 轴标签的颜色
- docker - 我可以避免为蓝绿部署重复我的服务定义吗?
- java - 水平圆形图像以显示用户个人资料图片