How to include special character (e.g. '=') as part of variable value in docker-compose .env file?

3/16/2019

As per docker docs, environment variables in .env file expected to be in in key-val format as VAR=VAL which works fine for sample like foo=bar but no mention of unavoidable special characters e.g. '=', which may confuse for key-val separator OR space both part of valid db connection string as in:

secrets.env file:

 connectionString=Data Source=some-server;Initial Catalog=db;User ID=uid;Password=secretpassword

which is referred in docker-compose.debug.yaml file content as:

services:
  some-service:
    container_name: "service-name"
    env_file:
      - secrets.env
    ports:
      - "80:80"

Which is further used to transform into docker-compose.yaml as shown complete flow below:

enter image description here

So the question is - how do you include connection string which has = and Spaces as part of value ?

Need - We have few micro-services within VS solution and looking forward to avoid repetition of same connection strings otherwise needs in service spec of `docker-compose.yaml'

Tried including values in single/double quote but post transformation whatever after = is treated as value including quotes just similar like kubernets yaml file convention

-- AnilR
docker
docker-compose
kubernetes

1 Answer

3/16/2019

I ran a test without any issues:

$ 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
-- BMitch
Source: StackOverflow