How to reference several env file with absolute paths

I’m trying to reference 2 env files inside a docker compose because the second one is going to be shared with several other compose files and is in a different path

I have done this but I can’t get it work
I’m using this as a reference Set environment variables | Docker Docs


  uptime_kuma:
    container_name: UptimeKuma
    image: louislam/uptime-kuma:latest
    <<: [*config, *dns, *sec]
    env_file:
      - path: .env  #default path with specific variables for this compose
      - path: /mnt/services/docker/docker-compose/ipinventory.env  #shared env file with other docker compose with an absolute path
    networks:
      eth1:
        ipv4_address: ${uptimekuma_ip}
    ports:
      - 3001:3001
    volumes:
      - /mnt/services/docker/Uptime Kuma:/app/data

Is this possible? is there another way to achieve the same? I could just use relatives paths as well the other env file is 1 dir up

env_file sets the CONTAINER’S environment to what’s defined within the files specified

The environment Docker Compose interpolates variables from is not definable within the compose file itself, as the interpolation happens before the YAML is parsed

You can specify a custom .env file to interpolate from using the --env-file flag:

docker compose --env-file .env --env-file /mnt/... [command]

You can also specify them using $COMPOSE_ENV_FILES

1 Like

Thanks, I’m trying to apply this solution in Unraid but i get these errors. What I’m doing wrong?

BTW in case is relevant
root@Unraid:~# docker-compose config
no configuration file provided: not found

I have seen this solution but it’s with docker run and only 1 env file Passing Environment Variables in a File - Docker Engine - Unraid so I guess it doesn’t apply

  1. docker-compose is deprecated, use docker compose. (Although, it does seem like your command is aliased to the proper docker compose)

  2. Your command was: docker compose --env-file [file] --env-file [file2].
    You specified flags but you never really specified what command docker compose should actually execute

  3. You’re currently in the /root directory - This does not seem like the right location, your compose file is not there.

It is. It explains exactly point #3 - your compose file is not in the directory you’re executing the compose command at

The link you provided is irrelevant

1 Like

Ok, if I understood you correctly it would be something like this. Sorry but I’m still learning

docker-compose -f /mnt/services/docker/docker-compose/Monitoring/docker-compose.yml --env-file /mnt/services/docker/docker-compose/Monitoring/.env --env-file /mnt/services/docker/docker-compose/ipinventory.env up -d

Am I right?

Then I should be able to use this in that compose file

    env_file:
      - path: .env  #default path with specific variables for this compose
      - path: /mnt/services/docker/docker-compose/ipinventory.env

Let me introduce you to cd (Change Directory)

/some/location $ cd /somewhere/else
/somewhere/else $ 

So

cd /mnt/services/docker/docker-compose/Monitoring
docker compose --env-file .env --env-file ../ininventory.env up -d

.. mean the parent directory containing the one you’re in

And no, service.env_file is used for a different purpose!

Thanks

I have seen that only works if the other file is named “.env” as well if I try to use another name “ip.env” in the fille name and command it fails

But anyway, my questions now are

Can I run this together with this in a single command?

docker compose -f /mnt/services/docker/docker-compose/Monitoring/docker-compose.yml up -d

Or it requires 2 commands? I guess the env-file command go first.

Every time I stop the docker compose project do I need to run this again or it’s somehow permanent?

Ok, I did a few tests, I’m going to answer my self, thanks for you help

docker compose -f /mnt/services/docker/docker-compose/Monitoring/docker-compose.yml --env-file /mnt/services/docker/docker-compose/Monitoring/.env --env-file /mnt/services/docker/docker-compose/.env up -d
docker compose -f /mnt/services/docker/docker-compose/Monitoring/docker-compose.yml --env-file /mnt/services/docker/docker-compose/Monitoring/.env --env-file /mnt/services/docker/docker-compose/.env down

ip.env is not the source of the error, read the error message, there’s no .env file in the directory you’re in

Yes. There’s no way to make this permanent with pure Docker, then again, simply change your working directory instead of using such convoluted paths all the time