diff --git a/api/handlers.go b/api/handlers.go index 3d09f077b3..5e97a5c185 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -152,8 +152,18 @@ func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) { } // Parse flags. - all := boolValue(r, "all") - limit := intValueOrZero(r, "limit") + var ( + all = boolValue(r, "all") + limit = intValueOrZero(r, "limit") + before *cluster.Container + ) + if value := r.FormValue("before"); value != "" { + before = c.cluster.Container(value) + if before == nil { + httpError(w, fmt.Sprintf("No such container %s", value), http.StatusNotFound) + return + } + } // Parse filters. filters, err := dockerfilters.FromParam(r.Form.Get("filters")) @@ -184,7 +194,7 @@ func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) { candidates := []*cluster.Container{} for _, container := range c.cluster.Containers() { // Skip stopped containers unless -a was specified. - if !container.Info.State.Running && !all && limit <= 0 { + if !container.Info.State.Running && !all && before == nil && limit <= 0 { continue } @@ -232,6 +242,12 @@ func getContainersJSON(c *context, w http.ResponseWriter, r *http.Request) { // Convert cluster.Container back into dockerclient.Container. out := []*dockerclient.Container{} for _, container := range candidates { + if before != nil { + if container.Id == before.Id { + before = nil + } + continue + } // Create a copy of the underlying dockerclient.Container so we can // make changes without messing with cluster.Container. tmp := (*container).Container diff --git a/test/integration/api/ps.bats b/test/integration/api/ps.bats index 014fea6a6e..013317b2d3 100644 --- a/test/integration/api/ps.bats +++ b/test/integration/api/ps.bats @@ -45,6 +45,23 @@ function teardown() { [[ "${lines[1]}" == *"false"* ]] } +@test "docker ps --before" { + start_docker_with_busybox 2 + swarm_manage + + docker_swarm run -d --name c1 busybox echo c1 + docker_swarm run -d --name c2 busybox echo c2 + + run docker_swarm ps --before c1 + [ "${#lines[@]}" -eq 1 ] + + run docker_swarm ps --before c2 + [ "${#lines[@]}" -eq 2 ] + + run docker_swarm ps --before c3 + [ "$status" -eq 1 ] +} + @test "docker ps --filter" { start_docker_with_busybox 2 swarm_manage