我正在尝试将 docker 容器部署到其他服务器。
这里是代码,里面有一些文件
服务器.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);
包.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"
}
配置.js
module.exports = {
db: {
production: "mongodb://"+process.env.MONGODB_ADDRESS+":27017/product_quantities",
development: "mongodb://"+process.env.MONGODB_ADDRESS+":27017/product_quantities",
}
};
docker 文件
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文件
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
我已经通过 docker 命令在本地实现了 docker 镜像,并且还推送到了 docker hub 上。
现在在其他服务器上,当我们通过 docker pull 命令从 docker hub 中拉取容器时,它已成功运行,但是当我们运行 docker 命令时,它就会运行
返回错误是:
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
最佳答案
您似乎没有运行 mongoDB。您有本地 Mongo 安装还是也已容器化?
如果是,请确保 Mongo 实例也在您的服务器上运行。
关于node.js - 如何在其他服务器上部署docker文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43346554/