Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error "Invalid template" parsing .env file #9746

Closed
3 tasks done
alejgarciarodriguez opened this issue Aug 12, 2022 · 10 comments
Closed
3 tasks done

Error "Invalid template" parsing .env file #9746

alejgarciarodriguez opened this issue Aug 12, 2022 · 10 comments

Comments

@alejgarciarodriguez
Copy link

  • I have tried with the latest version of Docker Desktop
  • I have tried disabling enabled experimental features
  • I have uploaded Diagnostics
  • Diagnostics ID: 795F330D-B823-4025-9550-703A3E38857E/20220812105351

Expected behavior

docker compose works as expected

Actual behavior

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

Information

  • macOS Version: Monterey 12.5
  • Intel chip or Apple chip: Apple chip (M1)
  • Docker Desktop Version: 4.11.1

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?

@alejgarciarodriguez
Copy link
Author

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
Copy link
Contributor

nicks commented Aug 12, 2022

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.

@nicks nicks transferred this issue from docker/for-mac Aug 12, 2022
@bwrgiraud
Copy link

I suspect this change was introduced in the following PR: compose-spec/compose-go#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
Copy link

ayyouboulidi commented Aug 16, 2022

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
Copy link

havekes commented Aug 19, 2022

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
Copy link

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
Copy link

jonaskello commented Aug 30, 2022

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
Copy link

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
Copy link

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
Copy link
Member

milas commented Sep 27, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants