Reputation: 3299
I have an .env
file and a docker-compose file. I reference the env file using the env_file
property on the docker-compose. Then I use the variable interpolation to set another environment variable using the variable defined in the .env file. For some reason, Docker doesn't recognize the variable defined in the .env file.
app.env:
MYSQL_DATABASE=wordpress
DB_USER=root
MYSQL_ROOT_PASSWORD=example
docker-compose.yml:
version: '3.1'
services:
wordpress:
container_name: 'wp'
build: .
restart: unless-stopped
links:
- mysql
env_file:
- app.env
environment:
- WORDPRESS_DB_USER=${DB_USER}
- WORDPRESS_DB_PASSWORD=${MYSQL_ROOT_PASSWORD}
- WORDPRESS_DB_NAME=${MYSQL_DATABASE}
ports:
- 80:80
Upvotes: 5
Views: 4121
Reputation: 28593
It seems you mixed up the meaning of the env_file configuration option. It is passing multiple environment variables from an external file through to a service’s containers. It is the same as environment
, but they are loaded from the file.
If you want to docker compose
recognize the variable defined in the .env
file it should be exactly .env file.
To achieve what you want you need:
1. Rename app.env
to .env
:
mv app.env .env
2. Delete env_file
configuration option from docker-compose.yaml
:
version: '3.1'
services:
wordpress:
container_name: 'wp'
build: .
restart: unless-stopped
links:
- mysql
environment:
- WORDPRESS_DB_USER=${DB_USER}
- WORDPRESS_DB_PASSWORD=${MYSQL_ROOT_PASSWORD}
- WORDPRESS_DB_NAME=${MYSQL_DATABASE}
ports:
- 80:80
Upvotes: 7