docker-compose does not set environment variables
Problem
Description of the issue When using `docker-compose up` or `docker-compose run -e ENVIRONMENT_VAR=value`, environment variables are not being applied either through the `-e` command line parameters or from the docker-compose.yml file. The docker-compose.yml file I am using is as follows: [code block] Context information (for bug reports) Output of `docker-compose version` [code block] Output of `docker version` [code block] Output of `docker-compose config` [code block] Steps to reproduce the issue 1. Create a docker-compose.yml file that sets environment variables 2. Run docker-compose up Observed result Environment Variables are not applied Expected result Environment variables should be applied to containers started by docker-compose Stacktrace / full error message [code block] Additional information OS Version: Windows 10 Pro v1909, `docker-compose` install method: Docker Desktop for Windows Installer, followed by manual upgrade of docker-compose (Issue occurred on docker-compose version 1.25.4 which was included with Docker Desktop and with manual upgrade to 1.25.5)
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix Docker-Compose Environment Variable Issue
The issue arises when the environment variables are not properly defined in the docker-compose.yml file or when the command line arguments are not recognized due to syntax errors or incorrect usage. Additionally, the environment variables may not be set in the correct context or may be overridden by other configurations.
Awaiting Verification
Be the first to verify this fix
- 1
Check docker-compose.yml Syntax
Ensure that the environment variables are defined correctly in the docker-compose.yml file. They should be listed under the appropriate service and formatted correctly.
yamlversion: '3' services: my_service: image: my_image environment: - ENVIRONMENT_VAR=value - 2
Use .env File for Variables
Create a .env file in the same directory as your docker-compose.yml file to define environment variables. Docker Compose automatically reads this file.
bashENVIRONMENT_VAR=value - 3
Run Docker-Compose with Correct Syntax
When using command line options, ensure you are using the correct syntax. For example, use 'docker-compose run -e ENVIRONMENT_VAR=value my_service' to set environment variables for a specific service.
bashdocker-compose run -e ENVIRONMENT_VAR=value my_service - 4
Check for Overriding Variables
Verify that there are no other configurations or files (like Dockerfile or other environment files) that might override the environment variables set in docker-compose.yml or .env file.
- 5
Rebuild Containers
After making changes to the environment variables, rebuild the containers to ensure that the new environment settings are applied. Use the command 'docker-compose up --build'.
bashdocker-compose up --build
Validation
To confirm the fix worked, run 'docker-compose up' and check the logs or use 'docker exec -it <container_id> env' to list the environment variables inside the running container. Ensure that the expected environment variables are present.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep