Skip to content

Commit

Permalink
introduce --no-attach to ignore some service output
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Jan 3, 2023
1 parent 8b4ac37 commit d5e4f00
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type upOptions struct {
noPrefix bool
attachDependencies bool
attach []string
noAttach []string
timestamp bool
wait bool
}
Expand Down Expand Up @@ -128,6 +129,7 @@ func upCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
flags.BoolVar(&up.attachDependencies, "attach-dependencies", false, "Attach to dependent containers.")
flags.BoolVar(&create.quietPull, "quiet-pull", false, "Pull without printing progress information.")
flags.StringArrayVar(&up.attach, "attach", []string{}, "Attach to service output.")
flags.StringArrayVar(&up.noAttach, "no-attach", []string{}, "Don't attach to specified service.")
flags.BoolVar(&up.wait, "wait", false, "Wait for services to be running|healthy. Implies detached mode.")

return upCmd
Expand Down Expand Up @@ -185,6 +187,7 @@ func runUp(ctx context.Context, streams api.Streams, backend api.Service, create
if len(attachTo) == 0 {
attachTo = project.ServiceNames()
}
attachTo = utils.RemoveAll(attachTo, upOptions.noAttach)

create := api.CreateOptions{
Services: services,
Expand Down
4 changes: 4 additions & 0 deletions docs/reference/compose_up.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Create and start containers
| `-d`, `--detach` | | | Detached mode: Run containers in the background |
| `--exit-code-from` | `string` | | Return the exit code of the selected service container. Implies --abort-on-container-exit |
| `--force-recreate` | | | Recreate containers even if their configuration and image haven't changed. |
| `--no-attach` | `stringArray` | | Don't attach to specified service. |
| `--no-build` | | | Don't build an image, even if it's missing. |
| `--no-color` | | | Produce monochrome output. |
| `--no-deps` | | | Don't start linked services. |
Expand All @@ -40,6 +41,9 @@ Builds, (re)creates, starts, and attaches to containers for a service.
Unless they are already running, this command also starts any linked services.

The `docker compose up` command aggregates the output of each container (like `docker compose logs --follow` does).
One can optionally select a subset of services to attach to using `--attach` flag, or exclude some services using
`--no-attach` to prevent output to be flooded by some verbose services.

When the command exits, all containers are stopped. Running `docker compose up --detach` starts the containers in the
background and leaves them running.

Expand Down
13 changes: 13 additions & 0 deletions docs/reference/docker_compose_up.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ long: |-
Unless they are already running, this command also starts any linked services.
The `docker compose up` command aggregates the output of each container (like `docker compose logs --follow` does).
One can optionally select a subset of services to attach to using `--attach` flag, or exclude some services using
`--no-attach` to prevent output to be flooded by some verbose services.
When the command exits, all containers are stopped. Running `docker compose up --detach` starts the containers in the
background and leaves them running.
Expand Down Expand Up @@ -104,6 +107,16 @@ options:
experimentalcli: false
kubernetes: false
swarm: false
- option: no-attach
value_type: stringArray
default_value: '[]'
description: Don't attach to specified service.
deprecated: false
hidden: false
experimental: false
experimentalcli: false
kubernetes: false
swarm: false
- option: no-build
value_type: bool
default_value: "false"
Expand Down
11 changes: 11 additions & 0 deletions pkg/utils/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ func Contains[T any](origin []T, element T) bool {
}
return false
}

// RemoveAll removes all elements from origin slice
func RemoveAll[T any](origin []T, elements []T) []T {
var filtered []T
for _, v := range origin {
if !Contains(elements, v) {
filtered = append(filtered, v)
}
}
return filtered
}

0 comments on commit d5e4f00

Please sign in to comment.