bake: load .env file from working dir for compose files - #1261
Conversation
a64873c to
01bb0a8
Compare
|
As discussed with @tonistiigi I changed the current behavior (see updated PR description). |
| default: | ||
| e, err := dotenv.UnmarshalBytes(f.Data) |
There was a problem hiding this comment.
Previously, files with an unknown extension were parsed using the HCL parser. Now they will be parsed as env files. @tonistiigi Do you think it could be an issue? Maybe instead we could check for .env extension and default to hcl?
There was a problem hiding this comment.
Previously, files with an unknown extension were parsed using the HCL parser. Now they will be parsed as env files.
What is the difference?
I would prefer to not change the -f behavior and only handle .env as a special case when parsing compose.
There was a problem hiding this comment.
Ah, I think I understand you intentions better now.
Still in the first PR maybe .env is enough. Just to have compatibility.
For the -f we can maybe first split the files between compose and HCL. Then parse HCL and store all the attributes in the process. Then parse compose(for the second time if needed) and pass the attributes and variables.
If we support -f this way it shouldn't be only for some special limited set of HCL files that don't have any blocks but ideally all the vars that would be replaced in HCL would be replaced in Compose yaml as well(and additionally only Compose does .env as well).
There was a problem hiding this comment.
For the
-fwe can maybe first split the files between compose and HCL. Then parse HCL and store all the attributes in the process. Then parse compose(for the second time if needed) and pass the attributes and variables.If we support
-fthis way it shouldn't be only for some special limited set of HCL files that don't have any blocks but ideally all the vars that would be replaced in HCL would be replaced in Compose yaml as well(and additionally only Compose does.envas well).
Ah right we also need to store the attributes before passing them to HCL/JSON/Compose files when parsed. I missed that.
I will just add .env support for compose in this PR and open another one for -f.
There was a problem hiding this comment.
@crazy-max Thankyou for this PR to fix load .env file from working dir for compose files ! I was facing the exact issue that's discussed here and it's resolved.
Very much looking forward to add another option -f file to load another .env data file such that the below command will run or please suggest another option to pass a custom env variable file. Essentially same as #1135
docker buildx bake -f docker-compose.yml -f .ghcr.env should work.
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
| func parseCompose(dt []byte) (*compose.Project, error) { | ||
| return loader.Load(compose.ConfigDetails{ | ||
| func ParseCompose(dt []byte, envs map[string]string) (*Config, error) { | ||
| if wd, err := os.Getwd(); err == nil { |
There was a problem hiding this comment.
Looks like this suits better in ParseComposeFile. envs is also passed to this function so shouldn't be extended. But ParseComposeFile already reads env from the host.
| return curenv | ||
| } | ||
|
|
||
| dt, err := os.ReadFile(ef) |
There was a problem hiding this comment.
I think these syscalls should still error unless the error is notexist
|
|
||
| envs, err := dotenv.UnmarshalBytes(dt) | ||
| if err != nil { | ||
| return curenv |
There was a problem hiding this comment.
Is the behavior to ignore parse error the behavior in compose as well?
There was a problem hiding this comment.
I read the wrong implementation. The right one from compose-go indeed check for errors.
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
fixes #282
carry #489
Loads
.envfile if available in the working directory for compose files. The file format is the same as the compose library: https://github.com/compose-spec/compose-go/tree/master/dotenv.