How to Properly Reference an Env File in Docker Compose?

0
14
Asked By CuriousCoder42 On

Hello, I'm trying to understand how Docker Compose handles .env files. According to the documentation, I should be able to reference an env file from anywhere relative to my compose YAML file. My compose file is located in `workspace/docker/engines`, and I have the .env file somewhere else, specifically in my home directory. I can reference secret files using a `~/` path, but can I do the same for my .env? Just to add, I named the file 'file.env' and placed it in the same directory as my compose YAML, but that didn't seem to work either. However, when I use the `--env-file` option, it functions correctly regardless of the location. I also learned that utilizing the following in my compose setup works:

```
env_file:
- path: "../.env"
required: true
```

5 Answers

Answered By DockerDude28 On

It’s tricky because you can't really reference a .env file from just anywhere unless you use `--env-file`. Also, it has to be named `.env`; otherwise, you have to use `--env-file`. So, keeping it next to your compose.yaml and naming it `.env` should solve your problem.

Answered By PathMaster35 On

I typically reference my env file like this: `/folder/folder/super.env` and it works just fine.

Answered By ContainerQueen91 On

There's some confusion here about .env files. The `--env-file` option is specifically for telling Docker Compose where to find its own .env file, which holds variables for the Docker Compose file. The doc you referenced talks about the env file used for services in containers, so you need to specify that path in the service definition.

Answered By EnvGuru99 On

I’m using the env_file directive for each service, and it’s working perfectly without any issues for me.

Answered By DevNinja77 On

You can reference the ENV file, but they won't show up in your container unless you explicitly include them in your compose file. Make sure you are including them correctly.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.