Environment Variables

Environment Variables

In this tutorial we will discuss about docker environment variables. An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer.

Docker Environment Variables

Environment variables are a common way for developers to move application and infrastructure configuration into an external source outside of application code.

A common reason to do this is to enable easier switching between environments. If you keep the key configuration, you need to move your application between a development and testing machine separate from the code; your application is more portable.

Let’s start with a simple web application written in python.

import os 
from flask import Flask 

app = Flask(__name__)

..
..
..

color = "blue"

@app.route("/")
def main():
    print(color)
    return rendor_template('hello.html', color=color)
    
if __name__ == "__main__":
    app.run(host="0.0.0.0", port="8080")

This piece of code is used to create a web application that displays a web page with a background color.

If you look closely into the application code you will see a line that sets the background color to blue. Now that works just fine.

However if you decide to change the color in the future you will have to change the application code. It is a best practice to move such information out of the application code and into say an environment variable called APP_COLOR.

import os 
from flask import Flask 

app = Flask(__name__)

..
..
..

color = os.environ.get("APP_COLOR")

@app.route("/")
def main():
    print(color)
    return rendor_template('hello.html', color=color)
    
if __name__ == "__main__":
    app.run(host="0.0.0.0", port="8080")

The next time you run the application set an environment called APP_COLOR to a desired value and the application now has a new color once your application get packed into docker image.

If you wish to pass the environment variable then you would now use the docker run command with -e or –env option to set the environment variable within the container.

$ docker run -e APP_COLOR=red my-simple-webapp:2.0.0

To deploy multiple containers with different colors, you would run the docker run command multiple times and set a different value for the environment variable each time.

$ docker run -e APP_COLOR=green my-simple-webapp:2.0.0

$ docker run -e APP_COLOR=yellow my-simple-webapp:2.0.0

$ docker run -e APP_COLOR=pink my-simple-webapp:2.0.0
Inspect Environment Variables

How do you find the environment variable set on a container that’s already running? Use the docker inspect command to inspect the properties of a running container.

Under the config section you will find the list of environment variables set on the container.

If you have many environment variables and especially if they’re meant to be secret, you can use an env-file option.

$ docker run --env-file ./my-env-file my-simple-webapp:2.0.0

The –env-file property takes a filename as an argument and expects each line to be in the VAR=VAL format.

Environment Variables


Scroll to top