How do I use the env_file directive in Docker Compose properly?

0
5
Asked By TechGuru58 On

I'm trying to organize my environment variable files for Docker Compose by separating them instead of using one large .env file. My plan is to have two files for each area: a 'common' file and a specific file. In this example, I have two files: .env.common and .env.main, but no .env file. However, when I try to start my containers, I'm getting ambiguous warning messages. Here's my `compose.yaml` setup:

```
services:
hello-main:
image: hello-world
env_file:
- .env.common
- .env.main
environment:
- TZ=${TZ}
- APPDIR=${APPDIR}
- PUID=${PUID}
- PGID=${PGID}
- FOOBAR=${FOOBAR}
- ZONE=example.com
```

The .env.common file contains variables common to every area:

```
TZ="America/New_York"
APPDIR=/home/docker/dockerservice
PUID=1000
PGID=1000
```

And .env.main has just a specific value:

```
FOOBAR=172.16.68.8
```

When I run the containers, I see warnings like:

```
WARN[0000] The "TZ" variable is not set. Defaulting to a blank string.
WARN[0000] The "APPDIR" variable is not set. Defaulting to a blank string.
WARN[0000] The "PUID" variable is not set. Defaulting to a blank string.
WARN[0000] The "PGID" variable is not set. Defaulting to a blank string.
WARN[0000] The "FOOBAR" variable is not set. Defaulting to a blank string.
```

It seems like neither the .env.common nor .env.main files are being utilized by the env_file directive. What might I be doing wrong? Also, I've updated my configuration based on some community suggestions, where I learned that I might not need the environment section unless I'm overriding values. So now it looks like this:

```
environment:
# - TZ=${TZ}
# - APPDIR=${APPDIR}
# - PUID=${PUID}
# - PGID=${PGID}
- FOOBAR=my_foobar # override ${FOOBAR}
- ZONE=example.com
```

Can someone help clarify what's going wrong?

3 Answers

Answered By NerdyDevCrime On

I recommend you drop the environment section completely unless you're specifically overriding values from the env_file. The way you have it set up should work without needing to define the variables in multiple places.

Answered By CodeWizard77 On

When you use env_file in Docker Compose, you actually don't need to define the environment variables separately in your compose file. The env_file directive automatically pulls the variables from your specified files. Try removing the environment section altogether to see if that fixes the warnings you're getting.

Answered By DevExplorer24 On

It looks like your issue might stem from the fact that you're not using a .env file. Without a .env file, any variables defined like ${PUID} are treated as empty by Docker since there's no source for them. If you want to keep using env_file, it's best to just stick to that and only define the variables in your .env.common and .env.main files. Those won't be accessible in compose without an actual .env file.

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.