Advantages
1. Continuous Integration Efficiency
- Docker enables you to build a container image and use that same image across every step of the deployment process.
2. Compatibility and Maintainability
- Eliminate the “it works on my machine” problem once and for all. One of the benefits that the entire team will appreciate is parity. Parity, in terms of Docker, means that your images run the same no matter which server or whose laptop they are running on.
3. Standardization
- Docker containers ensure consistency across multiple developments and release cycles, standardizing your environment.
- Docker provides repeatable development, build, test, and production environments. Standardizing service infrastructure across the entire pipeline allows every team member to work in a production equality environment.
Prerequisite
- Docker
- Docker-compose
- The compatible OS which supports docker [For Windows we need Windows-PRO version]
Dockerfile:
FROM node:12
WORKDIR /app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm install --production --silent && mv node_modules ../
COPY . .
EXPOSE 3010
RUN npm install -g nodemon
CMD npm startLet’s see what are above commands are actually meant for
From node:12WORKDIR /appCOPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]RUN npm install --production --silent && mv node_modules ../COPY . .EXPOSE 3010RUN npm install -g nodemonCMD npm startdocker build -t sample-app . .
as the current directoryThis is a command for building a docker image for a sample app that I have created.-t stands for giving an Allocate a pseudo-TTYWhile building the Image, if you are building an image for the first time, you see that images will be downloaded from the docker hub and files are processed from your local system to the docker container.Here is the sample output my docker image which I have built from the above docker file.Building Image will show the following output
Step 1/8 : FROM node:12
---> b074182f4154
Step 2/8 : WORKDIR /app
---> Using cache
---> e07060a5ab32
Step 3/8 : COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
---> Using cache
---> fd19d59b39e9
Step 4/8 : RUN npm install --production --silent && mv node_modules ../
---> Using cache
---> b2aa67ffc9c2
Step 5/8 : COPY . .
---> c05330c66aa3
Step 6/8 : EXPOSE 3000
---> Running in a5d196effe33
Removing intermediate container a5d196effe33
---> 94fc592e18f8
Step 7/8 : RUN npm install -g nodemon
---> Running in 34debda3811d
/usr/local/bin/nodemon -> /usr/local/lib/node_modules/nodemon/bin/nodemon.js
> nodemon@1.19.1 postinstall /usr/local/lib/node_modules/nodemon
> node bin/postinstall || exit 0
Love nodemon? You can now support the project via the open collective:
> https://opencollective.com/nodemon/donate
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/nodemon/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
+ nodemon@1.19.1
added 221 packages from 128 contributors in 18.789s
Removing intermediate container 34debda3811d
---> ffd667c80190
Step 8/8 : CMD npm start
---> Running in a9d44a256c2c
Removing intermediate container a9d44a256c2c
---> 5b2751381d7d
Successfully built 5b2751381d7d
Successfully tagged sample-app:latestdocker run -p 3000:3000 sample-appnode ./bin/www
Elasticsearch INFO: 2019-08-29T10:18:59Z
Adding connection to http://localhost:9200/
MongoDB connection error: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1054:14) {
name: 'MongoNetworkError',
errorLabels: [Array],
[Symbol(mongoErrorContextSymbol)]: {}
}]
npm ERR! code ELIFECYCLE
npm ERR! errno 255
npm ERR! mongoelastic@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 255
npm ERR!
npm ERR! Failed at the mongoelastic@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2019-08-29T10_18_59_116Z-debug.logWhy the docker file is failing ??
Our app needs to have “MongoDB” and as well as “Elastic Search” and our docker file was not able to find any source for MongoDB and Elastic Search.As you can see in the above error logs while running our docker file it throws an error for connecting with MongoDB. For solving the above issues, I had to dig up more regarding the docker.After exploring, I ran into docker-compose, which helps in defining and running multi-container Docker applications. With a single command, you create and start all the services from your configuration. I faced lots of failures but finally succeeded in making a perfect docker-compose as per our requirement.I have shared my docker-compose which runs with MongoDB and Elastic Search along with Node JS backend.We can add gif regarding the getting a miracle thingversion: "3"
services:
backend:
container_name: nodejs
restart: always
build: ./
ports:
- "3010:3010"
volumes:
- .:/app
- ./error.log:/usr/src/app/error.log
links:
- mongo
- elasticsearch
mongo:
container_name: mongo
image: mongo
ports:
- "27017:27017"
volumes:
- ./data:/data/db
elasticsearch:
container_name: elasticsearch
image: docker.elastic.co/elasticsearch/elasticsearch:7.0.0
environment:
- node.name=es01
- discovery.type=single-node
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- esdata:/usr/share/elasticsearch/data
ports:
- "9200:9200"
volumes:
esdata:
driver: localversion: "3"services:container_name:restart: alwaysbuild: ./ports:
- "3000:3000"
ports:
- "27017:27017"volumes:
- .:/app
- ./error.log:/usr/src/app/error.log
volumes:
- ./data:/data/db
volumes:
- esdata:/usr/share/elasticsearch/data links:
- mongo
- elasticsearchenvironment:
- node.name=es01
- discovery.type=single-node
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"docker-compose updocker-compose up -ddocker-compose -f docker-compose.dev.yml builddocker-compose -f docker-compose.dev.yml up -dmongo uses an image, skipping
elasticsearch uses an image, skipping
Building backend
Step 1/8 : FROM node:12
---> b074182f4154
Step 2/8 : WORKDIR /usr/src/app
---> Using cache
---> 51f2e238ec24
Step 3/8 : COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
---> 1cba60d1b0d1
Step 4/8 : RUN npm install --production --silent && mv node_modules ../
---> Running in 38ba6b4206ad
added 470 packages from 332 contributors and audited 6850 packages in 15.205s
found 55 vulnerabilities (9 low, 4 moderate, 40 high, 2 critical)
run `npm audit fix` to fix them, or `npm audit` for details
Removing intermediate container 38ba6b4206ad
---> cfbd4706cd3c
Step 5/8 : COPY . .
---> 3469281650a7
Step 6/8 : EXPOSE 3000
---> Running in 97aed101c9b1
Removing intermediate container 97aed101c9b1
---> 66cd1898a59b
Step 7/8 : RUN npm install -g nodemon
---> Running in 469c04f242cd
/usr/local/bin/nodemon -> /usr/local/lib/node_modules/nodemon/bin/nodemon.js
> nodemon@1.19.4 postinstall /usr/local/lib/node_modules/nodemon
> node bin/postinstall || exit 0
Love nodemon? You can now support the project via the open collective:
> https://opencollective.com/nodemon/donate
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/nodemon/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
+ nodemon@1.19.4
added 221 packages from 128 contributors in 27.31s
Removing intermediate container 469c04f242cd
---> db4710afc11b
Step 8/8 : CMD npm start
---> Running in 5b5a8fce0e08
Removing intermediate container 5b5a8fce0e08
---> 6f8fbf6e5989
Successfully built 6f8fbf6e5989
Successfully tagged express-mongo-elasticsearch-1_backend:latestStarting elasticsearch ... done
Starting mongo ... done Starting nodejs ... donedocker logs [container name]
E.g docker logs nodejs{"mongo":"Connected to server.","elastic":"Connected to server.","version":"1.0"}

