@@ -23,6 +23,14 @@ func GetNetworksFromConfig(cli *client.Client, project string, config *compose.C
23
23
}
24
24
networks [name ] = id
25
25
}
26
+ if _ , ok := networks ["default" ]; ! ok {
27
+ name , id , err := createNetwork (cli , project , "default" ,
28
+ compose.NetworkConfig {Name : fmt .Sprintf ("%s-default" , project )})
29
+ if err != nil {
30
+ return nil , err
31
+ }
32
+ networks [name ] = id
33
+ }
26
34
return networks , nil
27
35
}
28
36
@@ -138,11 +146,11 @@ func collectNetworks(cli *client.Client, project string) (map[string][]types.Net
138
146
return networks , nil
139
147
}
140
148
141
- func NetworkMode (serviceConfig compose.ServiceConfig , networks map [string ]string ) container.NetworkMode {
149
+ func NetworkMode (project string , serviceConfig compose.ServiceConfig , networks map [string ]string ) container.NetworkMode {
142
150
mode := serviceConfig .NetworkMode
143
151
if mode == "" {
144
152
if len (networks ) > 0 {
145
- for name := range getNetworksForService (serviceConfig ) {
153
+ for name := range getNetworksForService (project , serviceConfig ) {
146
154
if _ , ok := networks [name ]; ok {
147
155
return container .NetworkMode (networks [name ])
148
156
}
@@ -153,32 +161,29 @@ func NetworkMode(serviceConfig compose.ServiceConfig, networks map[string]string
153
161
return container .NetworkMode (mode )
154
162
}
155
163
156
- func getNetworksForService (config compose.ServiceConfig ) map [string ]* compose.ServiceNetworkConfig {
164
+ func getNetworksForService (project string , config compose.ServiceConfig ) map [string ]* compose.ServiceNetworkConfig {
157
165
if len (config .Networks ) > 0 {
158
166
return config .Networks
159
167
}
160
- return map [string ]* compose.ServiceNetworkConfig {" default" : nil }
168
+ return map [string ]* compose.ServiceNetworkConfig {fmt . Sprintf ( "%s- default", project ) : nil }
161
169
}
162
170
163
-
164
171
func BuildDefaultNetworkConfig (serviceConfig compose.ServiceConfig , networkMode container.NetworkMode ) * network.NetworkingConfig {
165
172
config := map [string ]* network.EndpointSettings {}
166
173
net := string (networkMode )
167
174
config [net ] = & network.EndpointSettings {
168
- Aliases : getAliases (serviceConfig .Name , serviceConfig .Networks [net ]),
175
+ Aliases : getAliases (serviceConfig .Name , serviceConfig .Networks [net ], "" ),
169
176
}
170
-
171
177
return & network.NetworkingConfig {
172
178
EndpointsConfig : config ,
173
179
}
174
180
}
175
181
176
-
177
182
func ConnectContainerToNetworks (context context.Context , cli * client.Client ,
178
183
serviceConfig compose.ServiceConfig , containerID string , networks map [string ]string ) error {
179
184
for key , net := range serviceConfig .Networks {
180
185
config := & network.EndpointSettings {
181
- Aliases : getAliases (serviceConfig .Name , net ),
186
+ Aliases : getAliases (serviceConfig .Name , net , containerID ),
182
187
}
183
188
err := cli .NetworkConnect (context , networks [key ], containerID , config )
184
189
if err != nil {
@@ -188,8 +193,11 @@ func ConnectContainerToNetworks(context context.Context, cli *client.Client,
188
193
return nil
189
194
}
190
195
191
- func getAliases (serviceName string , c * compose.ServiceNetworkConfig ) []string {
196
+ func getAliases (serviceName string , c * compose.ServiceNetworkConfig , containerID string ) []string {
192
197
aliases := []string {serviceName }
198
+ if containerID != "" {
199
+ aliases = append (aliases , containerShortID (containerID ))
200
+ }
193
201
if c != nil {
194
202
aliases = append (aliases , c .Aliases ... )
195
203
}
@@ -219,3 +227,7 @@ func ExposedPorts(ports []compose.ServicePortConfig) nat.PortSet {
219
227
}
220
228
return natPorts
221
229
}
230
+
231
+ func containerShortID (containerID string ) string {
232
+ return containerID [:12 ]
233
+ }
0 commit comments