Reputation: 13800
I keep all my docker stuff in a docker/
subdir under my project root, including docker-compose.yml
and .env
.
When I run:
$ docker-compose -f /path/to/my-project/docker/docker-compose.yml up -d
everything works fine. However, I'd like to avoid having to specify -f
every time, so I set the COMPOSE_FILE
variable:
$ export COMPOSE_FILE="/path/to/my-project/docker/docker-compose.yml"
$ docker-compose up -d
WARN[0000] The "DJANGO_DB_USER" variable is not set. Defaulting to a blank string.
WARN[0000] The "DJANGO_DB_NAME" variable is not set. Defaulting to a blank string.
WARN[0000] The "DJANGO_DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DJANGO_DB_PASSWORD" variable is not set. Defaulting to a blank string.
...
Unfortunately, the docker/.env
file is no longer used when using COMPOSE_FILE
as opposed to specifying via command line.
What's the best practice for handling this? My main goal is to not have to type a long command every time I want to run a compose command.
I'm using docker-compose
version 2.2.3
Upvotes: 1
Views: 1731
Reputation: 20447
If you use the -f
flag to point to the compose file, the .env
needs to be next to the compose file. However, if you use the variable COMPOSE_FILE
then the .env
needs to be at PWD
, so where you invoke the compose CLI.
That is why I tend to put my .env file at the root of my project and add variables as such:
COMPOSE_PROJECT_NAME=my-project
COMPOSE_FILE=.docker/compose.yaml
The project name variable is useful so that compose doesnt name the project just docker
. If have this pattern in a couple different project, you end up with name clashes when not setting the project name.
This allows me to hide all the docker related cruft in its own directory and still being able to just do docker compose up
.
The only thing is that you need this env file or another mechanism such as direnv/.envrc to let compose know what you want to do.
Upvotes: 1