Now Reading
Portainer – How to add environment variables in a container
[vc_row thb_full_width=”true” thb_row_padding=”true” thb_column_padding=”true” css=”.vc_custom_1608290870297{background-color: #ffffff !important;}”][vc_column][vc_row_inner][vc_column_inner][vc_empty_space height=”20px”][thb_postcarousel style=”style3″ navigation=”true” infinite=”” source=”size:6|post_type:post”][vc_empty_space height=”20px”][/vc_column_inner][/vc_row_inner][/vc_column][/vc_row]

Portainer – How to add environment variables in a container

shipping containers

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

shipping containers
Image: iStockphoto/wissanu01

Environment variables are an important part of the container puzzle. This feature allows you define the variables required for the containers that you deploy. For instance, say you’re going to deploy a full-stack application that depends on MySQL. You might need to be able set up databases, passwords, and users to enable this deployment. Environment variables can be used to do this.

SEE: Hiring kit: Backend 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. Variables are defined in key-value pairs within the.env files.

VARIABLE2_NAME=another value

You can include as many key-value pair as you want in the.env files. 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 will show you how to use the Portainer environment variables feature.

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 that has environment variables

Let’s use MySQL as an example of assigning environment variables. Log in to Portainer and click Containers on the left navigation. The page that results will be displayed (Figure AClick Add Container.

Figure A

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

In the Create a container window (Figure BGive the container a name, then type mysqlIn the Image field.

Figure B

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

Scroll down and click on the Env tab. Click here to add an environment variableFigure C).

Figure C

The Environment Variables tab on the container creation page.
The environment variables tab can be found on the container creation webpage.

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. Click on Add an environment variable. This will set the MYSQL_DATABASE to a value that gives the database a name, such as trtestdb. Add another environment variable using the key MYSQL_USER. Set this value to something similar to trtestuser. Add another environment variable with the key MYSQL_PASSWORD and a value that has a strong/unique password.

The complete list should look something similar to the one in Figure D.

Figure D

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

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. Click on the container to scroll down and view all variables (Figure E).

Figure E

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

Docker developers, this is how to apply environment variables within the Portainer web GUI.

Subscribe to TechRepublic’s How to make YouTube tech workJack Wallen provides the latest tech advice to business pros.

View Comments (0)

Leave a Reply

Your email address will not be published.