浏览代码

Merge pull request #11220 from ndeloof/ps_profile

Introduce ps --orphans to include/exclude services not declared by project
Guillaume Lours 1 年之前
父节点
当前提交
ba24a656bf
共有 3 个文件被更改,包括 22 次插入4 次删除
  1. 11 4
      cmd/compose/ps.go
  2. 1 0
      docs/reference/compose_ps.md
  3. 10 0
      docs/reference/docker_compose_ps.yaml

+ 11 - 4
cmd/compose/ps.go

@@ -42,6 +42,7 @@ type psOptions struct {
 	Filter   string
 	Status   []string
 	noTrunc  bool
+	Orphans  bool
 }
 
 func (p *psOptions) parseFilter() error {
@@ -84,6 +85,7 @@ func psCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *c
 	flags.StringArrayVar(&opts.Status, "status", []string{}, "Filter services by status. Values: [paused | restarting | removing | running | dead | created | exited]")
 	flags.BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
 	flags.BoolVar(&opts.Services, "services", false, "Display services")
+	flags.BoolVar(&opts.Orphans, "orphans", true, "Include orphaned services (not declared by project)")
 	flags.BoolVarP(&opts.All, "all", "a", false, "Show all stopped containers (including those created by the run command)")
 	flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
 	return psCmd
@@ -95,12 +97,17 @@ func runPs(ctx context.Context, dockerCli command.Cli, backend api.Service, serv
 		return err
 	}
 
-	if project != nil && len(services) > 0 {
+	if project != nil {
 		names := project.ServiceNames()
-		for _, service := range services {
-			if !utils.StringContains(names, service) {
-				return fmt.Errorf("no such service: %s", service)
+		if len(services) > 0 {
+			for _, service := range services {
+				if !utils.StringContains(names, service) {
+					return fmt.Errorf("no such service: %s", service)
+				}
 			}
+		} else if !opts.Orphans {
+			// until user asks to list orphaned services, we only include those declared in project
+			services = names
 		}
 	}
 

+ 1 - 0
docs/reference/compose_ps.md

@@ -12,6 +12,7 @@ List containers
 | [`--filter`](#filter) | `string`      |         | Filter services by a property (supported filters: status).                                                                                                                                                                                                                                                                                                                                                                           |
 | [`--format`](#format) | `string`      | `table` | Format output using a custom template:<br>'table':            Print output in table format with column headers (default)<br>'table TEMPLATE':   Print output in table format using the given Go template<br>'json':             Print in JSON format<br>'TEMPLATE':         Print output using the given Go template.<br>Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates |
 | `--no-trunc`          |               |         | Don't truncate output                                                                                                                                                                                                                                                                                                                                                                                                                |
+| `--orphans`           |               |         | Include orphaned services (not declared by project)                                                                                                                                                                                                                                                                                                                                                                                  |
 | `-q`, `--quiet`       |               |         | Only display IDs                                                                                                                                                                                                                                                                                                                                                                                                                     |
 | `--services`          |               |         | Display services                                                                                                                                                                                                                                                                                                                                                                                                                     |
 | [`--status`](#status) | `stringArray` |         | Filter services by status. Values: [paused \| restarting \| removing \| running \| dead \| created \| exited]                                                                                                                                                                                                                                                                                                                        |

+ 10 - 0
docs/reference/docker_compose_ps.yaml

@@ -70,6 +70,16 @@ options:
       experimentalcli: false
       kubernetes: false
       swarm: false
+    - option: orphans
+      value_type: bool
+      default_value: "true"
+      description: Include orphaned services (not declared by project)
+      deprecated: false
+      hidden: false
+      experimental: false
+      experimentalcli: false
+      kubernetes: false
+      swarm: false
     - option: quiet
       shorthand: q
       value_type: bool