Jack Wallen shows you how easy it is to use environment variables for your containers using the Portiner management platform.
Environmental variables are an important part of the container puzzle. This function allows you to define the necessary variables for the containers you deploy. For example, suppose you are going to deploy a full-stack application that relies on MySQL. You may want to be able to define things like databases, passwords, and users for that deployment. To do that, you use environment variables.
SEE: Hiring Kit: Back-end Developer (Tech Republic Premium)
When environment variables are used in a command-line implementation (such as when you use Docker-compose), they are defined in an .env file. Within the .env file, variables are defined in key-value pairs, such as:
VARIABLE1_NAME=some value
VARIABLE2_NAME=another value
You can add as many key-value pairs as you need in the .env file. When you deploy your container with Docker-compose, it reads the .env file and applies the key-value pairs as needed.
Of carrierthis is all handled in a powerful web-based GUI, so there’s no need to manually create an .env file.
I want to show you how to make use of the environment variables in Portainer.
What you need
All you need for this to work is a active instance of Portinerwhich can be easily deployed using Docker.
That is it. Let’s enable our environment variables.
Create a container with environment variables
Let’s use MySQL as an example for mapping environment variables. Log in to Portiner and click on Containers in the left navigation. On the resulting page (Image A), click Add Container.
Image A
In the Create a container window (Figure B), give the container a name, then type mysql
in the Image field.
Figure B
Then scroll down and click the Env tab. Here click Add an environment variable (Figure C†
Figure C
For our first environment variable, we are going to set the root password with the MYSQL_ROOT_PASSWORD key and set the value to a strong/unique password. Click Add an environment variable again and set the MYSQL_DATABASE key to a value that names the database (such as trtestdb). Add another environment variable with the MYSQL_USER key. Set this value to something like trtestuser. Finally, add another environment variable with the key MYSQL_PASSWORD and a value with another strong/unique password.
The full list of key-value pairs should look something like this in Figure D†
Figure D
After you have entered the key-value pairs, scroll up again and click Deploy the container. The implementation should be pretty quick, and you’ll end up with a running instance of MySQL with all your custom environment variables mapped to it. If you click on that container you can scroll down and see all of the variables listed (Figure E†
Figure E
And that, docker developers, is how you apply environment variables from Portainer’s web-based GUI.
Subscribe to TechRepublic’s How to make technology work on YouTube for all the latest technical advice for business professionals from Jack Wallen.