@@ -12,8 +12,9 @@ import (
1212)
1313
1414type DockerService struct {
15- client * client.Client
16- context context.Context
15+ client * client.Client
16+ context context.Context
17+ isConnected bool
1718}
1819
1920func NewDockerService () * DockerService {
@@ -31,45 +32,52 @@ func (docker *DockerService) Init() error {
3132
3233 docker .client = client
3334 docker .context = ctx
35+
36+ _ , err = docker .client .Ping (docker .context )
37+
38+ if err != nil {
39+ log .Debug ().Err (err ).Msg ("Docker not connected" )
40+ docker .isConnected = false
41+ docker .client = nil
42+ docker .context = nil
43+ return nil
44+ }
45+
46+ docker .isConnected = true
47+ log .Debug ().Msg ("Docker connected" )
48+
3449 return nil
3550}
3651
37- func (docker * DockerService ) GetContainers () ([]container.Summary , error ) {
52+ func (docker * DockerService ) getContainers () ([]container.Summary , error ) {
3853 containers , err := docker .client .ContainerList (docker .context , container.ListOptions {})
3954 if err != nil {
4055 return nil , err
4156 }
4257 return containers , nil
4358}
4459
45- func (docker * DockerService ) InspectContainer (containerId string ) (container.InspectResponse , error ) {
60+ func (docker * DockerService ) inspectContainer (containerId string ) (container.InspectResponse , error ) {
4661 inspect , err := docker .client .ContainerInspect (docker .context , containerId )
4762 if err != nil {
4863 return container.InspectResponse {}, err
4964 }
5065 return inspect , nil
5166}
5267
53- func (docker * DockerService ) DockerConnected () bool {
54- _ , err := docker .client .Ping (docker .context )
55- return err == nil
56- }
57-
5868func (docker * DockerService ) GetLabels (appDomain string ) (config.App , error ) {
59- isConnected := docker .DockerConnected ()
60-
61- if ! isConnected {
69+ if ! docker .isConnected {
6270 log .Debug ().Msg ("Docker not connected, returning empty labels" )
6371 return config.App {}, nil
6472 }
6573
66- containers , err := docker .GetContainers ()
74+ containers , err := docker .getContainers ()
6775 if err != nil {
6876 return config.App {}, err
6977 }
7078
7179 for _ , ctr := range containers {
72- inspect , err := docker .InspectContainer (ctr .ID )
80+ inspect , err := docker .inspectContainer (ctr .ID )
7381 if err != nil {
7482 return config.App {}, err
7583 }
0 commit comments