Table of Contents
This tip is about the how to pass environment variables to Docker containers. So read this free guide, How to pass environment variables to Docker containers step by step. If you have query related to same article you may contact us.
How to pass environment variables to Docker containers – Guide
Did you know that you can pass your host’s environment variables to Docker containers? Using this feature, you can make the development of these containers a little more efficient. But before we get into the how of this, we need to address what – as in, what are environment variables?
Environment variables are dynamically named values that can be stored and then passed to services, applications or scripts. This is an easy way to store a value in a centralized location (usually in memory) and use it globally. To learn more about what environment variables are, see “Linux 101: What are environment variables?”
These types of variables are incredibly useful. trick To have up your sleeve. And if you’re a container developer, they can help make your job a little easier.
what will you need
The only thing you need to pass environment variables to Docker containers is a running Docker instance and a user who is part of the docker group. That’s it. Let’s pass some variables.
How to set an environment variable
To pass an environment variable to a container, we first have to set it. I will demonstrate this on Linux. If you use a different operating system for container development, you will need to find out how to do the same on the platform of your choice.
Let’s say we want to set the variable to a database user and we plan to use that variable for different containers. We could define a variable called DB_USER, which would be workable for any container using any type of database. Let’s say the value for DB_USER is TechRepublic. To set this variable, we would issue the command:
export DB_USER = TechRepublic
To check if the variable has been defined, issue the command:
echo $DB_USER
You should see TechRepublic printed on the terminal. That’s it, you defined your variable. Let’s go a step further (for example purposes) and also set a password as an environment variable. You wouldn’t do this in production, but it’s a good way to illustrate how this is done. Set an environment variable for the password with:
export DB_PWORD = T3chR3public
How to pass the variable to a container
Now that you understand how environment variables work, you can see how easily they can be passed to their containers. First I will demonstrate how to do this from the docker command line and then using an .env file.
Unlike using environment variables in Linux, you cannot set them on the host and then pass those set variables to the container in the same way as you would on the host system. In another words, you can’t use the variables we just defined with the Docker command, such as:
docker run –name postgresql -e $ DB_PWORD -e $ DB_USER -d postgres
If you try to deploy the container as such, it will run but exit immediately. Why? Because unlike the Linux system, where you can set environment variables as you like, container images expect certain variables. For example, PostgreSQL database cannot use DB_PWORD or DB_USER as it expects POSTGRES_PASSWORD and POSTGRES_USER. To do this, you can set these environment variables on your Linux hosts with the commands:
export POSTGRES_PASSWORD = t3chr3public
export POSTGRES_USER = TechRepublic
OK, now we can run the same command with:
docker run –name postgresql -e POSTGRES_PASSWORD -e POSTGRES_USER -d postgres
The command will succeed and the container will remain running. You can test it by accessing the PostgreSQL command inside the container, issuing:
docker exec -it postgresql psql -U $ POSTGRES_USER
You should find yourself on the PostgreSQL console in your container.
How to pass variables with an .env file
One of the problems with passing environment variables as described above is that they remain in memory (until you unset them). To avoid this, we use an environment variable file.
Let’s stick with the same variables as we used above. Create a new .env file with the command:
nano .env
In this file, paste the following:
POSTGRES_PASSWORD = t3chr3public
POSTGRES_USER = TechRepublic
Save and close the file.
Now how do we pass these variables? Simple, we would issue the command:
docker run –name postgresql –env-file .env -d postgres
Be sure to use the full path to the .env file (if you are not running the docker command in the same directory that contains the file). Your container will be deployed and ready to use. You can test it out by accessing the PostgreSQL console. The only difference is that you need to manually enter the user (since we haven’t set the POSTGRES_USER variable on the host system). This command would be:
docker exec -it postgresql psql -U TechRepublic
And this is how you pass environment variables to a Docker container, either from the command line or using an .env file. Fortunately, you can use this method in your developer workflow to make things a little more efficient.
From the news www.techrepublic.com
Final note
I hope you like the guide How to pass environment variables to Docker containers. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.