Yo, welcome. In this article, we will learn how to dockerize our Node JS application after all features already develop. This tutorial will cover some points including:
Prepare NodeJS Application
Create Docker Image
Create Docker Compose Script
Run App Container
Ok, with that said let's jump right into it.
Before we're going please ensure you've already installed the docker and docker-compose on your computer. Please follow this step to download and install.
Intro
What's docker?
Docker is one of the dockerizing tools. With docker, you can create a container for your application inside of your computer virtual machine. Usually, when we want to deploy our application into the server or VPS, we need to install a database, nginx, storage, and so on.
If the Nginx is down, your application will not be accessible to your user. This why the docker can help you to maintain your application inside the container. You can run a node js, database (PostgreSQL), Storage, and Nginx into a single container. This means when something is wrong with one of these, your other application can be run normally.
Docker Compose allows you can define and run the multi-application into a different service that is written using the YAML file as configuration.
That's why if you run multiple applications inside your server I will recommend using docker. Now you will understand how docker work.
Prepare Node JS App
So, the first thing that you should do is to create a simple Node JS Application. You can follow this tutorial to create a Simple REST API Example. You also can just use another example.
Create Docker Image
Docker is running from the image, you can pull and push your image into the Docker Hub. So before we can run the application inside the container, we need to create a new image for our application.
Create a new file called '.dockerignore', then put this code inside.
# Ignore the resource when building the image
# of you app
/node_modules
.env
*.md
.prettierrc
This file works the same as the gitignore file. They use it to manage and exclude the file when building the docker image. In this example, I just exclude the node_module, .env, all o the .md file, and the prettier configuration. You can add and remove the following with your application preferences.
Create a new file called 'Dockerfile' and then put this code inside
# Docker file configuration
# Use an official Node.js runtime as a parent image
FROM node:14
# Set the working directory to /app
WORKDIR /app
# Copy the package.json and package-lock.json files to the working directory
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code to the working directory
COPY . .
# Start the Node.js application
CMD [ "npm", "start" ]
The docker file is the file configuration when building the docker image. Every app you build needs to specify here. Let me explain what is this exactly
FROM node:14
When starting to create a docker file, you should specify the base image to run for this application. in this case, we will use Node JS version 14. You can browse some versions in the docker hub for NodeJS Image.WORKDIR
Tell the docker where should we working on.Now we can copy the package.json & and package-lock.json files into the working directory by using
COPY
command. We need a package.json file to install all dependencies first.After dependencies are installed. This will make a node_modules virtual folder. then we can copy all of our source code into the working directory.
Then we can run the application with
CMD
following with the argument.
Now your docker file is ready. With this, you can build your docker image and also run into a container. But in this case, we will manage all of our application services using docker-compose.
Create Docker Compose
Ok, after all the preparation is ready. Now we can create the configuration for composing the docker image into a container. The way we can use the docker-compose feature. Anyway, docker-compose is separated from the docker bundle. so please install Docker Compose.
So now let's create one file called 'docker-compose. yaml' and then put this code inside.
# Docker compose configuration file
version: '3.8'
services:
# Your application service
# Automaticaly create the image then run the container
app:
build:
context: .
ports:
- '4000:4000'
restart: always
environment:
PORT: 4000
DATABASE_PORT: YOUR_DATABASE_PORT
DATABASE_NAME: YOUR_DATABASE_NAME
DATABASE_URL: YOUR_DATABASE_URL
DATABASE_USER: YOUR_DATABASE_USER
DATABASE_PASS: YOUR_DATABASE_PASS
# Run your database service
mongodb:
image: mongo:6.0
container_name: mongodb
restart: always
ports:
- '27017:27017'
environment:
MONGO_INITDB_ROOT_USERNAME: YOUR_DATABASE_USER
MONGO_INITDB_ROOT_PASSWORD: YOUR_DATABASE_PASS
This compose file is running two services including your application, and the database. If you take a look at the app service, you will not see the image but you can see the built property. This means when the compose is run, the docker will automatically build your image and run as a container.
You also can add another one for example the NGINX, and Cloud Storage. Please ensure to check the image and all instructions from Docker Hub. Anyway, don't forget to change the environment with your settings.
Run Application
Now, let's go to the terminal and change the directory to your application folder. then run the following command.
Run
docker --version
to check your docker version installed.Now run
docker compose up -d
command to do everything for your service including pulling or creating images, and creating containers. The-d
argument is to enable the detach mode, so your code is running in the background.Now check the docker container by running
docker ps
command.
Good luck, now your application is already dockerized. You can easily run and stop the container. Please learn more about the docker command if you're not familiar with it. In the last word, try docker with any other languages and frameworks to expand your skills.