Skip to content

Commit 1921c62

Browse files
author
Wen Cheng Ma
committed
Re-implement --before and --since as options for --filter
* This commit will mark --before and --since as deprecated, but leave their behavior unchanged until they are removed, then re-implement them as options for --filter. * And update the related docs. * Update the integration tests. Fixes issue #17716 Signed-off-by: Wen Cheng Ma <[email protected]>
1 parent ee03a05 commit 1921c62

File tree

6 files changed

+77
-55
lines changed

6 files changed

+77
-55
lines changed

api/client/ps.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ func (cli *DockerCli) CmdPs(args ...string) error {
2828
size = cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes")
2929
all = cmd.Bool([]string{"a", "-all"}, false, "Show all containers (default shows just running)")
3030
noTrunc = cmd.Bool([]string{"-no-trunc"}, false, "Don't truncate output")
31-
nLatest = cmd.Bool([]string{"l", "-latest"}, false, "Show the latest created container, include non-running")
32-
since = cmd.String([]string{"-since"}, "", "Show created since Id or Name, include non-running")
33-
before = cmd.String([]string{"-before"}, "", "Show only container created before Id or Name")
34-
last = cmd.Int([]string{"n"}, -1, "Show n last created containers, include non-running")
31+
nLatest = cmd.Bool([]string{"l", "-latest"}, false, "Show the latest created container (includes all states)")
32+
since = cmd.String([]string{"#-since"}, "", "Show containers created since Id or Name (includes all states)")
33+
before = cmd.String([]string{"#-before"}, "", "Only show containers created before Id or Name")
34+
last = cmd.Int([]string{"n"}, -1, "Show n last created containers (includes all states)")
3535
format = cmd.String([]string{"-format"}, "", "Pretty-print containers using a Go template")
3636
flFilter = opts.NewListOpts(nil)
3737
)

daemon/list.go

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ type listContext struct {
7171
filters filters.Args
7272
// exitAllowed is a list of exit codes allowed to filter with
7373
exitAllowed []int
74-
// beforeContainer is a filter to ignore containers that appear before the one given
75-
beforeContainer *Container
76-
// sinceContainer is a filter to stop the filtering when the iterator arrive to the given container
77-
sinceContainer *Container
74+
// beforeFilter is a filter to ignore containers that appear before the one given
75+
// this is used for --filter=before= and --before=, the latter is deprecated.
76+
beforeFilter *Container
77+
// sinceFilter is a filter to stop the filtering when the iterator arrive to the given container
78+
// this is used for --filter=since= and --since=, the latter is deprecated.
79+
sinceFilter *Container
7880
// ContainersConfig is the filters set by the user
7981
*ContainersConfig
8082
}
@@ -155,6 +157,25 @@ func (daemon *Daemon) foldFilter(config *ContainersConfig) (*listContext, error)
155157
}
156158
}
157159

160+
var beforeContFilter, sinceContFilter *Container
161+
if i, ok := psFilters["before"]; ok {
162+
for _, value := range i {
163+
beforeContFilter, err = daemon.Get(value)
164+
if err != nil {
165+
return nil, err
166+
}
167+
}
168+
}
169+
170+
if i, ok := psFilters["since"]; ok {
171+
for _, value := range i {
172+
sinceContFilter, err = daemon.Get(value)
173+
if err != nil {
174+
return nil, err
175+
}
176+
}
177+
}
178+
158179
imagesFilter := map[string]bool{}
159180
var ancestorFilter bool
160181
if ancestors, ok := psFilters["ancestor"]; ok {
@@ -183,16 +204,15 @@ func (daemon *Daemon) foldFilter(config *ContainersConfig) (*listContext, error)
183204
return nil
184205
}, 1)
185206

186-
var beforeCont, sinceCont *Container
187207
if config.Before != "" {
188-
beforeCont, err = daemon.Get(config.Before)
208+
beforeContFilter, err = daemon.Get(config.Before)
189209
if err != nil {
190210
return nil, err
191211
}
192212
}
193213

