2022-02-17 22:00:46 +01:00
|
|
|
module docker
|
|
|
|
|
|
|
|
import json
|
|
|
|
import net.urllib
|
|
|
|
|
|
|
|
struct Container {
|
2022-02-20 13:10:48 +01:00
|
|
|
id string [json: Id]
|
|
|
|
names []string [json: Names]
|
2022-02-17 22:00:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn containers() ?[]Container {
|
2022-02-20 20:26:39 +01:00
|
|
|
res := request('GET', urllib.parse('/containers/json') ?) ?
|
2022-02-17 22:00:46 +01:00
|
|
|
|
2022-02-19 21:41:26 +01:00
|
|
|
return json.decode([]Container, res.text) or {}
|
2022-02-17 22:00:46 +01:00
|
|
|
}
|
2022-02-20 20:26:39 +01:00
|
|
|
|
|
|
|
pub struct NewContainer {
|
|
|
|
image string [json: Image]
|
|
|
|
entrypoint []string [json: Entrypoint]
|
|
|
|
cmd []string [json: Cmd]
|
|
|
|
env []string [json: Env]
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CreatedContainer {
|
|
|
|
id string [json: Id]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_container(c &NewContainer) ?string {
|
|
|
|
res := request_with_json('POST', urllib.parse('/containers/create') ?, c) ?
|
|
|
|
|
|
|
|
if res.status_code != 201 {
|
|
|
|
return error('Failed to create container.')
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.decode(CreatedContainer, res.text) ?.id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn start_container(id string) ?bool {
|
|
|
|
res := request('POST', urllib.parse('/containers/$id/start') ?) ?
|
|
|
|
println(res)
|
|
|
|
|
|
|
|
return res.status_code == 204
|
|
|
|
}
|