Skip to content

Commit caa0cbb

Browse files
committed
Introduce ps --orphans so user can include/exclude services not declared by project
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 8c0c5cb commit caa0cbb

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

cmd/compose/ps.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type psOptions struct {
4242
Filter string
4343
Status []string
4444
noTrunc bool
45+
Orphans bool
4546
}
4647

4748
func (p *psOptions) parseFilter() error {
@@ -84,6 +85,7 @@ func psCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *c
8485
flags.StringArrayVar(&opts.Status, "status", []string{}, "Filter services by status. Values: [paused | restarting | removing | running | dead | created | exited]")
8586
flags.BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
8687
flags.BoolVar(&opts.Services, "services", false, "Display services")
88+
flags.BoolVar(&opts.Orphans, "orphans", true, "Include orphaned services (not declared by project)")
8789
flags.BoolVarP(&opts.All, "all", "a", false, "Show all stopped containers (including those created by the run command)")
8890
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
8991
return psCmd
@@ -95,12 +97,17 @@ func runPs(ctx context.Context, dockerCli command.Cli, backend api.Service, serv
9597
return err
9698
}
9799

98-
if project != nil && len(services) > 0 {
100+
if project != nil {
99101
names := project.ServiceNames()
100-
for _, service := range services {
101-
if !utils.StringContains(names, service) {
102-
return fmt.Errorf("no such service: %s", service)
102+
if len(services) > 0 {
103+
for _, service := range services {
104+
if !utils.StringContains(names, service) {
105+
return fmt.Errorf("no such service: %s", service)
106+
}
103107
}
108+
} else if !opts.Orphans {
109+
// until user asks to list orphaned services, we only include those declared in project
110+
services = names
104111
}
105112
}
106113

docs/reference/compose_ps.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ List containers
1212
| [`--filter`](#filter) | `string` | | Filter services by a property (supported filters: status). |
1313
| [`--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 |
1414
| `--no-trunc` | | | Don't truncate output |
15+
| `--orphans` | | | Include orphaned services (not declared by project) |
1516
| `-q`, `--quiet` | | | Only display IDs |
1617
| `--services` | | | Display services |
1718
| [`--status`](#status) | `stringArray` | | Filter services by status. Values: [paused \| restarting \| removing \| running \| dead \| created \| exited] |

docs/reference/docker_compose_ps.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ options:
7070
experimentalcli: false
7171
kubernetes: false
7272
swarm: false
73+
- option: orphans
74+
value_type: bool
75+
default_value: "true"
76+
description: Include orphaned services (not declared by project)
77+
deprecated: false
78+
hidden: false
79+
experimental: false
80+
experimentalcli: false
81+
kubernetes: false
82+
swarm: false
7383
- option: quiet
7484
shorthand: q
7585
value_type: bool

0 commit comments

Comments
 (0)