DEV Community

NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on

Docker - An introduction with Nodejs

You must have heard of the popular docker which has made life easy for developers across the globe.

Have you heard a conversation like this?

Developer 1: It doesn't work on my computer ๐Ÿ˜’

Developer 2: Ahhh! But it works on mine perfectly ๐Ÿคจ

Enter fullscreen mode Exit fullscreen mode

It's not an uncommon conversation. Usually, this is caused by difference in working environment setup or configuration. This is the main purpose for why docker is used today.

In this tutorial, I will teach you what docker is, why use it and how to use it on a nodejs app.

What is Docker?

Normally, the definition is:

Docker is a containerization platform which is used for packaging an application and it's dependencies together to ensure the effortless functioning of the application irrespective of the working environment.

Well, those lines are just telling us that:

Docker is a tool designed to make it easy to create, deploy and run application by using containers.

Why use Docker?

The image below is simply the reason why we should use docker.

Alt Text

You can say that Docker ships your machine alongside you code so that when your team mates get your code, they also get your machine configurations. Since the code is working on your machine with those configurations, it will definitely work in their machine because they have your configurations.

The Time that would have been used to start setting up that project on a new machine can now be put into something more productive or essential.

How to use Docker?

Installation

  • Please visit Docker Website
  • In the side menu, checkout how to install docker for your machine under Docker Desktop tab

Note For windows users
A. You need to enable virtualization in your computer. To check if virtualization is enabled, follow these steps:

  • On your keyboard Press ctrl + alt + del
  • Select Task Manager in the menu that follows
  • Click on the performance tab in the windows dialogue box that pops up. This is mine

Alt Text

If yours isn't enabled, this article shows how to enable virtualization

B. For those using windows 8 or earlier, please use docker toolbox

Dockerizing a Nodejs Application

Ensure that you have started your docker and it is set to running in order for you to see the changes or docker effect.

The project that we will be using for this tutorial is the project we made in the article: "Nodejs Code Structure Optimization With Express Routing".

  • Let's get started by cloning the starter project from github

  • Follow the instructions in the Readme to setup the project.

  • If your project setup is complete and your server is now running, you should be getting the following response in your browser

Alt Text

  • Next, create a file in the root directory and name it Dockerfile with no extension.

Configuring Dockerfile

  • In the file, Enter the following code to specify the docker node we are using

# use docker node 10
FROM node:10

Enter fullscreen mode Exit fullscreen mode
  • Below, enter the following code to create a directory for the docker application

# create a directory to run docker
WORKDIR /app

Enter fullscreen mode Exit fullscreen mode
  • Copy package.json file into the /app directory with the following code

# copy package.json into the new directory
COPY package.json /app

Enter fullscreen mode Exit fullscreen mode
  • Install the dependencies of the project in the docker application with these code

# install the dependencies
RUN npm install

Enter fullscreen mode Exit fullscreen mode
  • Now copy every other file and folder from your project into the docker /app directory. Use these code:

# copy all other files and folder into the app directory
COPY . /app

Enter fullscreen mode Exit fullscreen mode
  • Specify the port at which the docker app will run by exposing or opening a port with the following code

# open port 5000
EXPOSE 5000

Enter fullscreen mode Exit fullscreen mode
  • Run the docker app with the following code

# run the server
CMD node index.js

Enter fullscreen mode Exit fullscreen mode

Our Dockerfile now looks like this:


# use docker node 10
FROM node:10

# create a directory to run docker
WORKDIR /app

# copy package.json into the new directory
COPY package.json /app

# install the dependencies
RUN npm install

# copy all other files into the app directory
COPY . /app

# open port 5000
EXPOSE 5000


# run the server
CMD node index.js

Enter fullscreen mode Exit fullscreen mode

Build the Docker App

  • To build the docker app, type in the following command in the terminal and hit the Enter key

docker build -t docker-node-app .

Enter fullscreen mode Exit fullscreen mode

Your terminal should look like this:

Docker Build Image

In the command above, docker-node-app is the name of the docker app we are creating. So yours might vary. Also, do not forget the period (.) at the end

Run the Docker App

  • Finally, run the docker app with the following command in the terminal:

docker run -it -p 5000:3000 docker-node-app

Enter fullscreen mode Exit fullscreen mode

It will read exactly the same message like the normal app but this time around, it loads on port 5000

Alt Text

In the command above, we are telling docker to run the app we built on port 5000 even though, our main app runs on port 3000. Hopefully that makes sense.

Result

Our Docker app now runs on port 5000 while the original app runs on port 3000. Check your browser

Alt Text

To see all your docker apps running, use the following command in a new terminal

docker ps

Enter fullscreen mode Exit fullscreen mode

Alt Text

If you also check your docker dashboard, you will see your docker app running like mine below:

Alt Text

YESSS!!! Congratulations ๐Ÿฅณ on creating your first deployment with docker.

Conclusion

In a fast paced system, the importance of docker for efficiency cannot be overly emphasized. Hence, the need to learn it.

The github repo has been updated with the dockerfile. Do check it out and leave a star. ๐Ÿ˜Š

Most of the code we used are found on docker hub. Many companies like Microsoft, mongoDB, PHP etc has already made code (or images) for these things so all you need is to make your own copy. I encourage you to check it out.

These configuration are called images. For example, the node image that we used is found here.

I hope you enjoyed this tutorial as much as I enjoyed making it. I make weekly articles so see you next week.

Thank you for stopping by.๐Ÿค—

Top comments (6)

Collapse
 
teclado profile image
Teclado

How do you manage to track changes without using volumes?

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Hi Teclado, I am not sure I have tried that before

Collapse
 
teclado profile image
Teclado

Then, every time you make any change and want to run the app, you have to rebuild the whole container? May not be very much work, but I'd prefer to use volumes. I asked because I've had some problems with docker volumes in Windows: At least a couple of months ago, you had to name them and access via other container that makes a bridge to use with SMB.

Thread Thread
 
ebereplenty profile image
NJOKU SAMSON EBERE

Oh... That sounds nice. I will check it out. Thanks

Collapse
 
andrewbaisden profile image
Andrew Baisden

Cool article how often do you use Docker though? Is it with every project?

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Thank you for reading

If I will be collaborating with someone, I try to use docker.