Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.
Step 1 : Install Docker
You can download and install Docker on multiple platforms at Dockerhub
Step 2 : Create Dockerfile
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
For a demonstration of our example, we have created a simple Hello World NodeJS REST API application as shown below and are trying to dockerize it.
'use strict';
const express = require('express');
// Constants
const PORT = 3001;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello World');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
Go into your project root directory and create a file called “Dockerfile”. Next, add instructions to assemble the image as mentioned below.
First, add the OS image and then all the applications that would be required to build and run the project.
Alpine is a minimal Docker image based on Alpine Linux with a complete package index and only 5 MB in size!. Also, we would have to install Node
FROM alpine
FROM node:12
Now we specify the working directory for the project.
WORKDIR /usr/src/app
Further, we make sure that both package.json AND package-lock.json are copied to the container.
COPY package*.json ./
Install all the node dependencies by running npm install
RUN npm install
Bundle app source
COPY . .
Now that the environment is set up, next we run the application and expose the port on which the application would run on.
EXPOSE 3001
CMD [ "node", "index.js" ]
Below is the complete Dockerfile
FROM alpine
FROM node:12
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3001
CMD [ "node", "index.js" ]
Step 3 : Build Image
Now that the Dockerfile is ready with all the instructions for the build, our next steps is to build the image
docker build -t itsrohitnaik/node .
Note that isrohitnaik is my username on docker hub, you might have to create your own login on docker hub.
The below command would list all the docker images.
docker images
Step 4 : Run Image
To run the docker image simply execute below command and then navigate to localhost:9090
docker run -p 9090:3001 -d itsrohitnaik/abc
Note that here port 9090 is your local port and 3001 is container port.
Below are some important Docker commands that you would need while performing this exercise.
Shows all the current images running.
docker ps
Shows history of all the docker images that were run along with the containerId.
docker ps -a
In case if your docker image is failing to you can always check the logs and to do so you can execute the below command.
docker logs containerID
Below command would take you to container terminal.
docker exec -it containerID /bin/bash
Stops the container.
docker stop containerID
Hope this article was helpful. As always you can find the source code of this example at my GitHub page https://github.com/thatsrohitnaik/docker-nodejs/
Happy Coding !!