Environment Variables for Docker & Docker Compose!!!
A container's environment is not set until there's an explicit entry in the service configuration to make this happen. With Compose, there are two ways you can set environment variables in your containers with your Compose file.
services:
webapp:
environment:
DEBUG: "true"
or like that
env_file:
- path: ./default.env
required: true # default
- path: ./override.env
required: false
or can input many keys into file .env
So what is the difference between ".env" & "env_file" field?
Have you ever encountered an error like the one below when working with the docker-compose.yml file? And how to fix this error?
Recommended by LinkedIn
What is the difference between
The .env file in the project root and the env_file field in the Compose file are two different concepts.
Use .ENV file
docker-compose --env-file {path_to_store}/environment.env -d up [....]
Use ENV_FILE field
The env_file field is for setting the default environment for a container. Values set in this can be used in the container, but not in the Compose file.
And how to fix according to the above document?
Conclusion
Basically, the way to set and use Docker environment variables in general or docker-compose.yml file in particular is also based on Bash/Shell environment variables in Unix/Linux, but the difference is that the scope you need to call the variable is in the containers or docker-compose.yml file, so there will be a little difference here, and just pay attention to the scope of use and you will have complete control over it right away.
I believe you can do it and even do it well - very well!!!
Keep it up, my guys!!!
Useful tips