Compare commits

..

10 Commits

Author SHA1 Message Date
Guillaume Lours
a2b9c81254 Merge pull request #9246 from ndeloof/interactive
map --interactive to StdinOpen
2022-03-08 10:23:28 +01:00
Guillaume Lours
d75f22cc7b publish a draft release with auto generate release notes
Signed-off-by: Guillaume Lours <guillaume.lours@docker.com>
2022-03-08 10:19:06 +01:00
Nicolas De Loof
2282159922 map --interactive to StdinOpen
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
2022-03-08 10:17:17 +01:00
Nicolas De Loof
8c7951465e filter containers after project has been rebuilt from resources
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
2022-03-08 09:47:22 +01:00
Victor Timofei
9054f6a18b Fix json format for version command
Signed-off-by: Victor Timofei <victor@vtimothy.com>
2022-03-08 09:40:25 +01:00
Guillaume Lours
0ef4b90fae don't failed when trying to stop or rm services with no containers running
Signed-off-by: Guillaume Lours <guillaume.lours@docker.com>
2022-03-07 10:35:22 +01:00
Guillaume Lours
908c12120e Merge pull request #9237 from ndeloof/run_regression
don't try to start dependencies when there are none
2022-03-07 08:42:01 +01:00
Nicolas De Loof
dbe7de50a7 don't try to start dependencies when there are none
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
2022-03-07 08:25:05 +01:00
Zixuan James Li
fcff39631a Add documentation for COMPOSE_IGNORE_ORPHANS
Signed-off-by: Zixuan James Li <359101898@qq.com>
2022-03-05 22:54:16 +01:00
PIG208
bcaa908f74 Support COMPOSE_IGNORE_ORPHANS for compose run
This revives #7020 and resolves the issue mentioned in #4992.

Signed-off-by: Zixuan James Li <359101898@qq.com>
2022-03-05 22:54:16 +01:00
9 changed files with 59 additions and 11 deletions

View File

@@ -44,7 +44,8 @@ jobs:
- uses: ncipollo/release-action@v1
with:
artifacts: "bin/*"
prerelease: true
generateReleaseNotes: true
draft: true
commit: "v2"
token: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ github.event.inputs.tag }}

View File

