Convert Node.js App into Docker Container
In this blog we will discuss how to build docker image using node.js application. We have already created a sample node.js express test application server which available on my GitHub repo.
Here we are assuming that you have basic understanding docker image and container concept.
To deploy any type of application in a docker image you have to first create Dockerfile. This file contains all necessary information that required to create image like what image you want to create, location of the app, required ports and many more. Now its time to go and start putting instruction in the Dockerfile.
Each Dockerfile structure always starts from FROM directive, using this directive you specify base image and in this case we set it node:10-alpine which include Node.js runtime and npm. By default application container used non-root node user.
FROM node:10-alpine
Now we need to create some directories inside container and set right permissions. Using mkdir command create directory and subdirectory under /home/node and use chown command to set ownership of node user.
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
It’s good idea to always set container working directory
WORKDIR /home/node/app
Copy *.json file like package.json or package-lock.json file:
COPY package*.json ./
To create node_modules directory you need to install all the dependencies using npm install command, but before doing that change the current user to node.
USER node
RUN npm install
To bundle your app’s source code inside the Docker image, use the COPY
instruction:
COPY . .
We are almost done with configuration except exposing running port and run instruction.
EXPOSE 5000
CMD [ "node", "server.js" ]
Here is the complete instruction set of Dockerfile:
FROM node:10-alpine
mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
RUN npm install
COPY . .
EXPOSE 5000
CMD [ "node", "index.js" ]
To avoid anything to copying in the container you can specify in .dockerignore file. Create docker ignore file and add the following lines
node_modules
npm-debug.log
Build Docker Image
Our Dockerfile is ready to build docker image with give below command.
The . instruction help docker command to locate where Dockerfile is.
docker build . -t saqib/nodejs-test-server
To verify the docker image is created using given command and get the list of existing docker images on machine.
docker images
Run Docker Image
To run any docker image we using docker run command and pass required parameters like name, port etc. Here -d is for running detached, -p is for port forwarding.
docker run -d -p 5000:5000 saqib/nodejs-test-server
You can verify the response using any browser. Type http://ip-address:5000 and the response message “Hello World into TechNetBytes!”
Publish Your Image
To share your docker container with other community members you need to publish you image on docker hub which allow you to maintain public and private repositories of images. Sign-Up on https://hub.docker.com
build image again using docker username
docker build . -t [docker provided username]/nodejs-test-server
push docker image into the registry
docker push [docker provided username]/nodejs-test-server
Congratulation you have created and publish you first docker image in the registry! Thanks.