|
|
@@ -283,17 +283,37 @@ func StopContainerByName(containerName string) error {
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- for _, container := range containers {
|
|
|
- for _, name := range container.Names {
|
|
|
+ for _, cont := range containers {
|
|
|
+ for _, name := range cont.Names {
|
|
|
if name == "/"+containerName {
|
|
|
// Stop the container
|
|
|
- if err := cli.ContainerStop(ctx, container.ID, nil); err != nil {
|
|
|
+ options := container.StopOptions{}
|
|
|
+ if err := cli.ContainerStop(ctx, cont.ID, options); err != nil {
|
|
|
return fmt.Errorf("failed to stop container %s: %v", containerName, err)
|
|
|
}
|
|
|
- logger.Info(fmt.Printf("Successfully stopped container %s\n", containerName))
|
|
|
+ logger.Info(fmt.Sprintf("Successfully stopped container %s\n", containerName))
|
|
|
return nil
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return fmt.Errorf("container with name %s not found", containerName)
|
|
|
-}
|
|
|
+}
|
|
|
+
|
|
|
+func DockerListener() {
|
|
|
+ ctx := context.Background()
|
|
|
+ cli, err := client.NewClientWithOpts(client.FromEnv)
|
|
|
+ if err != nil {
|
|
|
+ logger.Error(fmt.Sprintf("Error initializing Docker client: %v", err))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ messages, errs := cli.Events(ctx, types.EventsOptions{})
|
|
|
+ for {
|
|
|
+ select {
|
|
|
+ case event := <-messages:
|
|
|
+ // Convert the Docker event to our custom event and send it to the EventBus
|
|
|
+ EventBus <- Event{Type: event.Action, Data: event}
|
|
|
+ case err := <-errs:
|
|
|
+ logger.Error(fmt.Sprintf("Docker event error: %v", err))
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|