How to add environment variables to a container with Portainer

Jack Wallen shows you how easy it is to use environment variables for your containers with the help of the Portainer management platform.

Image: iStockphoto/wissanu01

Environment variables are an important piece of the container puzzle. With this feature, you can define necessary variables for the containers you deploy. For instance, say you’re going to deploy a full-stack application that depends on MySQL. You might 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 (TechRepublic Premium)

When environment variables are used in a command-line deployment (such as when you use Docker-compose), they’ll be defined in a .env file. Within the .env file, variables are defined in key-value pairs, like so:

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’ll read the .env file and apply the key-value pairs as needed.

With Portainer, this is all handled within a powerful web-based GUI, so there’s no need to create a .env file manually.

I want to show you how to make use of the environment variables feature in Portainer.

What you’ll need

The only thing you’ll need to make this work is a running instance of Portainer, which can easily be deployed with the help of Docker.

That’s it. Let’s get our environment variables on.

How to create a container with environment variables

Let’s use MySQL as an example of assigning environment variables. Log into Portainer and click Containers in the left navigation. In the resulting page (Figure A), click Add Container.

Figure A

My list of containers grows every day.
My list of containers grows every day.

In the Create a Container window (Figure B), give the container a name and then type mysql in the Image field.

Figure B

Creating a new MySQL container in Portainer.
Creating a new MySQL container in Portainer.

Next, scroll down and click the Env tab. Here, click Add an environment variable (Figure C).

Figure C

The Environment Variables tab on the container creation page.
The environment variables tab on the container creation page.

For our first environment variable, we’re going to set the root password with the MYSQL_ROOT_PASSWORD key and set the value to a strong/unique password. Again, click Add an environment variable and set the MYSQL_DATABASE key to a value giving the database a name (such as trtestdb). Add another environment variable with the key MYSQL_USER. Set this value to something like trtestuser. Finally, add another environment variable with a key of MYSQL_PASSWORD key and a value with a different strong/unique password.

The full list of key-value pairs should look something like that in Figure D.

Figure D

Our list of environment variable key-value pairs is ready to go.
Our list of environment variable key-value pairs is ready to go.

Once you’ve filled out the key-value pairs, scroll back up and click Deploy the Container. The deployment should happen fairly quickly, and you’ll wind up with a running instance of MySQL that has all of your custom environment variables assigned. If you click on that container, you can scroll down and see the entirety of the variables listed (Figure E).

Figure E

Here you also see some environment variables that were added by the container image along with those we set.
Here you also see some environment variables that were added by the container image along with those we set.

And that, docker developers, is how you apply environment variables from within the Portainer web-based GUI.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.