9
9
"net/http"
10
10
"os"
11
11
"path"
12
+ "sync"
12
13
"time"
13
14
14
15
"github.com/docker/docker/api/types"
@@ -20,7 +21,8 @@ import (
20
21
)
21
22
22
23
const (
23
- TmpDir = "./tmp"
24
+ TmpDir = "./tmp"
25
+ containerTimeout = 1
24
26
)
25
27
26
28
type dockerHandler struct {
@@ -198,18 +200,26 @@ func (dh *dockerHandler) Start() error {
198
200
199
201
// start containers
200
202
// docker start <container>
203
+
204
+ wg := sync.WaitGroup {}
201
205
for _ , container := range dh .thisContainers {
202
- err := client .ContainerStart (
203
- context .Background (),
204
- container ,
205
- types.ContainerStartOptions {},
206
- )
207
- if err != nil {
208
- return err
209
- }
206
+ wg .Add (1 )
207
+ go func (c string ) {
208
+ err := client .ContainerStart (
209
+ context .Background (),
210
+ c ,
211
+ types.ContainerStartOptions {},
212
+ )
213
+ wg .Done ()
214
+ if err != nil {
215
+ log .Printf ("error starting container %s: %s" , c , err )
216
+ return
217
+ }
210
218
211
- log .Println ("started container" , container )
219
+ log .Println ("started container" , c )
220
+ }(container )
212
221
}
222
+ wg .Wait ()
213
223
214
224
// get container IPs
215
225
// docker inspect <container>
@@ -275,40 +285,44 @@ func (dh *dockerHandler) Destroy() error {
275
285
276
286
log .Printf ("fh: %+v" , dh )
277
287
278
- // stop containers
279
- // docker stop <container>
288
+ wg := sync.WaitGroup {}
280
289
log .Printf ("stopping containers: %v" , dh .thisContainers )
281
290
for _ , c := range dh .thisContainers {
282
- log .Println ("stopping container" , c )
291
+ log .Println ("removing container" , c )
283
292
284
- err := client .ContainerStop (
285
- context .Background (),
286
- c ,
287
- container.StopOptions {},
288
- )
289
- if err != nil {
290
- return err
291
- }
293
+ wg .Add (1 )
294
+ go func (c string ) {
295
+ log .Println ("stopping container" , c )
292
296
293
- log .Println ("stopped container" , c )
294
- }
297
+ timeout := 1 // seconds
295
298
296
- // remove containers
297
- // docker rm <container>
298
- for _ , container := range dh .thisContainers {
299
- log .Println ("removing container" , container )
299
+ err := client .ContainerStop (
300
+ context .Background (),
301
+ c ,
302
+ container.StopOptions {
303
+ Timeout : & timeout ,
304
+ },
305
+ )
306
+ if err != nil {
307
+ log .Printf ("error stopping container %s: %s" , c , err )
308
+ }
300
309
301
- err := client .ContainerRemove (
302
- context .Background (),
303
- container ,
304
- types.ContainerRemoveOptions {},
305
- )
306
- if err != nil {
307
- return err
308
- }
310
+ log .Println ("stopped container" , c )
311
+
312
+ err = client .ContainerRemove (
313
+ context .Background (),
314
+ c ,
315
+ types.ContainerRemoveOptions {},
316
+ )
317
+ wg .Done ()
318
+ if err != nil {
319
+ log .Printf ("error removing container %s: %s" , c , err )
320
+ }
321
+ }(c )
309
322
310
- log .Println ("removed container" , container )
323
+ log .Println ("removed container" , c )
311
324
}
325
+ wg .Wait ()
312
326
313
327
// remove network
314
328
// docker network rm <network>
0 commit comments