Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce --no-attach to ignore some service output #10137

Merged
merged 1 commit into from Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/compose/up.go
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
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
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
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
}