DockerRestApi/routes/restart.js

42 lines
976 B
JavaScript
Raw Normal View History

2019-09-20 16:54:00 +00:00
var express = require('express');
var router = express.Router();
var docker = require('../bin/dockerode');
const registry = 'registry.cliffbreak.de/';
2019-09-24 10:19:58 +00:00
router.get('/:id/:image', async (req, res, next) => {
2019-09-20 16:54:00 +00:00
if (req.params.image = ! null) {
await pullImage(req.params.image);
restartContainer(req.params.id).then(data => {
2019-09-20 16:54:00 +00:00
res.send(data);
});
}
else {
restartContainer(req.params.id).then(data => {
res.send(data);
});
}
});
function restartContainer(containername) {
return new Promise((resolve, reject) => {
const container = docker.getContainer(containername);
container.restart((err, data) => {
resolve({ err, data });
});
})
}
function pullImage(image) {
return new Promise((resolve, reject) => {
docker.pull(registry + image, (err, stream) => {
resolve({ err, stream });
});
})
}
module.exports = router;