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 🤨
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.
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.
The image below is simply the reason why we should use docker.
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.
- 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
Task Managerin the menu that follows
- Click on the
performancetab in the windows dialogue box that pops up. This is mine
If yours isn't enabled, this article shows how to enable virtualization
B. For those using windows 8 or earlier, please use docker toolbox
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
- Next, create a file in the root directory and name it
Dockerfilewith no extension.
- In the file, Enter the following code to specify the
docker nodewe are using
# use docker node 10 FROM node:10
- Below, enter the following code to create a directory for the docker application
# create a directory to run docker WORKDIR /app
package.jsonfile into the
/appdirectory with the following code
# copy package.json into the new directory COPY package.json /app
- Install the dependencies of the project in the docker application with these code
# install the dependencies RUN npm install
- Now copy every other file and folder from your project into the docker
/appdirectory. Use these code:
# 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
# open port 5000 EXPOSE 5000
- Run the docker app with the following code
# run the server CMD node index.js
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
- To build the docker app, type in the following command in the terminal and hit the
docker build -t docker-node-app .
Your terminal should look like this:
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
- Finally, run the docker app with the following command in the terminal:
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
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.
Our Docker app now runs on port 5000 while the original app runs on port 3000. Check your browser
To see all your docker apps running, use the following command in a new terminal
If you also check your docker dashboard, you will see your docker app running like mine below:
YESSS!!! Congratulations 🥳 on creating your first deployment with docker.
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.🤗