If we can already use the env or use proccess.env.VARIABLE_NAME(or any other programming language own getting .env variable method) in the script, why do we still need to add environment variable inside docker-compose file, like this:
So if Iām happy with using environment variable directly reading the ā.envā, I no need to add another environment section inside docker compose file? Is there any benefit of people using the environment section rather than reading the ā.envā? I knew there are levels of environment variable, environment variables inside docket compose will replace the ā.envā.
Nothing is really required, just an option for specific usecases. If your application can read a .env file, thatās fine, but that means, only your application will know about the variables and you need to read the file every time when you execute a command in the container using docker exec for example.
Also worth noting that variables in a .env file can be used to choose a docker network dynamically or set a port number which has nothing to do with the application in the container. It is also possible that the application expects a URL as an endpoint, but you want to create separate variables like EP_PROTOCOL, EP_HOSTNAME, EP_PATH and so on and you just concatenate it and optionally generate a new .env file for the application.
You could also have an appliation which was written without the support of reading a file for the variables but you still want to run it in a container.
So Docker and Docker Compose just makes things possible, but eventually it is up to you how you use the features.