2020-05-14 21:13:07 +02:00
/ *
2020-09-22 12:13:00 +02:00
Copyright 2020 Docker Compose CLI authors
2020-05-14 21:13:07 +02:00
2020-06-18 16:13:24 +02:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
2020-05-14 21:13:07 +02:00
2020-06-18 16:13:24 +02:00
http : //www.apache.org/licenses/LICENSE-2.0
2020-05-14 21:13:07 +02:00
2020-06-18 16:13:24 +02:00
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
2020-05-14 21:13:07 +02:00
* /
2020-05-14 21:04:38 +02:00
package compose
import (
"context"
up: handle various attach use cases better
By default, `compose up` attaches to all services (i.e.
shows log output from every associated container). If
a service is specified, e.g. `compose up foo`, then
only `foo`'s logs are tailed. The `--attach-dependencies`
flag can also be used, so that if `foo` depended upon
`bar`, then `bar`'s logs would also be followed. It's
also possible to use `--no-attach` to filter out one
or more services explicitly, e.g. `compose up --no-attach=noisy`
would launch all services, including `noisy`, and would
show log output from every service _except_ `noisy`.
Lastly, it's possible to use `up --attach` to explicitly
restrict to a subset of services (or their dependencies).
How these flags interact with each other is also worth
thinking through.
There were a few different connected issues here, but
the primary issue was that running `compose up foo` was
always attaching dependencies regardless of `--attach-dependencies`.
The filtering logic here has been updated so that it
behaves predictably both when launching all services
(`compose up`) or a subset (`compose up foo`) as well
as various flag combinations on top of those.
Notably, this required making some changes to how it
watches containers. The logic here between attaching
for logs and monitoring for lifecycle changes is
tightly coupled, so some changes were needed to ensure
that the full set of services being `up`'d are _watched_
and the subset that should have logs shown are _attached_.
(This does mean faking the attach with an event but not
actually doing it.)
While handling that, I adjusted the context lifetimes
here, which improves error handling that gets shown to
the user and should help avoid potential leaks by getting
rid of a `context.Background()`.
Signed-off-by: Milas Bowman <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
"errors"
2020-12-02 18:23:01 +01:00
"fmt"
2024-02-17 07:14:56 +05:30
"os"
up: handle various attach use cases better
By default, `compose up` attaches to all services (i.e.
shows log output from every associated container). If
a service is specified, e.g. `compose up foo`, then
only `foo`'s logs are tailed. The `--attach-dependencies`
flag can also be used, so that if `foo` depended upon
`bar`, then `bar`'s logs would also be followed. It's
also possible to use `--no-attach` to filter out one
or more services explicitly, e.g. `compose up --no-attach=noisy`
would launch all services, including `noisy`, and would
show log output from every service _except_ `noisy`.
Lastly, it's possible to use `up --attach` to explicitly
restrict to a subset of services (or their dependencies).
How these flags interact with each other is also worth
thinking through.
There were a few different connected issues here, but
the primary issue was that running `compose up foo` was
always attaching dependencies regardless of `--attach-dependencies`.
The filtering logic here has been updated so that it
behaves predictably both when launching all services
(`compose up`) or a subset (`compose up foo`) as well
as various flag combinations on top of those.
Notably, this required making some changes to how it
watches containers. The logic here between attaching
for logs and monitoring for lifecycle changes is
tightly coupled, so some changes were needed to ensure
that the full set of services being `up`'d are _watched_
and the subset that should have logs shown are _attached_.
(This does mean faking the attach with an event but not
actually doing it.)
While handling that, I adjusted the context lifetimes
here, which improves error handling that gets shown to
the user and should help avoid potential leaks by getting
rid of a `context.Background()`.
Signed-off-by: Milas Bowman <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
"strings"
2023-02-13 17:42:11 +01:00
"time"
2020-05-14 21:04:38 +02:00
2023-11-08 10:19:24 +01:00
"github.com/compose-spec/compose-go/v2/types"
2023-08-30 10:15:35 +02:00
"github.com/docker/cli/cli/command"
2024-02-19 15:38:45 +01:00
xprogress "github.com/moby/buildkit/util/progress/progressui"
2025-03-11 08:03:17 +01:00
"github.com/sirupsen/logrus"
2021-06-15 09:57:38 +02:00
"github.com/spf13/cobra"
2025-03-11 08:03:17 +01:00
"github.com/spf13/pflag"
2021-06-15 09:57:38 +02:00
2024-11-22 23:31:04 -05:00
"github.com/docker/compose/v2/cmd/formatter"
2021-08-31 18:53:24 +02:00
"github.com/docker/compose/v2/pkg/api"
2024-04-24 19:59:42 +01:00
ui "github.com/docker/compose/v2/pkg/progress"
2021-08-31 18:53:24 +02:00
"github.com/docker/compose/v2/pkg/utils"
2020-05-14 21:04:38 +02:00
)
2021-01-20 11:02:46 +01:00
// composeOptions hold options common to `up` and `run` to run compose project
type composeOptions struct {
2022-12-19 16:38:36 +00:00
* ProjectOptions
2021-01-20 11:02:46 +01:00
}
2021-01-06 14:23:37 +01:00
type upOptions struct {
2021-01-20 11:02:46 +01:00
* composeOptions
2024-03-08 14:07:51 +00:00
Detach bool
noStart bool
noDeps bool
cascadeStop bool
2024-04-03 09:51:11 +02:00
cascadeFail bool
2024-03-08 14:07:51 +00:00
exitCodeFrom string
noColor bool
noPrefix bool
attachDependencies bool
attach [ ] string
noAttach [ ] string
timestamp bool
wait bool
waitTimeout int
watch bool
navigationMenu bool
navigationMenuChanged bool
2021-03-02 14:33:26 +01:00
}
2023-12-29 11:45:45 +01:00
func ( opts upOptions ) apply ( project * types . Project , services [ ] string ) ( * types . Project , error ) {
2021-03-02 09:30:26 +01:00
if opts . noDeps {
2023-12-29 11:45:45 +01:00
var err error
project , err = project . WithSelectedServices ( services , types . IgnoreDependencies )
2021-03-02 09:30:26 +01:00
if err != nil {
2023-12-29 11:45:45 +01:00
return nil , err
2021-03-02 09:30:26 +01:00
}
}
if opts . exitCodeFrom != "" {
_ , err := project . GetService ( opts . exitCodeFrom )
if err != nil {
2023-12-29 11:45:45 +01:00
return nil , err
2021-03-02 09:30:26 +01:00
}
}
2023-12-29 11:45:45 +01:00
return project , nil
2021-03-02 09:30:26 +01:00
}
2024-10-07 15:37:22 +01:00
func ( opts * upOptions ) validateNavigationMenu ( dockerCli command . Cli ) {
2024-03-28 15:02:00 +00:00
if ! dockerCli . Out ( ) . IsTerminal ( ) {
opts . navigationMenu = false
return
}
2024-10-07 15:37:22 +01:00
// If --menu flag was not set
2024-03-28 15:02:00 +00:00
if ! opts . navigationMenuChanged {
2024-10-07 15:37:22 +01:00
if envVar , ok := os . LookupEnv ( ComposeMenu ) ; ok {
opts . navigationMenu = utils . StringToBool ( envVar )
return
}
// ...and COMPOSE_MENU env var is not defined we want the default value to be true
opts . navigationMenu = true
2024-03-28 15:02:00 +00:00
}
}
2024-04-03 09:51:11 +02:00
func ( opts upOptions ) OnExit ( ) api . Cascade {
switch {
case opts . cascadeStop :
return api . CascadeStop
case opts . cascadeFail :
return api . CascadeFail
default :
return api . CascadeIgnore
}
}
2024-10-07 15:37:22 +01:00
func upCommand ( p * ProjectOptions , dockerCli command . Cli , backend api . Service ) * cobra . Command {
2021-06-07 14:21:55 +02:00
up := upOptions { }
create := createOptions { }
2023-08-30 08:47:09 -04:00
build := buildOptions { ProjectOptions : p }
2020-05-14 21:04:38 +02:00
upCmd := & cobra . Command {
2022-08-02 13:11:57 +02:00
Use : "up [OPTIONS] [SERVICE...]" ,
2020-12-10 15:43:20 +01:00
Short : "Create and start containers" ,
2021-10-13 09:11:44 +05:30
PreRunE : AdaptCmd ( func ( ctx context . Context , cmd * cobra . Command , args [ ] string ) error {
2022-12-29 13:20:17 +00:00
create . pullChanged = cmd . Flags ( ) . Changed ( "pull" )
2023-06-12 15:18:25 +02:00
create . timeChanged = cmd . Flags ( ) . Changed ( "timeout" )
2024-03-08 14:07:51 +00:00
up . navigationMenuChanged = cmd . Flags ( ) . Changed ( "menu" )
2024-04-11 09:21:19 +02:00
if ! cmd . Flags ( ) . Changed ( "remove-orphans" ) {
create . removeOrphans = utils . StringToBool ( os . Getenv ( ComposeRemoveOrphans ) )
}
2021-10-11 17:52:31 +02:00
return validateFlags ( & up , & create )
2021-05-19 18:49:33 +02:00
} ) ,
2023-08-30 10:15:35 +02:00
RunE : p . WithServices ( dockerCli , func ( ctx context . Context , project * types . Project , services [ ] string ) error {
2023-05-14 16:50:41 +02:00
create . ignoreOrphans = utils . StringToBool ( project . Environment [ ComposeIgnoreOrphans ] )
2021-09-24 08:22:50 +02:00
if create . ignoreOrphans && create . removeOrphans {
up: handle various attach use cases better
By default, `compose up` attaches to all services (i.e.
shows log output from every associated container). If
a service is specified, e.g. `compose up foo`, then
only `foo`'s logs are tailed. The `--attach-dependencies`
flag can also be used, so that if `foo` depended upon
`bar`, then `bar`'s logs would also be followed. It's
also possible to use `--no-attach` to filter out one
or more services explicitly, e.g. `compose up --no-attach=noisy`
would launch all services, including `noisy`, and would
show log output from every service _except_ `noisy`.
Lastly, it's possible to use `up --attach` to explicitly
restrict to a subset of services (or their dependencies).
How these flags interact with each other is also worth
thinking through.
There were a few different connected issues here, but
the primary issue was that running `compose up foo` was
always attaching dependencies regardless of `--attach-dependencies`.
The filtering logic here has been updated so that it
behaves predictably both when launching all services
(`compose up`) or a subset (`compose up foo`) as well
as various flag combinations on top of those.
Notably, this required making some changes to how it
watches containers. The logic here between attaching
for logs and monitoring for lifecycle changes is
tightly coupled, so some changes were needed to ensure
that the full set of services being `up`'d are _watched_
and the subset that should have logs shown are _attached_.
(This does mean faking the attach with an event but not
actually doing it.)
While handling that, I adjusted the context lifetimes
here, which improves error handling that gets shown to
the user and should help avoid potential leaks by getting
rid of a `context.Background()`.
Signed-off-by: Milas Bowman <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
return fmt . Errorf ( "cannot combine %s and --remove-orphans" , ComposeIgnoreOrphans )
}
if len ( up . attach ) != 0 && up . attachDependencies {
return errors . New ( "cannot combine --attach and --attach-dependencies" )
2021-09-24 08:22:50 +02:00
}
2024-03-28 15:02:00 +00:00
2024-10-07 15:37:22 +01:00
up . validateNavigationMenu ( dockerCli )
2024-03-28 15:02:00 +00:00
2024-04-09 11:32:00 +02:00
if ! p . All && len ( project . Services ) == 0 {
return fmt . Errorf ( "no service selected" )
}
2024-03-28 15:02:00 +00:00
return runUp ( ctx , dockerCli , backend , create , up , build , project , services )
2021-04-15 12:43:18 +02:00
} ) ,
2023-08-30 10:15:35 +02:00
ValidArgsFunction : completeServiceNames ( dockerCli , p ) ,
2020-05-14 21:04:38 +02:00
}
2021-01-20 11:02:46 +01:00
flags := upCmd . Flags ( )
2021-06-07 14:21:55 +02:00
flags . BoolVarP ( & up . Detach , "detach" , "d" , false , "Detached mode: Run containers in the background" )
2024-02-14 20:02:37 +01:00
flags . BoolVar ( & create . Build , "build" , false , "Build images before starting containers" )
flags . BoolVar ( & create . noBuild , "no-build" , false , "Don't build an image, even if it's policy" )
2023-11-07 08:21:19 +01:00
flags . StringVar ( & create . Pull , "pull" , "policy" , ` Pull image before running ("always"|"missing"|"never") ` )
2024-04-11 09:21:19 +02:00
flags . BoolVar ( & create . removeOrphans , "remove-orphans" , false , "Remove containers for services not defined in the Compose file" )
2023-01-26 12:47:09 +01:00
flags . StringArrayVar ( & create . scale , "scale" , [ ] string { } , "Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present." )
2024-02-14 20:02:37 +01:00
flags . BoolVar ( & up . noColor , "no-color" , false , "Produce monochrome output" )
flags . BoolVar ( & up . noPrefix , "no-log-prefix" , false , "Don't print prefix in logs" )
flags . BoolVar ( & create . forceRecreate , "force-recreate" , false , "Recreate containers even if their configuration and image haven't changed" )
2021-06-07 14:21:55 +02:00
flags . BoolVar ( & create . noRecreate , "no-recreate" , false , "If containers already exist, don't recreate them. Incompatible with --force-recreate." )
2024-02-14 20:02:37 +01:00
flags . BoolVar ( & up . noStart , "no-start" , false , "Don't start the services after creating them" )
2021-06-07 14:21:55 +02:00
flags . BoolVar ( & up . cascadeStop , "abort-on-container-exit" , false , "Stops all containers if any container was stopped. Incompatible with -d" )
2024-04-03 09:51:11 +02:00
flags . BoolVar ( & up . cascadeFail , "abort-on-container-failure" , false , "Stops all containers if any container exited with failure. Incompatible with -d" )
2021-06-07 14:21:55 +02:00
flags . StringVar ( & up . exitCodeFrom , "exit-code-from" , "" , "Return the exit code of the selected service container. Implies --abort-on-container-exit" )
2024-02-14 20:02:37 +01:00
flags . IntVarP ( & create . timeout , "timeout" , "t" , 0 , "Use this timeout in seconds for container shutdown when attached or when containers are already running" )
flags . BoolVar ( & up . timestamp , "timestamps" , false , "Show timestamps" )
flags . BoolVar ( & up . noDeps , "no-deps" , false , "Don't start linked services" )
2021-06-07 14:21:55 +02:00
flags . BoolVar ( & create . recreateDeps , "always-recreate-deps" , false , "Recreate dependent containers. Incompatible with --no-recreate." )
2024-02-14 20:02:37 +01:00
flags . BoolVarP ( & create . noInherit , "renew-anon-volumes" , "V" , false , "Recreate anonymous volumes instead of retrieving data from the previous containers" )
flags . BoolVar ( & create . quietPull , "quiet-pull" , false , "Pull without printing progress information" )
up: handle various attach use cases better
By default, `compose up` attaches to all services (i.e.
shows log output from every associated container). If
a service is specified, e.g. `compose up foo`, then
only `foo`'s logs are tailed. The `--attach-dependencies`
flag can also be used, so that if `foo` depended upon
`bar`, then `bar`'s logs would also be followed. It's
also possible to use `--no-attach` to filter out one
or more services explicitly, e.g. `compose up --no-attach=noisy`
would launch all services, including `noisy`, and would
show log output from every service _except_ `noisy`.
Lastly, it's possible to use `up --attach` to explicitly
restrict to a subset of services (or their dependencies).
How these flags interact with each other is also worth
thinking through.
There were a few different connected issues here, but
the primary issue was that running `compose up foo` was
always attaching dependencies regardless of `--attach-dependencies`.
The filtering logic here has been updated so that it
behaves predictably both when launching all services
(`compose up`) or a subset (`compose up foo`) as well
as various flag combinations on top of those.
Notably, this required making some changes to how it
watches containers. The logic here between attaching
for logs and monitoring for lifecycle changes is
tightly coupled, so some changes were needed to ensure
that the full set of services being `up`'d are _watched_
and the subset that should have logs shown are _attached_.
(This does mean faking the attach with an event but not
actually doing it.)
While handling that, I adjusted the context lifetimes
here, which improves error handling that gets shown to
the user and should help avoid potential leaks by getting
rid of a `context.Background()`.
Signed-off-by: Milas Bowman <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
flags . StringArrayVar ( & up . attach , "attach" , [ ] string { } , "Restrict attaching to the specified services. Incompatible with --attach-dependencies." )
2024-02-14 20:02:37 +01:00
flags . StringArrayVar ( & up . noAttach , "no-attach" , [ ] string { } , "Do not attach (stream logs) to the specified services" )
flags . BoolVar ( & up . attachDependencies , "attach-dependencies" , false , "Automatically attach to log output of dependent services" )
2021-10-11 17:52:31 +02:00
flags . BoolVar ( & up . wait , "wait" , false , "Wait for services to be running|healthy. Implies detached mode." )
2024-11-22 23:31:04 -05:00
flags . IntVar ( & up . waitTimeout , "wait-timeout" , 0 , "Maximum duration in seconds to wait for the project to be running|healthy" )
2024-02-19 15:38:45 +01:00
flags . BoolVarP ( & up . watch , "watch" , "w" , false , "Watch source code and rebuild/refresh containers when files are updated." )
2024-05-07 13:42:08 +01:00
flags . BoolVar ( & up . navigationMenu , "menu" , false , "Enable interactive shortcuts when running attached. Incompatible with --detach. Can also be enable/disable by setting COMPOSE_MENU environment var." )
2025-03-11 08:03:17 +01:00
flags . BoolVarP ( & create . AssumeYes , "yes" , "y" , false , ` Assume "yes" as answer to all prompts and run non-interactively ` )
flags . SetNormalizeFunc ( func ( f * pflag . FlagSet , name string ) pflag . NormalizedName {
// assumeYes was introduced by mistake as `--y`
if name == "y" {
logrus . Warn ( "--y is deprecated, please use --yes instead" )
name = "yes"
}
return pflag . NormalizedName ( name )
} )
2020-05-14 21:04:38 +02:00
return upCmd
}
2024-03-26 17:53:50 +00:00
//nolint:gocyclo
2021-10-11 17:52:31 +02:00
func validateFlags ( up * upOptions , create * createOptions ) error {
2024-04-03 09:51:11 +02:00
if up . exitCodeFrom != "" && ! up . cascadeFail {
2021-10-11 17:52:31 +02:00
up . cascadeStop = true
}
2024-04-03 09:51:11 +02:00
if up . cascadeStop && up . cascadeFail {
return fmt . Errorf ( "--abort-on-container-failure cannot be combined with --abort-on-container-exit" )
}
2021-10-11 17:52:31 +02:00
if up . wait {
if up . attachDependencies || up . cascadeStop || len ( up . attach ) > 0 {
return fmt . Errorf ( "--wait cannot be combined with --abort-on-container-exit, --attach or --attach-dependencies" )
}
up . Detach = true
}
if create . Build && create . noBuild {
return fmt . Errorf ( "--build and --no-build are incompatible" )
}
2024-06-07 13:45:31 +02:00
if up . Detach && ( up . attachDependencies || up . cascadeStop || up . cascadeFail || len ( up . attach ) > 0 || up . watch ) {
2025-02-21 09:39:16 +01:00
if up . wait {
return fmt . Errorf ( "--wait cannot be combined with --abort-on-container-exit, --abort-on-container-failure, --attach, --attach-dependencies or --watch" )
} else {
return fmt . Errorf ( "--detach cannot be combined with --abort-on-container-exit, --abort-on-container-failure, --attach, --attach-dependencies or --watch" )
}
2021-10-11 17:52:31 +02:00
}
2024-09-11 12:18:45 +02:00
if create . noInherit && create . noRecreate {
return fmt . Errorf ( "--no-recreate and --renew-anon-volumes are incompatible" )
}
2021-10-11 17:52:31 +02:00
if create . forceRecreate && create . noRecreate {
return fmt . Errorf ( "--force-recreate and --no-recreate are incompatible" )
}
if create . recreateDeps && create . noRecreate {
return fmt . Errorf ( "--always-recreate-deps and --no-recreate are incompatible" )
}
2024-03-26 17:53:50 +00:00
if create . noBuild && up . watch {
return fmt . Errorf ( "--no-build and --watch are incompatible" )
}
2021-10-11 17:52:31 +02:00
return nil
}
2023-08-30 08:47:09 -04:00
func runUp (
ctx context . Context ,
2023-08-30 10:15:35 +02:00
dockerCli command . Cli ,
2023-08-30 08:47:09 -04:00
backend api . Service ,
createOptions createOptions ,
upOptions upOptions ,
buildOptions buildOptions ,
project * types . Project ,
services [ ] string ,
) error {
2025-03-05 12:04:28 +01:00
if err := checksForRemoteStack ( ctx , dockerCli , project , buildOptions , createOptions . AssumeYes , [ ] string { } ) ; err != nil {
return err
}
2023-01-26 12:47:09 +01:00
err := createOptions . Apply ( project )
if err != nil {
return err
}
2021-06-07 14:21:55 +02:00
2023-12-29 11:45:45 +01:00
project , err = upOptions . apply ( project , services )
2020-12-01 20:58:03 +01:00
if err != nil {
return err
}
2020-12-02 18:23:01 +01:00
2023-08-30 08:47:09 -04:00
var build * api . BuildOptions
if ! createOptions . noBuild {
if createOptions . quietPull {
2023-11-22 18:52:32 +01:00
buildOptions . Progress = string ( xprogress . QuietMode )
2023-08-30 08:47:09 -04:00
}
// BuildOptions here is nested inside CreateOptions, so
// no service list is passed, it will implicitly pick all
// services being created, which includes any explicitly
// specified via "services" arg here as well as deps
bo , err := buildOptions . toAPIBuildOptions ( nil )
if err != nil {
return err
}
2025-05-22 14:13:41 +02:00
bo . Services = services
2025-06-02 11:45:48 +02:00
bo . Deps = ! upOptions . noDeps
2023-08-30 08:47:09 -04:00
build = & bo
}
2021-06-14 16:26:14 +02:00
create := api . CreateOptions {
2023-08-30 08:47:09 -04:00
Build : build ,
2021-09-20 12:31:41 +02:00
Services : services ,
2021-06-07 14:21:55 +02:00
RemoveOrphans : createOptions . removeOrphans ,
2021-09-24 08:22:50 +02:00
IgnoreOrphans : createOptions . ignoreOrphans ,
2021-06-07 14:21:55 +02:00
Recreate : createOptions . recreateStrategy ( ) ,
RecreateDependencies : createOptions . dependenciesRecreateStrategy ( ) ,
Inherit : ! createOptions . noInherit ,
Timeout : createOptions . GetTimeout ( ) ,
QuietPull : createOptions . quietPull ,
2024-12-12 09:36:24 +01:00
AssumeYes : createOptions . AssumeYes ,
2020-12-02 18:23:01 +01:00
}
2021-06-07 14:21:55 +02:00
if upOptions . noStart {
return backend . Create ( ctx , project , create )
2021-02-09 16:51:48 +01:00
}
2021-02-08 11:42:42 +01:00
up: handle various attach use cases better
By default, `compose up` attaches to all services (i.e.
shows log output from every associated container). If
a service is specified, e.g. `compose up foo`, then
only `foo`'s logs are tailed. The `--attach-dependencies`
flag can also be used, so that if `foo` depended upon
`bar`, then `bar`'s logs would also be followed. It's
also possible to use `--no-attach` to filter out one
or more services explicitly, e.g. `compose up --no-attach=noisy`
would launch all services, including `noisy`, and would
show log output from every service _except_ `noisy`.
Lastly, it's possible to use `up --attach` to explicitly
restrict to a subset of services (or their dependencies).
How these flags interact with each other is also worth
thinking through.
There were a few different connected issues here, but
the primary issue was that running `compose up foo` was
always attaching dependencies regardless of `--attach-dependencies`.
The filtering logic here has been updated so that it
behaves predictably both when launching all services
(`compose up`) or a subset (`compose up foo`) as well
as various flag combinations on top of those.
Notably, this required making some changes to how it
watches containers. The logic here between attaching
for logs and monitoring for lifecycle changes is
tightly coupled, so some changes were needed to ensure
that the full set of services being `up`'d are _watched_
and the subset that should have logs shown are _attached_.
(This does mean faking the attach with an event but not
actually doing it.)
While handling that, I adjusted the context lifetimes
here, which improves error handling that gets shown to
the user and should help avoid potential leaks by getting
rid of a `context.Background()`.
Signed-off-by: Milas Bowman <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
var consumer api . LogConsumer
var attach [ ] string
if ! upOptions . Detach {
2023-08-30 10:15:35 +02:00
consumer = formatter . NewLogConsumer ( ctx , dockerCli . Out ( ) , dockerCli . Err ( ) , ! upOptions . noColor , ! upOptions . noPrefix , upOptions . timestamp )
2023-02-13 17:42:11 +01:00
up: handle various attach use cases better
By default, `compose up` attaches to all services (i.e.
shows log output from every associated container). If
a service is specified, e.g. `compose up foo`, then
only `foo`'s logs are tailed. The `--attach-dependencies`
flag can also be used, so that if `foo` depended upon
`bar`, then `bar`'s logs would also be followed. It's
also possible to use `--no-attach` to filter out one
or more services explicitly, e.g. `compose up --no-attach=noisy`
would launch all services, including `noisy`, and would
show log output from every service _except_ `noisy`.
Lastly, it's possible to use `up --attach` to explicitly
restrict to a subset of services (or their dependencies).
How these flags interact with each other is also worth
thinking through.
There were a few different connected issues here, but
the primary issue was that running `compose up foo` was
always attaching dependencies regardless of `--attach-dependencies`.
The filtering logic here has been updated so that it
behaves predictably both when launching all services
(`compose up`) or a subset (`compose up foo`) as well
as various flag combinations on top of those.
Notably, this required making some changes to how it
watches containers. The logic here between attaching
for logs and monitoring for lifecycle changes is
tightly coupled, so some changes were needed to ensure
that the full set of services being `up`'d are _watched_
and the subset that should have logs shown are _attached_.
(This does mean faking the attach with an event but not
actually doing it.)
While handling that, I adjusted the context lifetimes
here, which improves error handling that gets shown to
the user and should help avoid potential leaks by getting
rid of a `context.Background()`.
Signed-off-by: Milas Bowman <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
var attachSet utils . Set [ string ]
if len ( upOptions . attach ) != 0 {
// services are passed explicitly with --attach, verify they're valid and then use them as-is
attachSet = utils . NewSet ( upOptions . attach ... )
unexpectedSvcs := attachSet . Diff ( utils . NewSet ( project . ServiceNames ( ) ... ) )
if len ( unexpectedSvcs ) != 0 {
return fmt . Errorf ( "cannot attach to services not included in up: %s" , strings . Join ( unexpectedSvcs . Elements ( ) , ", " ) )
}
} else {
// mark services being launched (and potentially their deps) for attach
// if they didn't opt-out via Compose YAML
attachSet = utils . NewSet [ string ] ( )
var dependencyOpt types . DependencyOption = types . IgnoreDependencies
if upOptions . attachDependencies {
dependencyOpt = types . IncludeDependencies
}
2023-12-29 11:45:45 +01:00
if err := project . ForEachService ( services , func ( serviceName string , s * types . ServiceConfig ) error {
up: handle various attach use cases better
By default, `compose up` attaches to all services (i.e.
shows log output from every associated container). If
a service is specified, e.g. `compose up foo`, then
only `foo`'s logs are tailed. The `--attach-dependencies`
flag can also be used, so that if `foo` depended upon
`bar`, then `bar`'s logs would also be followed. It's
also possible to use `--no-attach` to filter out one
or more services explicitly, e.g. `compose up --no-attach=noisy`
would launch all services, including `noisy`, and would
show log output from every service _except_ `noisy`.
Lastly, it's possible to use `up --attach` to explicitly
restrict to a subset of services (or their dependencies).
How these flags interact with each other is also worth
thinking through.
There were a few different connected issues here, but
the primary issue was that running `compose up foo` was
always attaching dependencies regardless of `--attach-dependencies`.
The filtering logic here has been updated so that it
behaves predictably both when launching all services
(`compose up`) or a subset (`compose up foo`) as well
as various flag combinations on top of those.
Notably, this required making some changes to how it
watches containers. The logic here between attaching
for logs and monitoring for lifecycle changes is
tightly coupled, so some changes were needed to ensure
that the full set of services being `up`'d are _watched_
and the subset that should have logs shown are _attached_.
(This does mean faking the attach with an event but not
actually doing it.)
While handling that, I adjusted the context lifetimes
here, which improves error handling that gets shown to
the user and should help avoid potential leaks by getting
rid of a `context.Background()`.
Signed-off-by: Milas Bowman <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
if s . Attach == nil || * s . Attach {
2023-12-29 11:45:45 +01:00
attachSet . Add ( serviceName )
up: handle various attach use cases better
By default, `compose up` attaches to all services (i.e.
shows log output from every associated container). If
a service is specified, e.g. `compose up foo`, then
only `foo`'s logs are tailed. The `--attach-dependencies`
flag can also be used, so that if `foo` depended upon
`bar`, then `bar`'s logs would also be followed. It's
also possible to use `--no-attach` to filter out one
or more services explicitly, e.g. `compose up --no-attach=noisy`
would launch all services, including `noisy`, and would
show log output from every service _except_ `noisy`.
Lastly, it's possible to use `up --attach` to explicitly
restrict to a subset of services (or their dependencies).
How these flags interact with each other is also worth
thinking through.
There were a few different connected issues here, but
the primary issue was that running `compose up foo` was
always attaching dependencies regardless of `--attach-dependencies`.
The filtering logic here has been updated so that it
behaves predictably both when launching all services
(`compose up`) or a subset (`compose up foo`) as well
as various flag combinations on top of those.
Notably, this required making some changes to how it
watches containers. The logic here between attaching
for logs and monitoring for lifecycle changes is
tightly coupled, so some changes were needed to ensure
that the full set of services being `up`'d are _watched_
and the subset that should have logs shown are _attached_.
(This does mean faking the attach with an event but not
actually doing it.)
While handling that, I adjusted the context lifetimes
here, which improves error handling that gets shown to
the user and should help avoid potential leaks by getting
rid of a `context.Background()`.
Signed-off-by: Milas Bowman <milas.bowman@docker.com>
2023-08-17 17:43:13 -04:00
}
return nil
} , dependencyOpt ) ; err != nil {
return err
}
}
// filter out any services that have been explicitly marked for ignore with `--no-attach`
attachSet . RemoveAll ( upOptions . noAttach ... )
attach = attachSet . Elements ( )
}
timeout := time . Duration ( upOptions . waitTimeout ) * time . Second
2021-06-14 16:26:14 +02:00
return backend . Up ( ctx , project , api . UpOptions {
2021-06-07 14:21:55 +02:00
Create : create ,
2021-06-14 16:26:14 +02:00
Start : api . StartOptions {
2024-03-08 14:07:51 +00:00
Project : project ,
Attach : consumer ,
AttachTo : attach ,
ExitCodeFrom : upOptions . exitCodeFrom ,
2024-04-03 09:51:11 +02:00
OnExit : upOptions . OnExit ( ) ,
2024-03-08 14:07:51 +00:00
Wait : upOptions . wait ,
WaitTimeout : timeout ,
Watch : upOptions . watch ,
Services : services ,
2024-04-24 19:59:42 +01:00
NavigationMenu : upOptions . navigationMenu && ui . Mode != "plain" ,
2021-06-07 14:21:55 +02:00
} ,
2021-02-08 11:04:46 +01:00
} )
2021-02-09 12:40:31 +01:00
}
2023-11-08 10:19:24 +01:00
func setServiceScale ( project * types . Project , name string , replicas int ) error {
2023-11-27 11:02:19 +01:00
service , err := project . GetService ( name )
if err != nil {
return err
2021-02-12 14:54:57 +01:00
}
2023-11-27 11:02:19 +01:00
service . SetScale ( replicas )
project . Services [ name ] = service
return nil
2021-02-12 14:54:57 +01:00
}