diff --git a/index.js b/index.js index 41afac6..5e8ee9b 100644 --- a/index.js +++ b/index.js @@ -165,6 +165,58 @@ app.get('/container/:containerId/restart', function (req, res) { }) }); + +/** + * Start the container by the ID specified + */ +app.get('/container/:containerId/start', function (req, res) { + var containerId = req.params.containerId; + console.log("Start " + containerId); + + getContainer(containerId, function (container) { + docker.getContainer(container.Id).start(function (err, data) { + if (err) { + res.status(500); + res.send(err); + return; + } + res.status(200); //We found the container! This reponse can be trusted + res.send(data); + }); + }, function (status, message) { + res.status(status); + if (message) { + res.send(message); + } + }) +}); + + +/** + * Stop the container by the ID specified + */ +app.get('/container/:containerId/stop', function (req, res) { + var containerId = req.params.containerId; + console.log("Stop " + containerId); + + getContainer(containerId, function (container) { + docker.getContainer(container.Id).stop(function (err, data) { + if (err) { + res.status(500); + res.send(err); + return; + } + res.status(200); //We found the container! This reponse can be trusted + res.send(data); + }); + }, function (status, message) { + res.status(status); + if (message) { + res.send(message); + } + }) +}); + app.post('/container/:containerId/exec', function(req, res) { var containerId = req.params.containerId; console.log("Exec " + containerId); @@ -178,7 +230,7 @@ app.post('/container/:containerId/exec', function(req, res) { res.status(400); return; } - + getContainer(containerId, function (container) { if (config.get("debug")) console.log("Attempting to execute command in container " + container.Id); @@ -283,14 +335,14 @@ function getContainer(name, cb, error) if (err) { if (typeof error == "function") return error(500, err); - + return; } if (containers.length < 1) { if (typeof error == "function") return error(404, "container not found"); - + return; } diff --git a/readme.md b/readme.md index c5a172c..65001ff 100644 --- a/readme.md +++ b/readme.md @@ -47,12 +47,26 @@ The easiest way to get started is to run this service inside its own Docker cont ```bash docker run -d \ --name=ha-dockermon --restart=always \ --v /var/run/docker.sock:/var/run/docker.sock --v /path/to/config:/config --p 8126:8126 +-v /var/run/docker.sock:/var/run/docker.sock \ +-v /path/to/config:/config \ +-p 8126:8126 \ philhawthorne/ha-dockermon ``` +#### Docker Compose +If you prefer to use Docker Compose, here is a sample entry you can add to your Docker compose file. +```yaml + docker_mon: + image: philhawthorne/ha-dockermon + container_name: ha_dockermon + restart: always + volumes: + - /var/run/docker.sock:/var/run/docker.sock + - /path/to/config:/config + ports: + - 8126:8126 +``` + #### NodeJS You can run this service directly from a host that has NPM and NodeJS installed. Just checkout this repository and then run: @@ -127,4 +141,4 @@ switch: ``` # Further Reading -For more in-depth Home Assistant examples and some ideas for use, please check out [this article on my blog](https://philhawthorne.com/ha-dockermon-use-home-assistant-to-monitor-start-or-stop-docker-containers). \ No newline at end of file +For more in-depth Home Assistant examples and some ideas for use, please check out [this article on my blog](https://philhawthorne.com/ha-dockermon-use-home-assistant-to-monitor-start-or-stop-docker-containers).