NodeJS and docker development environment setup

Martin
DevOps Dudes
Published in
3 min readMay 21, 2020

This short project demonstrates how to set up the environment for development with NodeJS and Docker. I will be preparing the minimal environment that will get you started with developing with docker. Building apps using containers from the start is like starting the day on the right foot. The colors are brighter and the project is one step closer to production. Nobody ever liked fixing dependencies, especially at the end of the project timeline. Do yourself a favor and start all your new projects in containers.

I will be building a user authentication and registration system and deploying everything using docker tools. Let me know in the comments below, what would you like to see next.

NodeJS

NodeJS has proven to be a very versatile open-source cross-platform JavsScript runtime environment.

Server-side scripting is the feature we are going to use to develop this application.

docker

We will be developing using containers. Developing applications with containers from the start is the best choice you can make for starting a cloud, hybrid, and even non-cloud applications. Having docker also makes the application resilient because we will be running the app with the same container base image as in production. You can use the same development environment for developing any other application in NodeJS or continue where I left off.

What you need

You need a Docker environment on your computer and a text editor of your choice. I will be using a Macintosh computer. Any other OS should work fine.

Install Docker here

The result

The result is the environment that puts you on track to deploy your code in the container. You will be able to instantly change the code without waiting for the container to spin up. Also, you will be able to interact with the code through the browser. You can continue on your own or follow me with building the next steps.

In a new folder we initialize the project using `npm init`. This creates a package.json file that is crucial for the project.

Place the server.js file in the root of your project:

Create a folder called “views” and in there create index.ejs:

Lastly, create docker-compose.yaml file in the root directory:

The folder structure should look like this:

project   
| README.md
│ docker-compose.yaml
| server.js
| package.json
|
└───views
│ │ index.ejs

Compile and run the project with:

> docker-compose up

Visit your localhost page with your browser to find the project’s interface:

http://localhost:3001/

Congrats! The environment is set up for development. You can make a change in the existing files and save. The container will automatically start running with the new code in place.

If you are interested in how to build and publish a container image to the registry and towards production, there is an article for that: https://medium.com/@martinzim/nodejs-app-building-and-publishing-to-the-registry-with-docker-b7a53aac4cb6

Also, clap if you think this content is useful.

--

--