v2.10 expands .env file values
Related to issue https://github.com/docker/compose/issues/9704. I have a php symfony framework with .env file and config params inside. There I have row: CORS_ALLOW_ORIGIN=^https?://localhost(:[0-9]+)?$ when I'm trying to build using docker-compose I can see error: Invalid template: "^https?://localhost(:[0-9]+)?$" docker-compose v2.10.2 (Linux Standalone binary), docker 2.10.17
I had the exact same issue after updating to 2.10.2. Doubling the $ did the trick. See https://github.com/docker/compose/issues/9704#issuecomment-1202335152
I confirm this same issue related to .env file located in project directory with docker-compose.yml.
I've confirmed here that adding double dollar signs, or single quotes, does prevent expansion of the variable... at the expense of modifying it. Some examples:
Without any escaping:
$ cat .env
FOO=$+0i
$ docker run -it --rm --env-file .env busybox env | grep FOO
FOO=$+0i
$ docker-compose ls
Invalid template: "$+0i"
With double dollar signs:
$ cat .env
FOO=$$+0i
$ docker run -it --rm --env-file .env busybox env | grep FOO
FOO=$$+0i
$ docker-compose ls
NAME STATUS CONFIG FILES
With single quotes:
$ cat .env
FOO='$+0i'
$ docker run -it --rm --env-file .env busybox env | grep FOO
FOO='$+0i'
$ docker-compose ls
NAME STATUS CONFIG FILES
Note that while this doesn't use docker compose for running busybox, the behavior is the same as I demonstrated above. What is the expected workaround here, for environment variables to escape the workaround characters in the service?
I have same problem in docker desktop 4.12
Related to issue #9704. I have a php symfony framework with .env file and config params inside. There I have row: CORS_ALLOW_ORIGIN=^https?://localhost(:[0-9]+)?$ when I'm trying to build using docker-compose I can see error: Invalid template: "^https?://localhost(:[0-9]+)?$" docker-compose v2.10.2 (Linux Standalone binary), docker 2.10.17
^http://localhost:[0-9]+ -- try this
This is fixed in v2.11.2 - the parser is now more flexible and won't error out in this situation anymore.
.env
UNQUOTED=^https?://localhost(:[0-9]+)?$
DOUBLE_QUOTED="^https?://localhost(:[0-9]+)?$"
SINGLE_QUOTED='^https?://localhost(:[0-9]+)?$'
Running env from a Compose service:
UNQUOTED=^https?://localhost(:[0-9]+)?$
DOUBLE_QUOTED=^https?://localhost(:[0-9]+)?$
SINGLE_QUOTED=^https?://localhost(:[0-9]+)?$
For situations like this, I would recommend using single quotes to prevent any unintentional interpolation - see #9879 for details.