Pass Environment variables to a NodeJS Project & deploy docker image to Kubernetes


I started learning Kubernetes a few days back and wanted to make a small project to test how to pass the env variables from Kubernetes pod to a docker image. Through this we will learn

  • How to make a NodeJS docker image.
  • Push the image to container registry.
  • Use the image in your Kubernetes cluster.
  • Pass the environment variables from Kubernetes pod manifest to your docker image.



  1. Initialize a NodeJS project

Make a new folder, navigate to that folder in your terminal. Type following commands to initialize a node project

npm init -y

To install express as a dependency:

npm install express

To use environment variables we install dotenv. We are using it as a dev-dependency as it’s only required during development.

npm install — save-dev dotenv

2. Create index.js

3. Create Dockerfile

This uses latest node image. Line 3 and 4 is to provide caching. Line 6 tells us to run cmd node index.js to run our image in container.

4. Make a .env file & add the following line


Restart server with command node index.js. If you now go to localhost:3000 we see Hello Yash

5. Build Docker image

We now build the docker image and run it to view the application on localhost:3000

docker build -t nodeProject:latestdocker image ls  //optional: To view your image docker run — name nodeProject -d -p 3000:3000 nodeProject

Before proceeding to next step make sure you have:

  • An account at docker hub
  • After creating the account make a new repository.

5. Push to DockerHub

Now we tag the image as latest & push it to DockerHub.

docker tag nodeProject:latest <dockerUsername>/<repoName>:latestdocker logindocker push <dockerUsername>/<repoName>:latest

In this tutorial I’m going to use minikube to deploy the image on Kubernetes cluster

6. Create pod.yaml file

We define the image that we pushed to DockerHub. You can use my docker image for testing. We define containerPort as 3000 and environment variable with key as NAME and its value as “Yash”

7. Create Kubernetes cluster

minikube startkubectl config use-context minikube

8. Deploy to Kubernetes

Apply the changes to minikube cluster. Make sure pod.yaml is in the current working directory.

kubectl apply -f pod.yaml
  • Check to see if the pod is up & it’s status
kubectl get pods
  • Port forward the pod to view in localhost
kubectl port-forward demo 3000
  • Go to localhost:3000 to see the env variable we passed in yaml file

The environment variables we passed were used the container. Similarly we can change the value or add more variables and use them in our NodeJS project.


In this post we created a NodeJS project, added a Dockerfile, created the docker image & pushed it to DockerHub. We then used the same image in our pod manifest & deployed it to Kubernetes cluster. The environment variable passed through the pod manifest were used by the container and we were able to view it by port forwarding the port on localhost. The Environment variables are configurable and we can add more than one variable to the manifest.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store