Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 54a7819

Browse files
authored
Merge pull request #1290 from docker/restart_logs
2 parents 4d05e02 + 4462f12 commit 54a7819

File tree

10 files changed

+45
-31
lines changed

10 files changed

+45
-31
lines changed

aci/compose.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,6 @@ func (cs *aciComposeService) Kill(ctx context.Context, project *types.Project, o
207207
return errdefs.ErrNotImplemented
208208
}
209209

210-
func (cs *aciComposeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
211-
return errdefs.ErrNotImplemented
210+
func (cs *aciComposeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
211+
return 0, errdefs.ErrNotImplemented
212212
}

api/client/compose.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ func (c *composeService) Kill(ctx context.Context, project *types.Project, optio
8080
return errdefs.ErrNotImplemented
8181
}
8282

83-
func (c *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
84-
return errdefs.ErrNotImplemented
83+
func (c *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
84+
return 0, errdefs.ErrNotImplemented
8585
}

api/compose/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type Service interface {
5252
// Kill executes the equivalent to a `compose kill`
5353
Kill(ctx context.Context, project *types.Project, options KillOptions) error
5454
// RunOneOffContainer creates a service oneoff container and starts its dependencies
55-
RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) error
55+
RunOneOffContainer(ctx context.Context, project *types.Project, opts RunOptions) (int, error)
5656
}
5757

5858
// CreateOptions group options of the Create API

cli/cmd/compose/run.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/docker/compose-cli/api/client"
2727
"github.com/docker/compose-cli/api/compose"
2828
"github.com/docker/compose-cli/api/progress"
29+
"github.com/docker/compose-cli/cli/cmd"
2930
)
3031

3132
type runOptions struct {
@@ -85,7 +86,11 @@ func runRun(ctx context.Context, opts runOptions) error {
8586
Writer: os.Stdout,
8687
Reader: os.Stdin,
8788
}
88-
return c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
89+
exitCode, err := c.ComposeService().RunOneOffContainer(ctx, project, runOpts)
90+
if exitCode != 0 {
91+
return cmd.ExitCodeError{ExitCode: exitCode}
92+
}
93+
return err
8994
}
9095

9196
func startDependencies(ctx context.Context, c *client.Client, project types.Project, requestedServiceName string) error {

ecs/local/compose.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,6 @@ func (e ecsLocalSimulation) Ps(ctx context.Context, projectName string, options
172172
func (e ecsLocalSimulation) List(ctx context.Context) ([]compose.Stack, error) {
173173
return e.compose.List(ctx)
174174
}
175-
func (e ecsLocalSimulation) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
176-
return errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose run")
175+
func (e ecsLocalSimulation) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
176+
return 0, errors.Wrap(errdefs.ErrNotImplemented, "use docker-compose run")
177177
}

ecs/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ import (
2525
"github.com/docker/compose-cli/api/errdefs"
2626
)
2727

28-
func (b *ecsAPIService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
29-
return errdefs.ErrNotImplemented
28+
func (b *ecsAPIService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
29+
return 0, errdefs.ErrNotImplemented
3030
}

kube/compose.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,6 @@ func (s *composeService) Kill(ctx context.Context, project *types.Project, optio
194194
}
195195

196196
// RunOneOffContainer creates a service oneoff container and starts its dependencies
197-
func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) error {
198-
return errdefs.ErrNotImplemented
197+
func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
198+
return 0, errdefs.ErrNotImplemented
199199
}

local/compose/attach.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (s *composeService) getContainerStreams(ctx context.Context, container moby
114114
Stdin: true,
115115
Stdout: true,
116116
Stderr: true,
117-
Logs: true,
117+
Logs: false,
118118
})
119119
if err != nil {
120120
return nil, nil, err

local/compose/convergence.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func (s *composeService) ensureService(ctx context.Context, observedState Contai
114114
w.Event(progress.RunningEvent(name))
115115
case status.ContainerCreated:
116116
case status.ContainerRestarting:
117+
case status.ContainerExited:
117118
w.Event(progress.CreatedEvent(name))
118119
default:
119120
eg.Go(func() error {

local/compose/run.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)