This context provides a comprehensive guide on using environment variables in Python and Docker.
Abstract
The context begins by explaining the purpose and benefits of using environment variables in programming, specifically in Python and Docker. It then proceeds to demonstrate how to access these variables in Python using the os package. The text also introduces the use of .env files for managing environment variables, emphasizing their convenience and security. The latter part of the context focuses on using environment variables in Docker, particularly with docker-compose for local development. The author provides a step-by-step guide on setting up a Docker environment with environment variables, including creating a Dockerfile, .env file, and docker-compose file. The context concludes with a summary of the importance of environment variables in software development.
Opinions
The author believes that using environment variables is a useful way of providing inputs into programs, allowing for the addition of configurations and secret variables.
The author suggests that using .env files is a more convenient and secure method for managing environment variables compared to inserting variables through the terminal.
The author emphasizes the importance of not pushing .env files containing secrets to git.
The author recommends using docker-compose for local development, even if only one docker container is being created.
The author highlights the difference between the env_file option and the environment option in the docker-compose file.
The author advises using a .dockerignore file to avoid adding the .env-file into the image, especially if it contains sensitive information.
The author concludes by stressing the importance of environment variables in software development, as they are a common medium for injecting configurations into software.
Environment variables in Python & Docker
Generated using OpenAI DALL·E 2.
Using environment variables is a useful way of providing inputs into programs. It can be used to:
add configurations
insert secret variables
These variables are frequently used when deploying applications with your cloud service provider and/or Docker. It is definitely something you should have knowledge of since it is used everywhere, regardless of programming language or system.
To list existing environment variables in bash, you can open a terminal and type:
printenv
To set an environment variable in bash, you simply type:
exportMY_ENV_VARIABLE="MY VALUE"
To verify that it now exists, type:
echo$MY_ENV_VARIABLE
And “MY VALUE” should be printed.
Python
To access these variables in Python we can use the os package. There are two ways to access them. Here I will use one of the variables I found on my system (MacBook, bash) when typing printenv (TERM_PROGRAM):
Great, we know how to access the variables, but inserting variables through the terminal using export KEY=VALUE is rather inconvenient. Instead, we would like to have a way to change multiple variables easily and insert them into the software smoothly.
Enter .env-files. These files are handy for inserting env-variables by only changing a single file. Each line in the file will describe a variable to value-mapping, for instance:
Here the second variable was quoted, but the result will be the same whether you type with or without quotes. The third variable uses previously defined environment variables using ${<variable_name>}. All variables will by default be loaded as strings. The names for these files are usually “.env”. If we have multiple files we could add a prefix or suffix and switch between them. Now, imagine we have a file called .env with the content specified above. To insert these into Python locally, we can use the python-dotenv package. Start by installing it:
pip install python-dotenv
Then only a single function call is necessary to load the variables from the file:
NOTE: if you have secrets in a .env file, don’t forget to add it to .gitignore to avoid it from being pushed to git.
Docker
When deploying applications docker is often used. With it, there are more appropriate options for inserting environment variables than using python-dotenv. Here I’m going to talk about local development. When you deploy your container(s) to the cloud the insertion of env-variables would depend on what provider and deployment option that is used.
I will use docker-compose here. While it is not necessary, I believe it’s a great tool regardless if you create one or multiple docker containers. For the demo project the following file structure is used:
Folder structure
There is a quite a few files, but they all contain very little code. In the docker file we have:
A python base-image is imported, here alpine because it is lightweight. The app folder is then copied and finally the python file, main.py, is run. In the .env file we have:
MY_SECRET_VARIABLE = "I'm batman!"
As described, the variable is “secret”, env-variables that are not secret can be hardcoded in the docker-compose file, which is shown below:
Note the comment describing the difference between the env_file option and the environment optionand using the filename .env specifically as opposedtosomething else.
Next, a .dockerignore file is added. This is to avoid adding the .env-file into the image since it might contain sensitive information. The content is thus only:
.env
Finally, we have the main.py file that will be run. Here the environment variables are just printed:
To run everything the following expression is typed in the terminal:
docker-compose -f docker-compose.yml up --build
And the result is:
Results
Summary
Environment variables are important because they are a common medium for injecting configurations into our software. While in this article Python was used, environment variables can be used with most programming languages. Good luck!
If you’re interested in reading more articles about Python, check out my reading list below: