mirror of
https://github.com/docker/compose.git
synced 2026-06-22 14:35:23 +00:00
Merge pull request #326 from docker/run_follow_logs
Run containers and attach to logs by default, detach with -d option
This commit is contained in:
commit
4a1b50a9cd
7 changed files with 76 additions and 12 deletions
|
|
@ -57,9 +57,7 @@ func runLogs(ctx context.Context, containerName string, opts logsOpts) error {
|
|||
if err != nil {
|
||||
return errors.Wrap(err, "cannot connect to backend")
|
||||
}
|
||||
var con io.Writer
|
||||
|
||||
con = os.Stdout
|
||||
var con io.Writer = os.Stdout
|
||||
if c, err := console.ConsoleFromFile(os.Stdout); err == nil {
|
||||
con = c
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,9 +19,14 @@ package run
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/containerd/console"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/docker/api/containers"
|
||||
|
||||
"github.com/docker/api/cli/options/run"
|
||||
"github.com/docker/api/client"
|
||||
"github.com/docker/api/progress"
|
||||
|
|
@ -43,7 +48,7 @@ func Command() *cobra.Command {
|
|||
cmd.Flags().StringVar(&opts.Name, "name", "", "Assign a name to the container")
|
||||
cmd.Flags().StringArrayVarP(&opts.Labels, "label", "l", []string{}, "Set meta data on a container")
|
||||
cmd.Flags().StringArrayVarP(&opts.Volumes, "volume", "v", []string{}, "Volume. Ex: user:key@my_share:/absolute/path/to/target")
|
||||
cmd.Flags().BoolP("detach", "d", true, "Run container in background and print container ID")
|
||||
cmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, "Run container in background and print container ID")
|
||||
cmd.Flags().Float64Var(&opts.Cpus, "cpus", 1., "Number of CPUs")
|
||||
cmd.Flags().VarP(&opts.Memory, "memory", "m", "Memory limit")
|
||||
|
||||
|
|
@ -64,8 +69,22 @@ func runRun(ctx context.Context, image string, opts run.Opts) error {
|
|||
err = progress.Run(ctx, func(ctx context.Context) error {
|
||||
return c.ContainerService().Run(ctx, containerConfig)
|
||||
})
|
||||
if err == nil {
|
||||
fmt.Println(opts.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
if !opts.Detach {
|
||||
var con io.Writer = os.Stdout
|
||||
if c, err := console.ConsoleFromFile(os.Stdout); err == nil {
|
||||
con = c
|
||||
}
|
||||
|
||||
req := containers.LogsRequest{
|
||||
Follow: true,
|
||||
Writer: con,
|
||||
}
|
||||
|
||||
return c.ContainerService().Logs(ctx, opts.Name, req)
|
||||
}
|
||||
fmt.Println(opts.Name)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
2
cli/cmd/run/testdata/run-help.golden
vendored
2
cli/cmd/run/testdata/run-help.golden
vendored
|
|
@ -5,7 +5,7 @@ Usage:
|
|||
|
||||
Flags:
|
||||
--cpus float Number of CPUs (default 1)
|
||||
-d, --detach Run container in background and print container ID (default true)
|
||||
-d, --detach Run container in background and print container ID
|
||||
-l, --label stringArray Set meta data on a container
|
||||
-m, --memory bytes Memory limit
|
||||
--name string Assign a name to the container
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ type Opts struct {
|
|||
Volumes []string
|
||||
Cpus float64
|
||||
Memory formatter.MemBytes
|
||||
Detach bool
|
||||
}
|
||||
|
||||
// ToContainerConfig convert run options to a container configuration
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue