DEV Community

Tirtha Guha
Tirtha Guha

Posted on

Setting environment variables to your container

In this post, we'll learn how to set environment variables in docker containers. Often things like password, environment configurations, API credentials etc are not checked in the repositories as a security measure. These are set in environment variables, from where the application reads it during the runtime.

Create a sample application

For this, we'll create a simple express application which will read the environment variable and show it on the browser

  1. run npm init to create a package json
$ mkdir test-app
$ cd test-app
$ npm init --y
$ npm install express --save
$ touch app.js
Enter fullscreen mode Exit fullscreen mode
  1. Open app.js and type in the following
// app.js
var express = require("express");
var app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.get("/", function (req, res, next) {
  res.send({ title: "Express", envVal: process.env.TESTVAL });
});

app.listen(3000, () => console.log("Running on http://localhost:3000"));
Enter fullscreen mode Exit fullscreen mode
  1. add the start script in package.json
{
  "name": "express-with-env",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "~4.16.1"
  }
}

Enter fullscreen mode Exit fullscreen mode
  1. Run the application

I'm using ubuntu v18 for development. If you're on windows, you may need to set the env variable in a slightly different way.

$ TESTVAL=QWERTY npm start
Enter fullscreen mode Exit fullscreen mode

Now if you open http://localhost:3000 in the browser, you would see

{
"title": "Express",
"envVal": "QWERTY"
}
Enter fullscreen mode Exit fullscreen mode

Create the docker image

Let's create a Dockerfile, parallel to your packageJSON

$ touch Dockerfile
$ touch .dockerignore
Enter fullscreen mode Exit fullscreen mode

Lets now open Dockerfile and add these entries into it.

FROM node:12-slim
WORKDIR /app
COPY ./package*.json ./
RUN npm install
COPY ./ ./
EXPOSE 3000

# Run the code
CMD [ "npm", "start" ]
Enter fullscreen mode Exit fullscreen mode

Build the docker image from the Dockerfile

$ sudo docker build -t test-image .
Enter fullscreen mode Exit fullscreen mode

Finally run the image with an entrypoint

$ sudo docker run -e "TESTVAL=Docker with env variable" -p 3000:3000 -d test-image
Enter fullscreen mode Exit fullscreen mode

if you now open http://localhost:3000 in a browser, you'll get

{
"title": "Express",
"envVal": "Docker with env variable"
}
Enter fullscreen mode Exit fullscreen mode

Let spin up another container from the same image

$ sudo docker run -e "TESTVAL=Another Docker Container from the same image" -p 4000:3000 -d test-image
Enter fullscreen mode Exit fullscreen mode

if you now open http://localhost:4000 in a browser, you'll get

{
"title": "Express",
"envVal": "Another Docker Container from the same image"
}
Enter fullscreen mode Exit fullscreen mode

PS:

Often enterprises have multiple prod and non-prod environments to support. Each environment often have different configurations, but your code must follow the strategy build once deploy anywhere. With this tutorial, you'll be able to do that effectively.

Top comments (0)