Docker - An introduction with Nodejs
Tags: docker, node, tutorial, beginners
Published: 2020-08-10 00:25:00

You must have heard of the popular [docker](https://www.docker.com/) 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 🤨

```
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](https://dev-to-uploads.s3.amazonaws.com/i/a6dtyzakxjdovdjb013r.JPG)

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](https://docs.docker.com/engine/)
* 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](https://dev-to-uploads.s3.amazonaws.com/i/h5sal17gi7ddab9qjle7.JPG)

*If yours isn't enabled, [this article](https://support.bluestacks.com/hc/en-us/articles/115003174386-How-can-I-enable-virtualization-VT-on-my-PC-#%E2%80%9C20%E2%80%9D) shows how to enable virtualization*

B. **For those using windows 8 or earlier, please use [docker toolbox](https://docs.docker.com/toolbox/toolbox_install_windows/#how-to-uninstall-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](https://dev.to/ebereplenty/nodejs-code-structure-optimization-with-express-routing-5fna)". 

* Let's get started by cloning the starter project [from github](https://github.com/EBEREGIT/server-tutorial)

* Follow the [instructions in the Readme](https://github.com/EBEREGIT/server-tutorial#setting-up) 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](https://dev-to-uploads.s3.amazonaws.com/i/htih1iqaqikr0b0z6ohn.JPG)

* 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

```javascript

# use docker node 10
FROM node:10

```
* Below, enter the following code to create a directory for the docker application

```javascript

# create a directory to run docker
WORKDIR /app

```

* Copy ``package.json`` file into the ``/app`` directory with the following code

```javascript

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

```

* Install the dependencies of the project in the docker application with these code

```javascript

# install the dependencies
RUN npm install

```

* Now copy every other file and folder from your project into the docker ``/app`` directory. Use these code:

```javascript

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

```

* Specify the port at which the docker app will run by exposing or opening a port with the following code

```javascript

# open port 5000
EXPOSE 5000

```

* Run the docker app with the following code

```javascript

# run the server
CMD node index.js

```

*Our ``Dockerfile`` now looks like this:*

```javascript

# 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

```

#### Build the Docker App

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

```javascript

docker build -t docker-node-app .

```
Your terminal should look like this:

![Docker Build Image](https://dev-to-uploads.s3.amazonaws.com/i/h58e0fg01juqxbars7g1.JPG)

*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:

```javascript

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

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g4v4fdz402aj9e2p9tyz.JPG)

*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](https://dev-to-uploads.s3.amazonaws.com/i/cupfgoq8lww3dxld0938.JPG)

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

```
docker ps

```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8fw1p9d0j5p3aytocpsi.JPG)


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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nsxacmflzqa3xq03kio1.JPG)

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](https://github.com/EBEREGIT/server-tutorial) 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](https://hub.docker.com/). 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](https://hub.docker.com/_/node).

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.🤗