Docker- Yep, that whale thing šŸ³

Swapnil Sinha
DataX Journal
Published in
6 min readMar 24, 2023

As a developer, I am sure most of you would have gone through the word named ā€˜Dockerā€™ or ā€˜DevOpsā€™ as you build your application.
What comes to your mind when you hear Docker? Something related to containers in ships, right?
When I came across this word, I read quite a few articles from hashnode, freecodecamp, etc., and my first thought was how it is now such a vital tech for any application developer to know.

So, about the logo, the Docker logo represents software that compiles a large amount of ordered information, indicating its practicality.
This article is suited for readers who know nothing apart from ā€œDocker.ā€ Moreover, the prime focus of this article is to get started with Docker using NodeJS. So, without any further ado, letā€™s get moving!!!

Docker

Why Do you need Docker?

Letā€™s understand its usefulness by taking a little scenario.

When you join a new company, one is provided with a new laptop and a project to which you can start contributing.

Setting up the development environment is the first thing you should do once you join a project. Installing project-specific dependencies, tools, and other resources could be necessary. During this, the mistakes you encounter in the middle can be fixed by reading the documentation, but imagine going with this procedure with a huge team that has lots of participants. Nightmare, right?

Docker has an advantage in this situation. Docker can build container-based programs that work in any environment and contain all the dependencies. Hence, configuring the development environment only requires one command. This is only one of the numerous use cases for Docker.
Furthermore, Docker can help standardize application operations, deploy production, and ship code swiftly.

Before working on Docker, we should understand some fundamentals:

  1. Docker Engine- The Docker engine is an open-source containerization technology used to build a containerizing application. To simplify, the software that hosts the containers is called Docker Engine.
  2. Docker Container- A container is a standard unit of software that packages up the code and all required dependencies needed to run an application on different platforms or computing environments.
  3. Docker Image- A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application. It contains code, runtime, system tools, libraries, and settings.

Docker Hub

Docker Hub is the largest community for Docker images. It enables you to distribute container images within your organization or amid the Docker community. Similar to GitHub, Docker Hub. Here, Docker images reside instead of the projectā€™s code.

How to Containerize a NodeJS Application using Docker

Prerequisite:

  1. A basic understanding of NodeJS Applications
  2. Docker desktop application

You can install the Docker Desktop application by following its original documentation.

You can verify if Docker is installed on your machine by querying its version like this:

docker -v
Checking Docker Version on terminal

Letā€™s start our implementation:
Instead of starting over from scratch, I built a basic Express API that exposes only one endpoint. So, clone the repo in your local editor:

git clone https://github.com/Swap-Nova/Docker-Using-NodeJS

Project Structure:

NodeJS Project Structure

I have created only one endpoint (ā€œ/ā€), and calling it will return ā€œGreeting from 5minslearnā€.

You will need to create a file named ā€œDockerfileā€ in the root directory. It is the default file name for the Docker engine. Paste the following code into the file:

FROM node:latest
WORKDIR /app
COPY . /app
RUN npm install
EXPOSE 8000
CMD ["npm","start"]

Tip: I recommend that you install the Docker extension if you're using VS Code. It will help you with its solid suggestions.

VS Code Extensions

How to Ignore Files so, theyā€™re not copied into the Docker Container

You have to exclude the unwanted files from being copied into the container. The .dockerignore file helps you with that. It works like .gitignore for Git.

.dockerignore file

So now we have completed the Docker Configuration. Let us run the application.

Building Docker Image

We can build the docker image by running the docker build command:

docker build -t image_name:version_number .

image_name indicates the container image name and version_number indicates the image version. The dot (.) at the end indicates that the docker image must be built from the current directory.

In our case, we will write the syntax here:

docker build -t swapnova/node_with_docker:0.0.1 .
Image Cmd

Once we have written the command the Docker Image will now be visible to us in our Docker Desktop.

Docker Desktop- Images

Running Docker Image

After we build the Docker image, the next step is to run it. When an image starts running on a Docker Engine, itā€™ll become a container.
The syntax for running the image which we have built in our previous step:

docker container run -d --name <name_of_app> -p <local_port>:<docker_port> <image_name>:<version>

In our case:

docker container run -d --name docker_with_node -p 8000:8000 swapnova/node_with_docker:0.0.1

Now the container will be visible to us in our Docker Desktop as well:

Docker Desktop- Containers

The Dashboard shows that our app is running. Letā€™s check the output on the browser by trying to access the ā€œ/ā€ endpoint. Hit locahost:8000/ on your browser. You should see a message similar to the one in the below screenshot:

Output after going to localhost:8000/

You have successfully run an app with a Docker container.

Push the Image to Docker Hub

After all the work, we can finally push the code to Docker Hub.
Remember to create your profile on Docker Hub.
After creating your profile, it is time to run the cmd below and login through the terminal:

docker login

After a successful login, you can push the image to the Docker hub.

docker push <docker_image>:<image_version>

In this case:

docker push swapnova/node_with_docker:0.0.1
Pushing the code to Docker Hub

Since our image is public, anyone on the internet can pull the image and run it on their machines without any third-party installation.

Conclusion

In this article, you can understand the basics of using Docker. To understand it completely I recommend making more projects using Docker to grasp it.

So, keep growing and keep learning. Auf Wiedersehen šŸ‘¾

--

--

Swapnil Sinha
DataX Journal

Writing for me is 'Peace in my mind.' I am a developer who solves real-time problems. Moreover , I am a Data Engineer @AB InBev.