I am trying to deploy docker container to other server.
Here code is, in which some files
server.js
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var halson = require('halson');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080;
var config = require('./config');
var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/product_quantities");
var ProductQuantity = require('./app/models/product_quantity');
var router = express.Router();
router.put('/product_quantities/:product_id', function(req, res) {
if (req.body.quantity_onhand == null) {
res.status(400);
res.setHeader('Content-Type', 'application/vnd.error+json');
res.json({ message: "quantity_onhand parameter is required"});
}
else{
ProductQuantity.findOne({ product_id: req.params.product_id}, function(err, productQuantity) {
if (err) return console.error(err);
var created = false; // track create vs. update
if (productQuantity == null) {
productQuantity = new ProductQuantity();
productQuantity.product_id = req.params.product_id;
created = true;
}
// set/update the onhand quantity and save
productQuantity.quantity_onhand = req.body.quantity_onhand;
productQuantity.save(function(err) {
if (err) {
res.status(500);
res.setHeader('Content-Type', 'application/vnd.error+json');
res.json({ message: "Failed to save productQuantity"});
} else {
// return the appropriate response code, based
// on whether we created or updated a ProductQuantity
if (created) {
res.status(201);
} else {
res.status(200);
}
res.setHeader('Content-Type', 'application/hal+json');
var resource = halson({
product_id: productQuantity.product_id,
quantity_onhand: productQuantity.quantity_onhand,
created_at: productQuantity.created_at
}).addLink('self', '/product_quantities/'+productQuantity.product_id)
res.send(JSON.stringify(resource));
}
});
});
}
});
// Register our route
app.use('/', router);
// Start the server
app.listen(port);
console.log('Running on port ' + port);
package.json
{
"name": "project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2",
"halson": "^2.3.1",
"mongoose": "^4.9.3"
}
}
config.js
module.exports = {
db: {
production: "mongodb://"+process.env.MONGODB_ADDRESS+":27017/product_quantities",
development: "mongodb://"+process.env.MONGODB_ADDRESS+":27017/product_quantities",
}
};
dockerfile
FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
EXPOSE 8080
RUN npm install
CMD ["npm", "start"]
service.yml file
services:
inventory:
git_url: git@github.com:launchany/microservices-node-inventory.git
git_branch: master
command: npm start
build_root: .
ports:
- container: 8080
http: 80
https: 443
env_vars:
NODE_ENV: production
databases:
- mongodb
I have Implemented docker image on local by docker command and also pushed on docker hub.
Now In other server when we pull container from docker hub by docker pull command it is successfully run but when we run command of docker then it
return error is:
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
via Rahul Saini
No comments:
Post a Comment