首页 > 解决方案 > 如何在 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 文件约定一样

标签: dockerkubernetesdocker-compose

解决方案


我进行了没有任何问题的测试:

$ 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

推荐阅读