mirror of
https://github.com/docker/compose.git
synced 2025-05-23 16:00:14 +02:00
when --index is not set select first service container
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
be22bc735a
commit
b0af2deb2b
@ -64,10 +64,10 @@ func copyCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
flags := copyCmd.Flags()
|
flags := copyCmd.Flags()
|
||||||
flags.IntVar(&opts.index, "index", 0, "Index of the container if there are multiple instances of a service .")
|
flags.IntVar(&opts.index, "index", 0, "index of the container if service has multiple replicas")
|
||||||
flags.BoolVar(&opts.all, "all", false, "Copy to all the containers of the service.")
|
flags.BoolVar(&opts.all, "all", false, "copy to all the containers of the service.")
|
||||||
flags.MarkHidden("all") //nolint:errcheck
|
flags.MarkHidden("all") //nolint:errcheck
|
||||||
flags.MarkDeprecated("all", "By default all the containers of the service will get the source file/directory to be copied.") //nolint:errcheck
|
flags.MarkDeprecated("all", "by default all the containers of the service will get the source file/directory to be copied.") //nolint:errcheck
|
||||||
flags.BoolVarP(&opts.followLink, "follow-link", "L", false, "Always follow symbol link in SRC_PATH")
|
flags.BoolVarP(&opts.followLink, "follow-link", "L", false, "Always follow symbol link in SRC_PATH")
|
||||||
flags.BoolVarP(&opts.copyUIDGID, "archive", "a", false, "Archive mode (copy all uid/gid information)")
|
flags.BoolVarP(&opts.copyUIDGID, "archive", "a", false, "Archive mode (copy all uid/gid information)")
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ func execCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *c
|
|||||||
|
|
||||||
runCmd.Flags().BoolVarP(&opts.detach, "detach", "d", false, "Detached mode: Run command in the background.")
|
runCmd.Flags().BoolVarP(&opts.detach, "detach", "d", false, "Detached mode: Run command in the background.")
|
||||||
runCmd.Flags().StringArrayVarP(&opts.environment, "env", "e", []string{}, "Set environment variables")
|
runCmd.Flags().StringArrayVarP(&opts.environment, "env", "e", []string{}, "Set environment variables")
|
||||||
runCmd.Flags().IntVar(&opts.index, "index", 1, "index of the container if there are multiple instances of a service [default: 1].")
|
runCmd.Flags().IntVar(&opts.index, "index", 0, "index of the container if service has multiple replicas")
|
||||||
runCmd.Flags().BoolVarP(&opts.privileged, "privileged", "", false, "Give extended privileges to the process.")
|
runCmd.Flags().BoolVarP(&opts.privileged, "privileged", "", false, "Give extended privileges to the process.")
|
||||||
runCmd.Flags().StringVarP(&opts.user, "user", "u", "", "Run the command as this user.")
|
runCmd.Flags().StringVarP(&opts.user, "user", "u", "", "Run the command as this user.")
|
||||||
runCmd.Flags().BoolVarP(&opts.noTty, "no-TTY", "T", !streams.Out().IsTerminal(), "Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY.")
|
runCmd.Flags().BoolVarP(&opts.noTty, "no-TTY", "T", !streams.Out().IsTerminal(), "Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY.")
|
||||||
|
@ -57,7 +57,7 @@ func portCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *c
|
|||||||
ValidArgsFunction: completeServiceNames(p),
|
ValidArgsFunction: completeServiceNames(p),
|
||||||
}
|
}
|
||||||
cmd.Flags().StringVar(&opts.protocol, "protocol", "tcp", "tcp or udp")
|
cmd.Flags().StringVar(&opts.protocol, "protocol", "tcp", "tcp or udp")
|
||||||
cmd.Flags().IntVar(&opts.index, "index", 1, "index of the container if service has multiple replicas")
|
cmd.Flags().IntVar(&opts.index, "index", 0, "index of the container if service has multiple replicas")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,12 +5,12 @@ Copy files/folders between a service container and the local filesystem
|
|||||||
|
|
||||||
### Options
|
### Options
|
||||||
|
|
||||||
| Name | Type | Default | Description |
|
| Name | Type | Default | Description |
|
||||||
|:----------------------|:------|:--------|:----------------------------------------------------------------------|
|
|:----------------------|:------|:--------|:--------------------------------------------------------|
|
||||||
| `-a`, `--archive` | | | Archive mode (copy all uid/gid information) |
|
| `-a`, `--archive` | | | Archive mode (copy all uid/gid information) |
|
||||||
| `--dry-run` | | | Execute command in dry run mode |
|
| `--dry-run` | | | Execute command in dry run mode |
|
||||||
| `-L`, `--follow-link` | | | Always follow symbol link in SRC_PATH |
|
| `-L`, `--follow-link` | | | Always follow symbol link in SRC_PATH |
|
||||||
| `--index` | `int` | `0` | Index of the container if there are multiple instances of a service . |
|
| `--index` | `int` | `0` | index of the container if service has multiple replicas |
|
||||||
|
|
||||||
|
|
||||||
<!---MARKER_GEN_END-->
|
<!---MARKER_GEN_END-->
|
||||||
|
@ -5,16 +5,16 @@ Execute a command in a running container.
|
|||||||
|
|
||||||
### Options
|
### Options
|
||||||
|
|
||||||
| Name | Type | Default | Description |
|
| Name | Type | Default | Description |
|
||||||
|:------------------|:--------------|:--------|:----------------------------------------------------------------------------------|
|
|:------------------|:--------------|:--------|:---------------------------------------------------------------------------------|
|
||||||
| `-d`, `--detach` | | | Detached mode: Run command in the background. |
|
| `-d`, `--detach` | | | Detached mode: Run command in the background. |
|
||||||
| `--dry-run` | | | Execute command in dry run mode |
|
| `--dry-run` | | | Execute command in dry run mode |
|
||||||
| `-e`, `--env` | `stringArray` | | Set environment variables |
|
| `-e`, `--env` | `stringArray` | | Set environment variables |
|
||||||
| `--index` | `int` | `1` | index of the container if there are multiple instances of a service [default: 1]. |
|
| `--index` | `int` | `0` | index of the container if service has multiple replicas |
|
||||||
| `-T`, `--no-TTY` | | | Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY. |
|
| `-T`, `--no-TTY` | | | Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY. |
|
||||||
| `--privileged` | | | Give extended privileges to the process. |
|
| `--privileged` | | | Give extended privileges to the process. |
|
||||||
| `-u`, `--user` | `string` | | Run the command as this user. |
|
| `-u`, `--user` | `string` | | Run the command as this user. |
|
||||||
| `-w`, `--workdir` | `string` | | Path to workdir directory for this command. |
|
| `-w`, `--workdir` | `string` | | Path to workdir directory for this command. |
|
||||||
|
|
||||||
|
|
||||||
<!---MARKER_GEN_END-->
|
<!---MARKER_GEN_END-->
|
||||||
|
@ -8,7 +8,7 @@ Print the public port for a port binding.
|
|||||||
| Name | Type | Default | Description |
|
| Name | Type | Default | Description |
|
||||||
|:-------------|:---------|:--------|:--------------------------------------------------------|
|
|:-------------|:---------|:--------|:--------------------------------------------------------|
|
||||||
| `--dry-run` | | | Execute command in dry run mode |
|
| `--dry-run` | | | Execute command in dry run mode |
|
||||||
| `--index` | `int` | `1` | index of the container if service has multiple replicas |
|
| `--index` | `int` | `0` | index of the container if service has multiple replicas |
|
||||||
| `--protocol` | `string` | `tcp` | tcp or udp |
|
| `--protocol` | `string` | `tcp` | tcp or udp |
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ options:
|
|||||||
- option: all
|
- option: all
|
||||||
value_type: bool
|
value_type: bool
|
||||||
default_value: "false"
|
default_value: "false"
|
||||||
description: Copy to all the containers of the service.
|
description: copy to all the containers of the service.
|
||||||
deprecated: true
|
deprecated: true
|
||||||
hidden: true
|
hidden: true
|
||||||
experimental: false
|
experimental: false
|
||||||
@ -42,8 +42,7 @@ options:
|
|||||||
- option: index
|
- option: index
|
||||||
value_type: int
|
value_type: int
|
||||||
default_value: "0"
|
default_value: "0"
|
||||||
description: |
|
description: index of the container if service has multiple replicas
|
||||||
Index of the container if there are multiple instances of a service .
|
|
||||||
deprecated: false
|
deprecated: false
|
||||||
hidden: false
|
hidden: false
|
||||||
experimental: false
|
experimental: false
|
||||||
|
@ -33,9 +33,8 @@ options:
|
|||||||
swarm: false
|
swarm: false
|
||||||
- option: index
|
- option: index
|
||||||
value_type: int
|
value_type: int
|
||||||
default_value: "1"
|
default_value: "0"
|
||||||
description: |
|
description: index of the container if service has multiple replicas
|
||||||
index of the container if there are multiple instances of a service [default: 1].
|
|
||||||
deprecated: false
|
deprecated: false
|
||||||
hidden: false
|
hidden: false
|
||||||
experimental: false
|
experimental: false
|
||||||
|
@ -7,7 +7,7 @@ plink: docker_compose.yaml
|
|||||||
options:
|
options:
|
||||||
- option: index
|
- option: index
|
||||||
value_type: int
|
value_type: int
|
||||||
default_value: "1"
|
default_value: "0"
|
||||||
description: index of the container if service has multiple replicas
|
description: index of the container if service has multiple replicas
|
||||||
deprecated: false
|
deprecated: false
|
||||||
hidden: false
|
hidden: false
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
"github.com/docker/compose/v2/pkg/api"
|
||||||
"github.com/docker/compose/v2/pkg/utils"
|
"github.com/docker/compose/v2/pkg/utils"
|
||||||
@ -72,7 +73,9 @@ func getDefaultFilters(projectName string, oneOff oneOff, selectedServices ...st
|
|||||||
|
|
||||||
func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName string, oneOff oneOff, stopped bool, serviceName string, containerIndex int) (moby.Container, error) {
|
func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName string, oneOff oneOff, stopped bool, serviceName string, containerIndex int) (moby.Container, error) {
|
||||||
defaultFilters := getDefaultFilters(projectName, oneOff, serviceName)
|
defaultFilters := getDefaultFilters(projectName, oneOff, serviceName)
|
||||||
defaultFilters = append(defaultFilters, containerNumberFilter(containerIndex))
|
if containerIndex > 0 {
|
||||||
|
defaultFilters = append(defaultFilters, containerNumberFilter(containerIndex))
|
||||||
|
}
|
||||||
containers, err := s.apiClient().ContainerList(ctx, moby.ContainerListOptions{
|
containers, err := s.apiClient().ContainerList(ctx, moby.ContainerListOptions{
|
||||||
Filters: filters.NewArgs(
|
Filters: filters.NewArgs(
|
||||||
defaultFilters...,
|
defaultFilters...,
|
||||||
@ -83,8 +86,16 @@ func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName
|
|||||||
return moby.Container{}, err
|
return moby.Container{}, err
|
||||||
}
|
}
|
||||||
if len(containers) < 1 {
|
if len(containers) < 1 {
|
||||||
return moby.Container{}, fmt.Errorf("service %q is not running container #%d", serviceName, containerIndex)
|
if containerIndex > 0 {
|
||||||
|
return moby.Container{}, fmt.Errorf("service %q is not running container #%d", serviceName, containerIndex)
|
||||||
|
}
|
||||||
|
return moby.Container{}, fmt.Errorf("service %q is not running", serviceName)
|
||||||
}
|
}
|
||||||
|
sort.Slice(containers, func(i, j int) bool {
|
||||||
|
x, _ := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel])
|
||||||
|
y, _ := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel])
|
||||||
|
return x < y
|
||||||
|
})
|
||||||
container := containers[0]
|
container := containers[0]
|
||||||
return container, nil
|
return container, nil
|
||||||
}
|
}
|
||||||
|
@ -24,25 +24,14 @@ import (
|
|||||||
"github.com/docker/compose/v2/pkg/api"
|
"github.com/docker/compose/v2/pkg/api"
|
||||||
|
|
||||||
moby "github.com/docker/docker/api/types"
|
moby "github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *composeService) Port(ctx context.Context, projectName string, service string, port uint16, options api.PortOptions) (string, int, error) {
|
func (s *composeService) Port(ctx context.Context, projectName string, service string, port uint16, options api.PortOptions) (string, int, error) {
|
||||||
projectName = strings.ToLower(projectName)
|
projectName = strings.ToLower(projectName)
|
||||||
list, err := s.apiClient().ContainerList(ctx, moby.ContainerListOptions{
|
container, err := s.getSpecifiedContainer(ctx, projectName, oneOffInclude, false, service, options.Index)
|
||||||
Filters: filters.NewArgs(
|
|
||||||
projectFilter(projectName),
|
|
||||||
serviceFilter(service),
|
|
||||||
containerNumberFilter(options.Index),
|
|
||||||
),
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", 0, err
|
return "", 0, err
|
||||||
}
|
}
|
||||||
if len(list) == 0 {
|
|
||||||
return "", 0, fmt.Errorf("no container found for %s%s%d", service, api.Separator, options.Index)
|
|
||||||
}
|
|
||||||
container := list[0]
|
|
||||||
for _, p := range container.Ports {
|
for _, p := range container.Ports {
|
||||||
if p.PrivatePort == port && p.Type == options.Protocol {
|
if p.PrivatePort == port && p.Type == options.Protocol {
|
||||||
return p.IP, int(p.PublicPort), nil
|
return p.IP, int(p.PublicPort), nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user