Adding ENV Variables to Your Akash Application

figurestudios
4 min readOct 19, 2021

--

This post is a continuation of the last one where I showed you how to develop and deploy an application on Akash using NodeJS.

So, you have an application ready to go, but you can’t publish it publicly because of all the exposed passwords and secrets hardcoded in the source code.

Say no more! Using ENV variables makes you able to send the secrets only to the provider in question — not to the blockchain, not to Dockerhub, and nobody else.

Explanation

This is continuing off the last post. It will not make sense if you haven’t read that one yet.

Requirements

Adding it in main.js

In NodeJS, ENV variables are always accessible and we don’t need extra libraries unless we want to store them in a file. The following example is how you can access an ENV variable named ENV_TEST and then log it to the console:

let env = process.env.ENV_TEST;

console.log(env);

Keep in mind that it’s usually used for mutable content such as links to keep track of or passwords, but that’s something you’ll have to track in your own program.

Using this example, we can make the ‘Hello World!’ text from last time into anything we want. Find this line from last time:

res.send(‘Hello World!’)

And change it to:

res.send(process.env.OUR_ENV_VARIABLE)

It should look like this in Visual Studio Code:

Current Visual Studio Code

Testing it locally

Luckily, we don’t need to deploy it to Akash to test it every time. You can both test it in Docker and NodeJS — but I recommend the latter for most casual users.

Now, unfortunately, the Node.js command prompt doesn’t work with ENV variables, at least not on my PC, so I have to use Git Bash. To access it, right-click on your ‘hello-akash’ folder and press ‘Git Bash Here’:

Current Prompt

Now, we can run the file with an interchangeable ENV variable with this command:

OUR_ENV_VARIABLE=”Hello Akash!” node main.js

It should look something like this:

Current Git Bash Command Prompt

Now, if we take a look at http://localhost:3000, we should see a page that looks like this:

Current tab at http://localhost:3000

And now you can change that ENV variable every time you restart the application. You can use the same principle for passwords, API keys, secrets, or just about anything you need mutable (and secret) on app launch!

Dockerizing & Deploying

You should already know how to build your Docker image and deploy it as normal from the last tutorial, but you don’t know how to add ENV variables to your Dockerfile nor manifest file just yet.

Adding onto our Dockerfile, we will add this line:

ENV OUR_ENV_VARIABLE = “”

Now the full Dockerfile will look like this:

FROM node:alpine
# This line basically says ‘we need to use this light-weight version of Linux which has Node pre-installed’
EXPOSE 3000
# This line opens port 3000
COPY . /app
# This line copies everything from the directory (.) in to the folder (/app)
WORKDIR /app
# This line says that we want to run all commands within the folder (/app)
ENV OUR_ENV_VARIABLE = “”
# This line lets us define an ENV variable (OUR_ENV_VARIABLE)
RUN npm install express
# This line installs the node package express. This is why it’s important to write down all packages you’re using. Sometimes you’ll need to save the specific versions when you’re installing packages too!
CMD [ “node”, “main.js” ]
# This line runs the command ‘node main.js’

Between the ‘image’ and the ‘expose’ you can see ENV variables set in this example below. The first section is what the ENV variable is called, and the second section of it (after the = sign), is what the value of the variable is.

Example with ENV variables

This is only sent to the provider in question, so you don’t have to worry about it being visible in your Docker Image nor the blockchain.

--

--