Lets create simple node app

mkdir app && cd app
npm init -y -s
npm i --save express
touch index.js

Now open index.js in your favourite text editor, and paste the sample code of this express app that listens to the http calls on the PORT set as environment variable and return the VERSION value, also read from environment in JSON format.

const express = require('express');
const app = express();

const PORT = process.env.PORT;
const VERSION = process.env.VERSION;

app.get('/health', (req, res) => {
   res.send({
       version: VERSION
   });
});

if(!PORT) {
   throw new Error(`No port specified`);
}

app.listen(PORT, () => {
    console.log(`App listening on port ${PORT}`);
});

Now it's possible to test the app. Run in one terminal window the node with this file and environment variables.

PORT=1234 VERSION=a node index.js 

In another terminal do the curl call to the specified port.

curl localhost:1234/health

After ensuring that everything works it's time to create the blueprint of docker image called Dockerfile - the file without any extension.

touch Dockerfile

In Dockerfile specify the parent image by FROM command, COPY some source files needed to setup the project, RUN the

FROM node:12-alpine

COPY index.js .
COPY package.json .

RUN npm i

CMD ["node", "index.js"]

After the Dockerfile is ready - we can built an image from it. Also it would hendy to set a tag for it, by flag -t otherwise the docker will assign some human unfriendly name to it.

docker build . -t test-app

Now having the docker image called test-app we can try running it using docker run but we have to specify env variables using parameter -e, and forward ports using -p.

docker run -e PORT=1234 -e VERSION=dockerised -p 4321:1234  -d test-app 

Here's an example of testing the image running using curl again.

curl localhost:1234/health

So now our app is dockerised and ready to ship.

Nice article about Dockerfiles
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/