docker / compose

Define and run multi-container applications with Docker
https://docs.docker.com/compose/
Apache License 2.0
33.99k stars 5.23k forks source link

Error "Invalid template" parsing .env file #9746

Closed alejgr closed 2 years ago

alejgr commented 2 years ago

Expected behavior

docker compose works as expected

Actual behavior

Error: Invalid template: "^https?://(localhost|127\\.0\\.0\\.1)(:[0-9]+)?$"

Information

Output of /Applications/Docker.app/Contents/MacOS/com.docker.diagnose check

Starting diagnostics

[PASS] DD0027: is there available disk space on the host? [PASS] DD0028: is there available VM disk space? [PASS] DD0031: does the Docker API work? [PASS] DD0004: is the Docker engine running? [PASS] DD0011: are the LinuxKit services running? [PASS] DD0016: is the LinuxKit VM running? [PASS] DD0001: is the application running? [PASS] DD0018: does the host support virtualization? [PASS] DD0017: can a VM be started? [PASS] DD0015: are the binary symlinks installed? [PASS] DD0003: is the Docker CLI working? [PASS] DD0013: is the $PATH ok? [PASS] DD0007: is the backend responding? [PASS] DD0014: are the backend processes running? [PASS] DD0008: is the native API responding? [PASS] DD0009: is the vpnkit API responding? [PASS] DD0010: is the Docker API proxy responding? [PASS] DD0012: is the VM networking working? [PASS] DD0032: do Docker networks overlap with host IPs? [SKIP] DD0030: is the image access management authorized? [PASS] DD0019: is the com.docker.vmnetd process responding? [PASS] DD0033: does the host have Internet access? No fatal errors detected.

Steps to reproduce the behavior

I am using a sample docker-compose file like this:

services:
  hello:
    container_name: hello-world
    image: hello-world

Also I have this .env file in the same directory:

CORS_ALLOW_ORIGIN=^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$

As you can see, .env file is not used in docker-compose.yaml, but when I run any docker compose command, the error above is thrown.

.env file is used by another application. If I rewrite it as follows (two $ at the end), then it works for docker compose, but this is not the correct value I want to have.

CORS_ALLOW_ORIGIN=^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$$

Why is docker-compose forcing me to change this .env file?

alejgr commented 2 years ago

With legacy docker-compose it is working

$ docker-compose version

docker-compose version 1.29.2, build 5becea4c
docker-py version: 5.0.0
CPython version: 3.9.0
OpenSSL version: OpenSSL 1.1.1h  22 Sep 2020
nicks commented 2 years ago

This is more of a compose issue, so I'm going to transfer it over to the compose repo to make sure they see it.

bwrgiraud commented 2 years ago

I suspect this change was introduced in the following PR: https://github.com/compose-spec/compose-go/pull/276

It seems that escaping the $ in a regex with \ resolves the issue, though it may cause issues with some tool chains.

E.g.:

CORS_ALLOW_ORIGIN="^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$"

will work if converted to:

CORS_ALLOW_ORIGIN="^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?\$"

(I'm not sure if the " are required; I'll leave that question to others. I use them in my .env file and it works with both phpDotEnv and Docker Compose.

ayyouboulidi commented 2 years ago

I am facing the same problem.

Before, this was working:

CORS_ALLOW_ORIGIN=^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$ (even without the " @bwrgiraud )

Now the only thing working is escaping the $ with a \ and adding " to the string in the .env file like this:

CORS_ALLOW_ORIGIN="^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?\$"

havekes commented 2 years ago

I am facing the same issue with a password variable in my .env which contains a $ sign like so : PASSWORD="[...]$[...]". Had to downgrade to compose 2.6 (docker desktop 4.10.1)

danielporto commented 2 years ago

Got the same issue, noticed different behavior in mac and linux. It works for mac, it does not in linux. Same docker version and compose version and files.

jonaskello commented 2 years ago

I have the same issue with Docker Desktop 4.11.1 on Windows:

❯ docker compose up -d
Invalid template: "{\"dev\": \"^(all|jonas)$\"}"

My .env file has this line, if I comment it out with # prefix it works:

LOG_FILTER={"dev": "^(all|jonas)$"}

The .env file is unrelated to docker, it just happens to be in the same directory.

danielporto commented 2 years ago

The workaround is using single quotes. It wont work with variables, of course. But at least the hashes are now read correctly with docker compose config.

NaeemKhan14 commented 2 years ago

I am passing secret_key as a variable in .env file. Yesterday it was working fine, today it has started giving me Invalid template error. I did not update or change anything.

milas commented 2 years ago

This is fixed in v2.11.2, which has a more flexible parser and won't return errors in this situation anymore.