@@ -28,15 +28,19 @@ const (
28
28
)
29
29
30
30
// Setup creates a new internal Docker network and connects container to it.
31
- func Setup (ctx context.Context , dockerCLI * client.Client , instanceID , containerID string ) (string , error ) {
31
+ func Setup (ctx context.Context , dockerCLI * client.Client , instanceID , containerName string ) (string , error ) {
32
32
networkName := getNetworkName (instanceID )
33
33
34
34
log .Dbg ("Discovering internal network:" , networkName )
35
35
36
36
networkResource , err := dockerCLI .NetworkInspect (ctx , networkName , types.NetworkInspectOptions {})
37
37
if err == nil {
38
- if err := dockerCLI .NetworkConnect (ctx , networkResource .ID , containerID , & network.EndpointSettings {}); err != nil {
39
- return "" , err
38
+ if ! hasContainerConnected (networkResource , containerName ) {
39
+ if err := dockerCLI .NetworkConnect (ctx , networkResource .ID , containerName , & network.EndpointSettings {}); err != nil {
40
+ return "" , err
41
+ }
42
+
43
+ log .Dbg (fmt .Sprintf ("Container %s has been connected to %s" , containerName , networkName ))
40
44
}
41
45
42
46
return networkResource .ID , nil
@@ -61,7 +65,7 @@ func Setup(ctx context.Context, dockerCLI *client.Client, instanceID, containerI
61
65
62
66
log .Dbg ("A new internal network has been created:" , internalNetwork .ID )
63
67
64
- if err := dockerCLI .NetworkConnect (ctx , internalNetwork .ID , containerID , & network.EndpointSettings {}); err != nil {
68
+ if err := dockerCLI .NetworkConnect (ctx , internalNetwork .ID , containerName , & network.EndpointSettings {}); err != nil {
65
69
return "" , err
66
70
}
67
71
@@ -108,11 +112,13 @@ func Connect(ctx context.Context, dockerCLI *client.Client, instanceID, containe
108
112
return fmt .Errorf ("internal network not found: %w" , err )
109
113
}
110
114
111
- if err := dockerCLI .NetworkConnect (ctx , networkResource .ID , containerID , & network.EndpointSettings {}); err != nil {
112
- return err
113
- }
115
+ if ! hasContainerConnected (networkResource , containerID ) {
116
+ if err := dockerCLI .NetworkConnect (ctx , networkResource .ID , containerID , & network.EndpointSettings {}); err != nil {
117
+ return err
118
+ }
114
119
115
- log .Dbg (fmt .Sprintf ("Container %s has been connected to %s" , instanceID , networkName ))
120
+ log .Dbg (fmt .Sprintf ("Container %s has been connected to %s" , instanceID , networkName ))
121
+ }
116
122
117
123
return nil
118
124
}
@@ -147,6 +153,16 @@ func Reconnect(ctx context.Context, dockerCLI *client.Client, instanceID, contai
147
153
return nil
148
154
}
149
155
156
+ func hasContainerConnected (networkResource types.NetworkResource , containerID string ) bool {
157
+ for _ , container := range networkResource .Containers {
158
+ if container .Name == containerID {
159
+ return true
160
+ }
161
+ }
162
+
163
+ return false
164
+ }
165
+
150
166
func getNetworkName (instanceID string ) string {
151
167
return networkPrefix + instanceID
152
168
}
0 commit comments