How to deploy docker file to another server
I am trying to deploy a docker container to another server.
Here's the code where 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 by local docker command and also pushed to docker hub.
Now on another server, when we pull the container out of the docker hub with the docker pull command, it starts successfully, but when we run the docker command, then it
Return error:
MongoError: Could not connect to server [localhost: 27017] on first connection [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
source to share
In server.js
you have:
mongoose.connect("mongodb://localhost/product_quantities");
In config.js
you export the prod and dev url for the mongodb connection string, but it doesn't look like you are using that anywhere in your code, so you always load the localhost connection string.
In your code, you need to use values ββfrom config.js
and in dockerfile, set environment value toMONGODB_ADDRESS
source to share