@@ -42,6 +42,7 @@ type runOptions struct {
Detach bool
Remove bool
noTty bool
interactive bool
user string
workdir string
entrypoint string
@@ -53,6 +54,7 @@ type runOptions struct {
servicePorts bool
name string
noDeps bool
ignoreOrphans bool
quietPull bool
}
@@ -134,6 +136,8 @@ func runCommand(p *projectOptions, backend api.Service) *cobra.Command {
if err != nil {
return err
}
ignore := project.Environment["COMPOSE_IGNORE_ORPHANS"]
opts.ignoreOrphans = strings.ToLower(ignore) == "true"
return runRun(ctx, backend, project, opts)
}),
ValidArgsFunction: serviceCompletion(p),
@@ -155,9 +159,8 @@ func runCommand(p *projectOptions, backend api.Service) *cobra.Command {
flags.BoolVar(&opts.servicePorts, "service-ports", false, "Run command with the service's ports enabled and mapped to the host.")
flags.BoolVar(&opts.quietPull, "quiet-pull", false, "Pull without printing progress information.")
cmd.Flags().BoolP("interactive", "i", true, "Keep STDIN open even if not attached. DEPRECATED")
cmd.Flags().MarkHidden("interactive") //nolint:errcheck
cmd.Flags().BoolP("tty", "t", true, "Allocate a pseudo-TTY. DEPRECATED")
cmd.Flags().BoolVarP(&opts.interactive, "interactive", "i", true, "Keep STDIN open even if not attached.")
cmd.Flags().BoolP("tty", "t", true, "Allocate a pseudo-TTY.")
cmd.Flags().MarkHidden("tty") //nolint:errcheck
flags.SetNormalizeFunc(normalizeRunFlags)
@@ -182,7 +185,7 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
}
err = progress.Run(ctx, func(ctx context.Context) error {
return startDependencies(ctx, backend, *project, opts.Service)
return startDependencies(ctx, backend, *project, opts.Service, opts.ignoreOrphans)
})
if err != nil {
return err
@@ -218,6 +221,14 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
Index: 0,
QuietPull: opts.quietPull,
}
for i, service := range project.Services {
if service.Name == opts.Service {
service.StdinOpen = opts.interactive
project.Services[i] = service
}
}
exitCode, err := backend.RunOneOffContainer(ctx, project, runOpts)
if exitCode != 0 {
errMsg := ""
@@ -229,7 +240,7 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
return err
}
func startDependencies(ctx context.Context, backend api.Service, project types.Project, requestedServiceName string) error {
func startDependencies(ctx context.Context, backend api.Service, project types.Project, requestedServiceName string, ignoreOrphans bool) error {
dependencies := types.Services{}
var requestedService types.ServiceConfig
for _, service := range project.Services {
@@ -242,8 +253,15 @@ func startDependencies(ctx context.Context, backend api.Service, project types.P
project.Services = dependencies
project.DisabledServices = append(project.DisabledServices, requestedService)
if err := backend.Create(ctx, &project, api.CreateOptions{}); err != nil {
err := backend.Create(ctx, &project, api.CreateOptions{
IgnoreOrphans: ignoreOrphans,
})
if err != nil {
return err
}
return backend.Start(ctx, project.Name, api.StartOptions{})
if len(dependencies) > 0 {
return backend.Start(ctx, project.Name, api.StartOptions{})
}
return nil
}

View File

@@ -57,7 +57,7 @@ func runVersion(opts versionOptions) {
return
}
if opts.format == formatter.JSON {
fmt.Printf(`{"version":%q}\n`, internal.Version)
fmt.Printf("{\"version\":%q}\n", internal.Version)
return
}
fmt.Println("Docker Compose version", internal.Version)

View File

@@ -99,3 +99,6 @@ Setting the `COMPOSE_FILE` environment variable is equivalent to passing the `-f
and so does `COMPOSE_PROFILES` environment variable for to the `--profiles` flag.
If flags are explicitly set on command line, associated environment variable is ignored
Setting the `COMPOSE_IGNORE_ORPHANS` environment variable to `true` will stop docker compose from detecting orphaned
containers for the project.

View File

@@ -98,6 +98,9 @@ long: |-
and so does `COMPOSE_PROFILES` environment variable for to the `--profiles` flag.
If flags are explicitly set on command line, associated environment variable is ignored
Setting the `COMPOSE_IGNORE_ORPHANS` environment variable to `true` will stop docker compose from detecting orphaned
containers for the project.
usage: docker compose
pname: docker
plink: docker.yaml

View File

@@ -100,7 +100,7 @@ func (s *composeService) projectFromName(containers Containers, projectName stri
Name: projectName,
}
if len(containers) == 0 {
return project, errors.New("no such project: " + projectName)
return project, errors.Wrap(api.ErrNotFound, fmt.Sprintf("no container found for project %q", projectName))
}
set := map[string]*types.ServiceConfig{}
for _, c := range containers {

View File

@@ -44,10 +44,14 @@ func (s *composeService) stop(ctx context.Context, projectName string, options a
}
project, err := s.projectFromName(containers, projectName, services...)
if err != nil {
if err != nil && !api.IsNotFoundError(err) {
return err
}
if len(services) > 0 {
containers = containers.filter(isService(services...))
}
return InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
return s.stopContainers(ctx, w, containers.filter(isService(service)), options.Timeout)
})

View File

@@ -98,8 +98,22 @@ func TestLocalComposeRun(t *testing.T) {
assert.Assert(t, strings.Contains(res.Stdout(), "8081->80/tcp"), res.Stdout())
})
t.Run("compose run orphan", func(t *testing.T) {
// Use different compose files to get an orphan container
c.RunDockerComposeCmd("-f", "./fixtures/run-test/orphan.yaml", "run", "simple")
res := c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
assert.Assert(t, strings.Contains(res.Combined(), "orphan"))
cmd := c.NewDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
res = icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
cmd.Env = append(cmd.Env, "COMPOSE_IGNORE_ORPHANS=True")
})
assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
})
t.Run("down", func(t *testing.T) {
c.RunDockerComposeCmd("-f", "./fixtures/run-test/compose.yaml", "down")
c.RunDockerComposeCmd("-f", "./fixtures/run-test/orphan.yaml", "down")
res := c.RunDockerCmd("ps", "--all")
assert.Assert(t, !strings.Contains(res.Stdout(), "run-test"), res.Stdout())
})

View File

@@ -0,0 +1,5 @@
version: '3.8'
services:
simple:
image: alpine
command: echo "Hi there!!"