Node JS and Docker : How to Guide PART ONE

Node JS and Docker : How to Guide PART ONE

Hi, this is my first blog post, Ye !!! I'm so happy to be able to share my back-end development journey with you.

Today I'll be talking about Node JS and Docker, we'll learn from the basics of docker to running your own docker container. Requirements: Knowledge of Node JS (just so you can understand my code).

Let's Go ...

Before we get into creating the docker configuration file, we'll do two things:

  1. Explain what docker is
  2. Create a Node JS server with nib-cli

What is Docker ?

Docker is a platform as a service (PAAS) that makes it easy to create containers and container based apps. What is a container ? a container is a way to package application with all the necessary dependencies and configuration. Containers live in a container repo, AWS ECR is an example, docker hub is another example.

We won't be talking much about docker, lets get into into configuration.

First, create your node js server using nib-cli, create a folder called dockerxnodejs, open terminal in that folder and :

npx nib-cli --package yarn --server express

Once it's done, open the folder on your text editor and start the server, you'll have an express API running on port 3990. To better structure our folder, create a folder inside dockerxnodejs called app and move all server files and folder into app.

Inside dockerxnodejs folder, create a file named Dockerfile (Make sure it's spelt like this), copy and paste the following code into Dockerfile:

What each line of code in our Dockerfile means:

The first thing we need to do is to add a line in our Dockerfile that tells Docker what base image we would like to use for our application.

FROM node:12.18-alpine

Then we add enviromental variables that can be used in our applications and yaml configuration file.

ENV NODE_ENV=production

The next step is to declare our work directory, where our app image will be. This instructs Docker to use this path as the default location for all subsequent commands. This way we do not have to type out full file paths but can use relative paths based on the working directory.

WORKDIR /usr/src/app

We have to copy our package.json file into our image so we can install all our npm packages, without that, our application wont work.

We are doing this because copying node_modules is tedious and nobody should ever copy node modules from their laptop to a server (Node JS law: Chapter 1 Section 24)

COPY ["./app/package.json", "./app/package-lock.json*", "./"]

Create a .dockerignore file and paste the following code in it.

**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
README.md

Now we install our npm modules with the RUN command, the RUN command runs linux command on a terminal.

RUN npm install --production

Next step is to copy all our files into the working directory.

COPY ./app .

Then the last line is our favorite, it's same as node index.js, that's what the CMD command does, it serves our application.

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

The full code below.

FROM node:12.18-alpine

ENV NODE_ENV=production

WORKDIR /usr/src/app

COPY ["./app/package.json", "./app/package-lock.json*", "./"]

RUN npm install --production 

COPY ./app .

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

Now to the final part, open your terminal in dockerxnodejs and type the following command,

docker build -t myapp:1.0 .

This will build the docker image to docker hub so we can start the container app. In the terminal after the command has executed, run the command:

docker image images

You'll see the list of all your docker images including the "myapp" you just uploaded, to know which container is currently running, use the command:

docker ps

this will show you that our just deployed container is not running yet.

Now we can start our just uploaded docker container app by typing in our terminal,

docker run myapp:1.0

then open another terminal and type:

docker ps

We have come to the end of this post, in our next post we'll be talking about docker yaml config and exposing our docker container app to the public.

Hi, my name is Chuks, I'm a backend developer skilled in Node JS, C#, Docker and Kubernetes and I'm available for remote, contract and full time(relocation offers), send an email to chuksgpfr@gmail.com, follow me on twitter ChuksGPFR.