<\/figure>\r\n\r\n\r\n\r\nThe picture above shows us that we need to use the following syntax to create environment variables:<\/p>\r\n\r\n\r\n\r\n
VARIABLENAME=variablevalue<\/pre>\r\n\r\n\r\n\r\nPlease note, the variables are case sensitive, the variable names are usually in UPPER CASE<\/strong> and the variable values are in lower case<\/strong>.<\/p>\r\n\r\n\r\n\r\nIn docker, if we do not set an environment variable, it will not have any value and docker compose substitutes them with an empty string. When working in docker, sometimes we might need to pass environment information to the operating container. To achieve this, we can employ both ENV and ARG variables. And in this article, we will only show you how to set environment variables in docker.<\/p>\r\n\r\n\r\n\r\n
<\/span>Set Environment Variables in Docker<\/span><\/h2>\r\n\r\n\r\n\r\nTo pass your environment variable to a container, we need to set it first. In this article, we are using Ubuntu, you can follow the demonstration if you are using Linux operating system for your container development.<\/p>\r\n\r\n\r\n\r\n
As previously explained, an environment variable consists of a variable name and its value. Let’s say we are going to create a variable named “POSTGRES_USER” and the variable value set to “masteruser”. To do so we can run this command:<\/p>\r\n\r\n\r\n\r\n
$ export POSTGRES_USER=masteruser\r\n$ export POSTGRES_PASSWORD=m0d1fyth15<\/pre>\r\n\r\n\r\n\r\nTo verify whether the variable is running or not, we can invoke this command:<\/p>\r\n\r\n\r\n\r\n
$ echo $POSTGRES_USER\r\n$ echo $POSTGRES_PASSWORD<\/pre>\r\n\r\n\r\n\r\nBy invoking the command above, you will see the variable value “masteruser”<\/p>\r\n\r\n\r\n\r\n <\/figure>\r\n\r\n\r\n\r\nPass the variable to a container<\/p>\r\n\r\n\r\n\r\n
In the previous section, we showed you how to create an environment variable. Now, there are three ways to set these variables for a docker container: with CLI arguments, use .env file, or through docker-compose.<\/p>\r\n\r\n\r\n\r\n
\r\nCLI arguments<\/li>\r\n<\/ol>\r\n\r\n\r\n\r\nWe can run a command to launch a docker container, docker run as arguments by adding an -e flag, or a shorthand for –env to pass the environment variable<\/p>\r\n\r\n\r\n\r\n
For example, we can run the following command to pass variables to a container.<\/p>\r\n\r\n\r\n\r\n
$ docker run --name postgresql -e $POSTGRES_PASSWORD -e $POSTGRES_USER -d postgres<\/pre>\r\n\r\n\r\n\r\nOnce finished, the container will automatically run. And, you can go to the PostgreSQL console within the container by simply running this command:<\/p>\r\n\r\n\r\n\r\n
$ docker exec -it postgresql psql -U $POSTGRES_USER<\/pre>\r\n\r\n\r\n\r\n <\/figure>\r\n\r\n\r\n\r\n\r\nUse .env file<\/li>\r\n<\/ol>\r\n\r\n\r\n\r\nBesides adding an -e flag in the command, we can also use an .env file to pass the variables.<\/p>\r\n\r\n\r\n\r\n
First, let’s create a .env file using nano as editor, you can use any other editor you like.<\/p>\r\n\r\n\r\n\r\n
$ nano .env<\/pre>\r\n\r\n\r\n\r\nAnd paste the following lines in that file<\/p>\r\n\r\n\r\n\r\n
POSTGRES_USER=masteruser\r\nPOSTGRES_PASSWORD=m0d1fyth15<\/pre>\r\n\r\n\r\n\r\nThen, press CTRL + O and CTRL + X to save and close the editor<\/p>\r\n\r\n\r\n\r\n
Next, we can run the command below to pass the variables in the .env file we just created<\/p>\r\n\r\n\r\n\r\n
$ docker run --name postgresql --env-file .env -d postgres<\/pre>\r\n\r\n\r\n\r\n <\/figure>\r\n\r\n\r\n\r\n\r\nDocker compose<\/li>\r\n<\/ol>\r\n\r\n\r\n\r\nSome people prefer not to launch Docker containers directly with the docker run command. They opt to use a docker-compose file instead to pass the environment variables.<\/p>\r\n\r\n\r\n\r\n
Using this option, you will need to configure the docker compose file to pass the session\u2019s variables through to the Docker container. This configuration here passes the POSTGRES_USER variable to both the built environment and the runtime environment and sets a default value if it does not exist.<\/p>\r\n\r\n\r\n\r\n <\/figure>\r\n\r\n\r\n\r\n