这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
22 changes: 18 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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).
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).