dockerize-your-development-environment-for-nodejs – klamser


Dockerize Your Development Environment For Node.js

Docker is a platform that can run containers, packages of software. To run these containers Docker uses OS-level virtualization. You can think of a container as a lightweight version of a virtual machine.

Getting Started:

We start out by creating a new folder in which we place our project, and we create our Dockerfile like this:


$ mkdir node-docker && cd node-docker

$ touch Dockerfile


FROM node:

latest WORKDIR /usr/src/app

COPY package*.json ./

ENV PORT 5000 

RUN npm cache clear –force && npm install


Tells Docker to get an image called node (version: latest) from the docker hub.


Sets the directory in which all the upcoming commands will be executed.


Does exactly what it says, it gets the package.json and package-lock.json and copies it to the WORKDIR.


Sets an environment variable inside the container with the name PORT and the value 5000


Executes the commands we pass in. In this case, clearing the npm cache and then installing all the dependencies from package.json.


Executes the command you insert here, right when the docker container is started

Simple Express App:

Now that we have our Dockerfile ready to go we need a simple express application that we can run inside a container. For that, we create two new files like this:

$ touch server.js package.json

package.json will get two dependencies, first express, and second nodemon:

“name”: “node-docker”,  “version”: “1.0.0”, 

“description”: “”, 

“main”: “server.js”, 



“start”: “nodemon server.js” 


“author”: “Jakob Klamser”, 

“license”: “MIT”, 

“dependencies”: {    “express”: “^4.17.1”  },

  “devDependencies”: { 

   “nodemon”: “^2.0.4” 



The express application will just return simple HTML when hitting the main page. Therefore server.js should look like this:

const express = require(‘express’); 

const app = express(); 

const PORT = process.env.PORT || 5000; 

app.get(‘/’, (req, res) => { 


`    <h1>Express + Docker</h1>  

<span>This projects runs inside a Docker container</span> 



app.listen(PORT, () => { 

console.log(`Listening on port ${PORT}!`);


Copyright @2023 . klamser  . All Rights Reserved .