194214
if config.Since != "" {
195-
sinceCont, err = daemon.Get(config.Since)
215+
sinceContFilter, err = daemon.Get(config.Since)
196216
if err != nil {
197217
return nil, err
198218
}
@@ -204,8 +224,8 @@ func (daemon *Daemon) foldFilter(config *ContainersConfig) (*listContext, error)
204224
names: names,
205225
images: imagesFilter,
206226
exitAllowed: filtExited,
207-
beforeContainer: beforeCont,
208-
sinceContainer: sinceCont,
227+
beforeFilter: beforeContFilter,
228+
sinceFilter: sinceContFilter,
209229
ContainersConfig: config,
210230
}, nil
211231
}
@@ -214,7 +234,7 @@ func (daemon *Daemon) foldFilter(config *ContainersConfig) (*listContext, error)
214234
// It also decides if the iteration should be stopped or not.
215235
func includeContainerInList(container *Container, ctx *listContext) iterationAction {
216236
// Do not include container if it's stopped and we're not filters
217-
if !container.Running && !ctx.All && ctx.Limit <= 0 && ctx.beforeContainer == nil && ctx.sinceContainer == nil {
237+
if !container.Running && !ctx.All && ctx.Limit <= 0 && ctx.beforeFilter == nil && ctx.sinceFilter == nil {
218238
return excludeContainer
219239
}
220240

@@ -240,25 +260,25 @@ func includeContainerInList(container *Container, ctx *listContext) iterationAct
240260

241261
// Do not include container if it's in the list before the filter container.
242262
// Set the filter container to nil to include the rest of containers after this one.
243-
if ctx.beforeContainer != nil {
244-
if container.ID == ctx.beforeContainer.ID {
245-
ctx.beforeContainer = nil
263+
if ctx.beforeFilter != nil {
264+
if container.ID == ctx.beforeFilter.ID {
265+
ctx.beforeFilter = nil
246266
}
247267
return excludeContainer
248268
}
249269

250-
// Stop iteration when the index is over the limit
251-
if ctx.Limit > 0 && ctx.idx == ctx.Limit {
252-
return stopIteration
253-
}
254-
255270
// Stop interation when the container arrives to the filter container
256-
if ctx.sinceContainer != nil {
257-
if container.ID == ctx.sinceContainer.ID {
271+
if ctx.sinceFilter != nil {
272+
if container.ID == ctx.sinceFilter.ID {
258273
return stopIteration
259274
}
260275
}
261276

277+
// Stop iteration when the index is over the limit
278+
if ctx.Limit > 0 && ctx.idx == ctx.Limit {
279+
return stopIteration
280+
}
281+
262282
// Do not include container if its exit code is not in the filter
263283
if len(ctx.exitAllowed) > 0 {
264284
shouldSkip := true

docs/misc/deprecated.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ parent = "mn_use_docker"
1212

1313
The following list of features are deprecated.
1414

15+
### Docker ps 'before' and 'since' options
16+
17+
**Deprecated In Release: [v1.10.0](https://github.com/docker/docker/releases/tag/v1.10.0)**
18+
19+
**Target For Removal In Release: v1.12**
20+
21+
The `docker ps --before` and `docker ps --since` options are deprecated.
22+
Use `docker ps --filter=before=...` and `docker ps --filter=since=...` instead.
23+
1524
### Command line short variant options
1625
**Deprecated In Release: v1.9**
1726

docs/reference/commandline/ps.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@ parent = "smn_cli"
1515
List containers
1616

1717
-a, --all=false Show all containers (default shows just running)
18-
--before="" Show only container created before Id or Name
1918
-f, --filter=[] Filter output based on conditions provided
2019
--format=[] Pretty-print containers using a Go template
2120
--help=false Print usage
22-
-l, --latest=false Show the latest created container, include non-running
23-
-n=-1 Show n last created containers, include non-running
21+
-l, --latest=false Show the latest created container (includes all states)
22+
-n=-1 Show n last created containers (includes all states)
2423
--no-trunc=false Don't truncate output
2524
-q, --quiet=false Only display numeric IDs
2625
-s, --size=false Display total file sizes
27-
--since="" Show created since Id or Name, include non-running
2826

2927
Running `docker ps --no-trunc` showing 2 linked containers.
3028

integration-cli/docker_cli_ps_test.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,52 +58,53 @@ func (s *DockerSuite) TestPsListContainersBase(c *check.C) {
5858
out, _ = dockerCmd(c, "ps", "-n=2")
5959
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("LIMIT: Container list is not in the correct order: \n%s", out))
6060

61-
// since
62-
out, _ = dockerCmd(c, "ps", "--since", firstID, "-a")
61+
// filter since
62+
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-a")
6363
expected = []string{fourthID, thirdID, secondID}
6464
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE & ALL: Container list is not in the correct order: \n%s", out))
6565

66-
out, _ = dockerCmd(c, "ps", "--since", firstID)
66+
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID)
6767
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE: Container list is not in the correct order: \n%s", out))
6868

69-
// before
70-
out, _ = dockerCmd(c, "ps", "--before", thirdID, "-a")
69+
// filter before
70+
out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID, "-a")
7171
expected = []string{secondID, firstID}
7272
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE & ALL: Container list is not in the correct order: \n%s", out))
7373

74-
out, _ = dockerCmd(c, "ps", "--before", thirdID)
74+
out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID)
7575
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE: Container list is not in the correct order: \n%s", out))
7676

77-
// since & before
78-
out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-a")
77+
// filter since & before
78+
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-a")
7979
expected = []string{thirdID, secondID}
8080
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE & ALL: Container list is not in the correct order: \n%s", out))
8181

82-
out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID)
82+
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID)
8383
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE: Container list is not in the correct order: \n%s", out))
8484

85-
// since & limit
86-
out, _ = dockerCmd(c, "ps", "--since", firstID, "-n=2", "-a")
85+
// filter since & limit
86+
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2", "-a")
8787
expected = []string{fourthID, thirdID}
8888

8989
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
9090

91-
out, _ = dockerCmd(c, "ps", "--since", firstID, "-n=2")
91+
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2")
9292
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, LIMIT: Container list is not in the correct order: \n%s", out))
9393

94-
// before & limit
95-
out, _ = dockerCmd(c, "ps", "--before", fourthID, "-n=1", "-a")
94+
// filter before & limit
95+
out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1", "-a")
9696
expected = []string{thirdID}
9797
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
9898

99-
out, _ = dockerCmd(c, "ps", "--before", fourthID, "-n=1")
99+
out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1")
100100
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE, LIMIT: Container list is not in the correct order: \n%s", out))
101101

102-
out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a")
102+
// filter since & filter before & limit
103+
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1", "-a")
103104
expected = []string{thirdID}
104105
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
105106

106-
out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-n=1")
107+
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1")
107108
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE, LIMIT: Container list is not in the correct order: \n%s", out))
108109

109110
}

man/docker-ps.1.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ docker-ps - List containers
77
# SYNOPSIS
88
**docker ps**
99
[**-a**|**--all**[=*false*]]
10-
[**--before**[=*BEFORE*]]
1110
[**-f**|**--filter**[=*[]*]]
1211
[**--format**=*"TEMPLATE"*]
1312
[**--help**]
@@ -16,7 +15,6 @@ docker-ps - List containers
1615
[**--no-trunc**[=*false*]]
1716
[**-q**|**--quiet**[=*false*]]
1817
[**-s**|**--size**[=*false*]]
19-
[**--since**[=*SINCE*]]
2018

2119
# DESCRIPTION
2220

@@ -27,16 +25,15 @@ the running containers.
2725
**-a**, **--all**=*true*|*false*
2826
Show all containers. Only running containers are shown by default. The default is *false*.
2927

30-
**--before**=""
31-
Show only containers created before Id or Name, including non-running containers.
32-
3328
**-f**, **--filter**=[]
3429
Provide filter values. Valid filters:
3530
exited=<int> - containers with exit code of <int>
3631
label=<key> or label=<key>=<value>
3732
status=(created|restarting|running|paused|exited)
3833
name=<string> - container's name
3934
id=<ID> - container's ID
35+
before=(<container-name>|<container-id>)
36+
since=(<container-name>|<container-id>)
4037
ancestor=(<image-name>[:tag]|<image-id>|<image@digest>) - filters containers that were
4138
created from the given image or a descendant.
4239

@@ -58,10 +55,10 @@ the running containers.
5855
Print usage statement
5956

6057
**-l**, **--latest**=*true*|*false*
61-
Show only the latest created container, include non-running ones. The default is *false*.
58+
Show only the latest created container (includes all states). The default is *false*.
6259

6360
**-n**=*-1*
64-
Show n last created containers, include non-running ones.
61+
Show n last created containers (includes all states).
6562

6663
**--no-trunc**=*true*|*false*
6764
Don't truncate output. The default is *false*.
@@ -72,9 +69,6 @@ the running containers.
7269
**-s**, **--size**=*true*|*false*
7370
Display total file sizes. The default is *false*.
7471

75-
**--since**=""
76-
Show only containers created since Id or Name, include non-running ones.
77-
7872
# EXAMPLES
7973
# Display all containers, including non-running
8074

0 commit comments

Comments
 (0)