@@ -20,17 +20,16 @@ import (
2020 "context"
2121 "fmt"
2222
23+ "github.com/docker/compose-cli/api/compose"
24+
2325 "github.com/compose-spec/compose-go/types"
2426 apitypes "github.com/docker/docker/api/types"
27+ "github.com/docker/docker/api/types/container"
2528 "github.com/docker/docker/api/types/filters"
26- "golang.org/x/sync/errgroup"
27-
28- "github.com/docker/compose-cli/api/compose"
29-
3029 moby "github.com/docker/docker/pkg/stringid"
3130)
3231
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 ) {
3433 originalServices := project .Services
3534 var requestedService types.ServiceConfig
3635 for _ , service := range originalServices {
@@ -53,23 +52,23 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
5352 requestedService .Labels = requestedService .Labels .Add (oneoffLabel , "True" )
5453
5554 if err := s .ensureImagesExists (ctx , project ); err != nil { // all dependencies already checked, but might miss requestedService img
56- return err
55+ return 0 , err
5756 }
5857 if err := s .waitDependencies (ctx , project , requestedService ); err != nil {
59- return err
58+ return 0 , err
6059 }
6160 if err := s .createContainer (ctx , project , requestedService , requestedService .ContainerName , 1 , opts .AutoRemove ); err != nil {
62- return err
61+ return 0 , err
6362 }
6463 containerID := requestedService .ContainerName
6564
6665 if opts .Detach {
6766 err := s .apiClient .ContainerStart (ctx , containerID , apitypes.ContainerStartOptions {})
6867 if err != nil {
69- return err
68+ return 0 , err
7069 }
7170 fmt .Fprintln (opts .Writer , containerID )
72- return nil
71+ return 0 , nil
7372 }
7473
7574 containers , err := s .apiClient .ContainerList (ctx , apitypes.ContainerListOptions {
@@ -79,16 +78,25 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
7978 All : true ,
8079 })
8180 if err != nil {
82- return err
81+ return 0 , err
8382 }
8483 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+ }
8993
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
92100 }
93- return eg . Wait ()
101+
94102}
0 commit comments