

Note: Placement of the –env-file is important, if the command is placed after the image name then you will probably get an error similar to:Įrror response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: “–env-file”: executable file not found in $PATH: unknown. This would load the environment file called my_env_file from the current working directly into the container of the image django_container:latest as the container starts. To load the values into a Docker container the docker run command is used with the –env-file flag. Note: Whitespace (spaces) between the key and the value are read as values, so DATABASE_NAME= geektechstuff and DATABASE_NAME=geektechstuff are two different values, as is a key=value that has a space at the end of the value before the carriage return compared to one that doesn’t. An environment file is a text file with each environment variable key and value set on an individual line.įor example in my example env file I would add values to each of the keys so that each line has key=value. With this in mind an environment file, or env file, can be useful. Setting environment values individually via the Docker Run command can become a little bothersome when there are multiple values, especially if it means typing in each one. In the above example the DATABASE_NAME, DATABASE_HOST, DATABASE_USER and DATABASE_PORT values are all set. Multiple values can be passed to the command at the same time: docker run -e DATABASE_NAME=my_db -e DATABASE_HOST=localhost -e DATABASE_USER=geektechstuff -e DATABASE_PORT=3306 django_container:latest This runs the image called django_container with the tag of latest and sets the DATABASE_NAME value to my_db. For example: docker run -e DATABASE_NAME=my_db django_container:latest To set the environment variables individually via the docker run command use the -e flag.

With Django set up to read environment variables I then needed to pass the variables into my Docker container. Setting Environment Variables In A Docker Container If the value has not been set within the OS environment then errors will occur. if a key of DATABASE_NAME has been set then Django expects to find DATABASE_NAME=the_name_of_the_database within the operating system’s (OS) environment. Django will then look for an environment variable using the same key e.g.
