How do I set up a .env file for my Docker service configuration?

0
0
Asked By CleverPanda87 On

I'm trying to figure out how to use a .env file for my service's configuration. My .env file is located at /home/user/docker/.env, and I have my config file at /home/user/docker/app/config.yml. I want to make sure I can reference the environment variables in my config like so: user: !ENV pass: !ENV. What's the best way to point my service to this .env file? Do I need to specify something in my Docker Compose file?

3 Answers

Answered By SharingWisdom99 On

I've learned that while naming your file .env is standard, it can lead to some confusion and bugs. To avoid this, you might want to hard code some defaults that won't change (not secrets, of course) and create a second env file with a different name, like .env.service.override. This way, you can use a sample default file without issues in a team setting.

Answered By TechNinja42 On

You can definitely point to your .env file when you run your Docker Compose setup. Use the `--env-file` option like this:

```bash
cd /home/user/docker/app/
docker compose --env-file ../.env up -d
```
This command changes to the app directory first and then specifies the path to your .env file, which should work perfectly for your service's configuration!

Answered By CoderChris89 On

To answer your question about pointing to the env file, yes, you absolutely need to add a line in your Compose file for that! You can check out the official documentation for detailed instructions. Search for "env" on their site, and you'll find what you need.

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.