Reputation: 31
At first, I placed .env in the root directory, defined env_file as - .env, and set APP_PORT, and it worked fine. After that, I changed the placement to secret/.env, but even if I define env_file as above, the port remains at the default 8000. I tried various things, such as restarting the container, but it didn't work.
services:
app:
container_name: "app"
build:
context: .
dockerfile: Dockerfile
env_file:
- secrets/.env
ports:
- "${APP_PORT:-8000}:8000"
volumes:
- .:/app
restart: always
I would be grateful for any advice.
Upvotes: 0
Views: 32
Reputation: 158908
Compose has two layers of environment-variable handling. It can substitute variables in the Compose file itself, and it defines the environment for each individual container. env_file:
defines only the per-container environment, but you're trying to use its value to substitute a value in the Compose file; it doesn't work for this.
The docker compose
base command defines an --env-file
option, so you can use this to provide the file.
docker compose --env-file secrets/.env up -d
If the file only contains values that are used directly in the Compose file (potentially including the right-hand side of environment:
, but you don't expect these values to be visible inside the container otherwise) then you can remove env_file:
.
For single values like this, and especially if you have a default value in the Compose file, you can also just set the environment variable on the host.
export APP_PORT=8765
docker compose up -d
In both cases it may be helpful to ensure these values are consistently set. If you use docker compose --env-file
, you may need to use it on every Compose invocation, lest it decide your containers have incorrect configuration and need to be rebuilt with default settings.
Upvotes: 0