Environment Variables for Docker & Docker Compose!!!

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?


Article content
Error when use wrong way of .env & env_file


Article content
And here is full view of issue with docker-compose.yml file

  • When you understand the difference between the .env and env_file field, you will understand the reason for the error and also know how to fix the above error easily.
  • This error often occurs with those who are new to the docker-compose.yml file or even those with many years of experience sometimes encounter it (for many reasons, they cannot read the documentation on the docker homepage clearly)

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

  • The .env is for settings a default environment for Compose. Values set in this file can be used within the Compose file.
  • Or you can use param in start command(up services), same method and result for you (or can use command export to set environment variables with style of Bash/Shell script for Unix/Linux)

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?

  • To fix it, you use --env_file during start, or export to set environment variables in the way of Bash/Shell script, or the default way is .env is saved in the root folder of the project.
  • And an important note is that you need to clearly distinguish whether you want to set environment variables for Containers or Docker Compose file to use, because if you identify incorrectly, you will use it incorrectly and lead to the above error.


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!!!

To view or add a comment, sign in

More articles by Long Nguyen

Others also viewed

Explore content categories