Home > Back-end >  How can we run a docker compose with .env in another directory?
How can we run a docker compose with .env in another directory?

Time:10-22

I am creating a docker compose file in a root directory with some id variable passing to it for volume creation using .env file, but one thing is I am creating a new directory for every id and placing .env in that directory, can we able to point this .env to pass parameters to the docker compose in root. Because I read some articles where that both need to reside in the same directory.

My docker-compose.yml

version: "3.7"
services:
xyz:
  image: xyz
  environment:
    ID: "${ID}"


 ports:
    - "XYZ:XYZ"
 volumes:
    - /XYZ${ID}:/data

Which will be basically in sub directory example in 1234 directory

.env file

ID=1234

CodePudding user response:

As documented in official page you can either override default path with:

docker compose --env-file ./config/.env.dev config

or start container with:

docker compose --env-file ./config/.env.dev up

There is also an option to define it in docker-compose.yaml fila as:

xyz:
  env_file:
    - /path-to/web-variables.env

CodePudding user response:

This might be what you're looking for: https://docs.docker.com/compose/environment-variables/#using-the---env-file--option

  • Related