Containerise Nodejs with Docker
Docker containers are very much popular in the DevOps community. Now a day all the companies are adopting Docker containers because of easy to implement and use. In this article, we will add our nodejs application with the Docker container.
Steps we are going to follow in this docker + Nodejs article
- Flow diagram that what we are going to do.
- Creating a simple nodejs application and push on Github.
- Install docker on our local system.
- Adding docker file and we will try to understand what is going on that file step by step.
- Docker image build.
- Run docker image.
- Access to local eg localhost:8080.
So, let’s proceed with one by one step
Step 1
Your own an application with some business logic. Now put that logic into a certain place where all the setups are according to your desired environment. For example, you are running our code on Linux alpine, node version is 16, etc.
Step 2: Creating sample Nodejs application:
I assume that you already know how to create and set up the nodejs applications. Here you can use your own project or else download the sample project from Git hub here and follow the steps written in the readme file of the repository.
Step 3: Install Docker on the local system:
- LINUX: Install docker on Linux by clicking docker official documentation
Step 3: Let’s add the docker file and add steps.
- create a docker file by the name of “Dockerfile”.
- Next, add the following code in Dockerfile which is created just now
# Download image of nodejs version 16 on alpine linux osFROM node:16-alpine# Once image is downloaded than now you are in alpine OS# Here We are creating a WORKDIR where all our code is going to stay.WORKDIR /app# Now once directry is created copy package.json from host machine to docker apline machine with current directoryCOPY package.json .# As already you know install all dependencies which are in package.json fileRUN npm install# Next copy all the files which are listed in current host directoryCOPY . .# If you see our application is running on 8080. So expose same port here also.# NOTE: In future articles we will se how to paas environment variables from docker to applicationEXPOSE 8080# Finally start our applicationCMD ["node", "index.js"]
- For more information plz read the comments written before each command.
- The above code can also be accessible by shifting branches by the following code
git checkout add-docker-code
Step 4: Build docker image:
If everything is fine now we are good to build our docker image with the above docker code.
So start building by the following code
docker build -t docker-nodejs .
Step 5: Run our build file by the following code
docker run -p 8080:8080 -d docker-nodejs
Wait let me explain these parameters
- run parameter show to run a docker build image. Here I am using docker-nodejs image
- -p 8080:8080 means which port. First 8080 indicates which port you are exposing into the host machine and the next port 8080 is the docker image port from where we are exposing our code.
- -d is the detached mode. It means we are not engaging our terminal for application logs.
- The explanation is done😎 now run the above code.
Step 6: Excess our code to localhost
- Type localhost:8080 URL in browse.
What next: Check multistage image build from docker official page