Cloud Open source

How to add environment variables to a container with Portainer

Jack Wallen reveals you the way straightforward it’s to use environment variables on your containers with the assistance of the Portainer administration platform.

shipping containers
Image: iStockphoto/wissanu01

Environment variables are an vital piece of the container puzzle. With this function, you may outline crucial variables for the containers you deploy. For occasion, say you’re going to deploy a full-stack utility that is dependent upon MySQL. You would possibly need to have the opportunity to outline issues like databases, passwords and customers for that deployment. To do this, you employ environment variables.

SEE: Hiring equipment: Back-end Developer (TechRepublic Premium)

When environment variables are utilized in a command-line deployment (similar to while you use Docker-compose), they’ll be outlined in a .env file. Within the .env file, variables are outlined in key-value pairs, like so:

VARIABLE1_NAME=some worth
VARIABLE2_NAME=one other worth

You can add as many key-value pairs as you want within the .env file. When you deploy your container with Docker-compose, it’ll learn the .env file and apply the key-value pairs as wanted.

With Portainer, that is all dealt with inside a highly effective web-based GUI, so there’s no want to create a .env file manually.

I would like to present you the way to make use of the environment variables function in Portainer.

What you’ll want

The solely factor you’ll want to make this work is a operating occasion of Portainer, which may simply be deployed with the assistance of Docker.

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

How to create a container with environment variables

Let’s use MySQL for example of assigning environment variables. Log into Portainer and click on Containers within the left navigation. In the ensuing web page (Figure A), click on Add Container.

Figure A

My list of containers grows every day.
My checklist of containers grows on daily basis.

In the Create a Container window (Figure B), give the container a identify after which sort mysql within the Image subject.

Figure B

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

Next, scroll down and click on the Env tab. Here, click on 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 web page.

For our first environment variable, we’re going to set the basis password with the MYSQL_ROOT_PASSWORD key and set the worth to a sturdy/distinctive password. Again, click on Add an environment variable and set the MYSQL_DATABASE key to a worth giving the database a identify (similar to trtestdb). Add one other environment variable with the important thing MYSQL_USER. Set this worth to one thing like trtestuser. Finally, add one other environment variable with a key of MYSQL_PASSWORD key and a worth with a completely different sturdy/distinctive password.

The full checklist of key-value pairs ought to look one thing like that in Figure D.

Figure D

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

Once you’ve stuffed out the key-value pairs, scroll again up and click on Deploy the Container. The deployment ought to occur pretty shortly, and also you’ll wind up with a operating occasion of MySQL that has your whole customized environment variables assigned. If you click on on that container, you may scroll down and see everything 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 additionally see some environment variables that have been added by the container picture alongside with these we set.

And that, docker builders, is the way you apply environment variables from inside the Portainer web-based GUI.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the newest tech recommendation for enterprise execs from Jack Wallen.

admin

Leave a Reply

Your email address will not be published.Required fields are marked *

*