Last active
July 31, 2019 15:27
-
-
Save farshidtz/46a100abbbe9b229d7071174a91ba4e4 to your computer and use it in GitHub Desktop.
Getting container events from Docker SDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"github.com/docker/docker/api/types" | |
"github.com/docker/docker/api/types/events" | |
"github.com/docker/docker/api/types/filters" | |
"github.com/docker/docker/client" | |
) | |
func main() { | |
ctx := context.Background() | |
cli, err := client.NewClientWithOpts() | |
if err != nil { | |
log.Fatal(err) | |
} | |
msgCh, errCh := cli.Events(ctx, types.EventsOptions{ | |
Filters: filters.NewArgs(filters.KeyValuePair{"type", events.ContainerEventType}), | |
}) | |
for { | |
select { | |
case err := <-errCh: | |
fmt.Println(err) | |
case msg := <-msgCh: | |
fmt.Println(msg.From, msg.Status) | |
switch msg.From { | |
case "image1", "image2": | |
if msg.Status == "die" { | |
log.Fatal(msg.From, "has died") | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment