@@ -20,17 +20,16 @@ import (
20
20
"context"
21
21
"fmt"
22
22
23
+ "github.com/docker/compose-cli/api/compose"
24
+
23
25
"github.com/compose-spec/compose-go/types"
24
26
apitypes "github.com/docker/docker/api/types"
27
+ "github.com/docker/docker/api/types/container"
25
28
"github.com/docker/docker/api/types/filters"
26
- "golang.org/x/sync/errgroup"
27
-
28
- "github.com/docker/compose-cli/api/compose"
29
-
30
29
moby "github.com/docker/docker/pkg/stringid"
31
30
)
32
31
33
- func (s * composeService ) RunOneOffContainer (ctx context.Context , project * types.Project , opts compose.RunOptions ) error {
32
+ func (s * composeService ) RunOneOffContainer (ctx context.Context , project * types.Project , opts compose.RunOptions ) ( int , error ) {
34
33
originalServices := project .Services
35
34
var requestedService types.ServiceConfig
36
35
for _ , service := range originalServices {
@@ -53,23 +52,23 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
53
52
requestedService .Labels = requestedService .Labels .Add (oneoffLabel , "True" )
54
53
55
54
if err := s .ensureImagesExists (ctx , project ); err != nil { // all dependencies already checked, but might miss requestedService img
56
- return err
55
+ return 0 , err
57
56
}
58
57
if err := s .waitDependencies (ctx , project , requestedService ); err != nil {
59
- return err
58
+ return 0 , err
60
59
}
61
60
if err := s .createContainer (ctx , project , requestedService , requestedService .ContainerName , 1 , opts .AutoRemove ); err != nil {
62
- return err
61
+ return 0 , err
63
62
}
64
63
containerID := requestedService .ContainerName
65
64
66
65
if opts .Detach {
67
66
err := s .apiClient .ContainerStart (ctx , containerID , apitypes.ContainerStartOptions {})
68
67
if err != nil {
69
- return err
68
+ return 0 , err
70
69
}
71
70
fmt .Fprintln (opts .Writer , containerID )
72
- return nil
71
+ return 0 , nil
73
72
}
74
73
75
74
containers , err := s .apiClient .ContainerList (ctx , apitypes.ContainerListOptions {
@@ -79,16 +78,25 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
79
78
All : true ,
80
79
})
81
80
if err != nil {
82
- return err
81
+ return 0 , err
83
82
}
84
83
oneoffContainer := containers [0 ]
85
- eg := errgroup.Group {}
86
- eg .Go (func () error {
87
- return s .attachContainerStreams (ctx , oneoffContainer , true , opts .Reader , opts .Writer )
88
- })
84
+ err = s .attachContainerStreams (ctx , oneoffContainer , true , opts .Reader , opts .Writer )
85
+ if err != nil {
86
+ return 0 , err
87
+ }
88
+
89
+ err = s .apiClient .ContainerStart (ctx , containerID , apitypes.ContainerStartOptions {})
90
+ if err != nil {
91
+ return 0 , err
92
+ }
89
93
90
- if err = s .apiClient .ContainerStart (ctx , containerID , apitypes.ContainerStartOptions {}); err != nil {
91
- return err
94
+ statusC , errC := s .apiClient .ContainerWait (context .Background (), oneoffContainer .ID , container .WaitConditionNotRunning )
95
+ select {
96
+ case status := <- statusC :
97
+ return int (status .StatusCode ), nil
98
+ case err := <- errC :
99
+ return 0 , err
92
100
}
93
- return eg . Wait ()
101
+
94
102
}
0 commit comments